自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringBoot集成JPA用法筆記

開發(fā) 前端
今天給大家整理SpringBoot集成JPA用法。希望對(duì)大家能有所幫助!

今天給大家整理SpringBoot集成JPA用法。希望對(duì)大家能有所幫助!

1.搭建SpringBoot項(xiàng)目

2.新建配置文件 application.yml

  1. server: 
  2. port: 8090 
  3. spring: 
  4. #通用的數(shù)據(jù)源配置 
  5.   datasource: 
  6. driverClassName: com.mysql.jdbc.Driver 
  7. url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8 
  8. username: root 
  9. password: root 
  10. jpa: 
  11. #將默認(rèn)的存儲(chǔ)引擎切換為 InnoDB 
  12.     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect 
  13. #配置在日志中打印出執(zhí)行的 SQL 語句信息。 
  14.     show-sql: true 
  15.     hibernate: 
  16. #配置指明在程序啟動(dòng)的時(shí)候要?jiǎng)h除并且創(chuàng)建實(shí)體類對(duì)應(yīng)的表 
  17.       # validate 加載 Hibernate 時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu) 
  18.       # create 每次加載 Hibernate ,重新創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫表數(shù)據(jù)丟失的原因。 
  19.       # create-drop 加載 Hibernate 時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)(退出是指退出sessionFactory) 
  20.       # update 加載 Hibernate 自動(dòng)更新數(shù)據(jù)庫結(jié)構(gòu) 
  21.       # none 不啟用 
  22.       ddl-auto: none 

 3、新建用戶實(shí)體類 UserInfoDAO.java

  1. package my.springboot.jpa.entity; 
  2. import javax.persistence.*; 
  3. import java.util.Date
  4. /** 
  5.  * 用戶表實(shí)體 
  6.  * **/ 
  7. @Entity 
  8. @Table(name = "userinfo"
  9. public class UserInfoDAO { 
  10. @Id 
  11. @GeneratedValue(strategy = GenerationType.IDENTITY) 
  12. private  Integer id; 
  13. @Column 
  14. private String userName; 
  15. @Column 
  16. private Integer age; 
  17. @Column(length = 500) 
  18. private String address; 
  19. @Column(name = "create_date"
  20. private Date createDate; 
  21. @Column(name = "create_user"
  22. private String createUser; 
  23.  
  24. public Integer getId() { 
  25. return id; 
  26.     } 
  27.  
  28. public void setId(Integer id) { 
  29. this.id = id; 
  30.     } 
  31.  
  32. public String getUserName() { 
  33. return userName; 
  34.     } 
  35.  
  36. public void setUserName(String userName) { 
  37. this.userName = userName; 
  38.     } 
  39.  
  40. public Integer getAge() { 
  41. return age; 
  42.     } 
  43.  
  44. public void setAge(Integer age) { 
  45. this.age = age; 
  46.     } 
  47.  
  48. public String getAddress() { 
  49. return address; 
  50.     } 
  51.  
  52. public void setAddress(String address) { 
  53. this.address = address; 
  54.     } 
  55.  
  56. public Date getCreateDate() { 
  57. return createDate; 
  58.     } 
  59.  
  60. public void setCreateDate(Date createDate) { 
  61. this.createDate = createDate; 
  62.     } 
  63.  
  64. public String getCreateUser() { 
  65. return createUser; 
  66.     } 
  67.  
  68. public void setCreateUser(String createUser) { 
  69. this.createUser = createUser; 
  70.     } 

4、倉(cāng)庫接口類 UserIfoRepository

  1. package my.springboot.jpa.dao; 
  2. import my.springboot.jpa.entity.UserInfoDAO; 
  3. import org.springframework.data.jpa.repository.JpaRepository; 
  4. import org.springframework.stereotype.Repository; 
  5. /** 
  6.  * 倉(cāng)庫接口類 UserIfoRepository 
  7.  **/ 
  8. @Repository 
  9. public interface UserIfoRepository extends  
  10. JpaRepository<UserInfoDAO, Integer> { 

5、新建測(cè)試用戶類 UserInfoTest.java

  1. package my.springboot.jpa; 
  2.  
  3. import my.springboot.jpa.dao.UserIfoRepository; 
  4. import my.springboot.jpa.entity.UserInfoDAO; 
  5. import org.junit.Test; 
  6. import org.junit.runner.RunWith; 
  7. import org.springframework.beans.factory.annotation.Autowired; 
  8. import org.springframework.boot.test.context.SpringBootTest; 
  9. import org.springframework.test.context.junit4.SpringRunner; 
  10. import java.util.Date
  11. import java.util.List; 
  12. import java.util.Optional; 
  13.  
  14. /** 
  15.  * 測(cè)試UserInfo用法 
  16.  **/ 
  17. @RunWith(SpringRunner.class) 
  18. @SpringBootTest 
  19. public class UserInfoTest { 
  20. @Autowired 
  21.     UserIfoRepository userIfoRepository; 
  22.  
  23. @Test 
  24.     public void test() { 
  25. //插入用戶測(cè)試 
  26.         UserInfoDAO dao = new UserInfoDAO(); 
  27.         dao.setUserName("小明"); 
  28.         dao.setAge(32); 
  29.         dao.setCreateDate(new Date()); 
  30.         dao.setCreateUser("管理員"); 
  31.         dao.setAddress("蘇州"); 
  32. userIfoRepository.save(dao); 
  33.         UserInfoDAO dao2 = new UserInfoDAO(); 
  34.         dao2.setUserName("小張"); 
  35.         dao2.setAge(35); 
  36.         dao2.setCreateDate(new Date()); 
  37.         dao2.setCreateUser("管理員"); 
  38.         dao2.setAddress("南京"); 
  39. userIfoRepository.save(dao2); 
  40.  
  41. // 查詢多條記錄 打印 
  42.         List<UserInfoDAO> list = userIfoRepository.findAll(); 
  43. for (UserInfoDAO item : list) { 
  44.  
  45.             System.out.println("姓名:" + item.getUserName()  
  46. " 年齡:" + item.getAge());        } 
  47. // 查詢單個(gè)記錄 
  48.         Optional<UserInfoDAO> mo = userIfoRepository.findById(2); 
  49.         System.out.println(mo.get().getUserName()); 
  50. //更新操作 
  51.         mo.get().setUserName("小明123"); 
  52. userIfoRepository.save(mo.get()); 
  53.         System.out.println(mo.get().getUserName()); 
  54. //刪除記錄 
  55.         userIfoRepository.delete(mo.get()); 
  56.     } 

6、配置生成數(shù)據(jù)表

  1. package my.springboot.jpa; 
  2. import org.springframework.boot.SpringApplication; 
  3. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  4. import org.springframework.boot.autoconfigure.domain.EntityScan; 
  5. import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
  6.  
  7. @SpringBootApplication 
  8. @EntityScan(basePackages = {"my.springboot.jpa.entity"}) 
  9. @EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"}) 
  10. public class JpaApplication { 
  11. public static void main(String[] args) { 
  12.         SpringApplication.run(JpaApplication.class, args); 
  13.     } 
  14.  
  15.  
  16.  
  17.  
  18. @EntityScan(basePackages = {"my.springboot.jpa.entity"}) 
  19. @EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"}) 

7、項(xiàng)目結(jié)構(gòu)圖

本文轉(zhuǎn)載自微信公眾號(hào)「IT技術(shù)分享社區(qū)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系IT技術(shù)分享社區(qū)公眾號(hào)。

 

責(zé)任編輯:姜華 來源: IT技術(shù)分享社區(qū)
相關(guān)推薦

2021-07-11 07:05:28

RedisSpringBoot用法

2021-06-05 07:34:00

SpringBootMybatis用法

2021-09-26 05:02:00

緩存Ehcache用法

2009-06-01 16:18:30

SpringJPA集成

2023-09-08 09:10:33

SpringBoot微服務(wù)架構(gòu)

2023-08-07 14:28:07

SpringBoot工具

2023-01-11 15:11:36

SpringEhcache

2010-08-16 15:11:02

DIV

2021-12-22 22:32:48

鴻蒙HarmonyOS應(yīng)用

2010-08-26 16:40:35

DIV定位

2010-09-14 16:20:19

DIV定位

2009-12-02 17:07:27

LINUX系統(tǒng)

2010-07-12 15:16:29

UML關(guān)聯(lián)

2012-04-09 11:29:55

ibmdw

2012-03-06 11:25:40

ibmdw

2022-08-31 07:24:56

Docker日志命令

2010-07-26 10:09:01

Perl split函

2010-08-25 15:15:52

CSSclip

2010-05-13 10:47:20

Collabnet S

2024-08-29 08:58:30

JPA編寫數(shù)據(jù)操
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)