SpringBoot集成JPA用法笔记

开发 前端
今天给大家整理SpringBoot集成JPA用法。希望对大家能有所帮助!

今天给大家整理SpringBoot集成JPA用法。希望对大家能有所帮助!

1.搭建SpringBoot项目

2.新建配置文件 application.yml

  1. server: 
  2. port: 8090 
  3. spring: 
  4. #通用的数据源配置 
  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. #将默认的存储引擎切换为 InnoDB 
  12.     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect 
  13. #配置在日志中打印出执行的 SQL 语句信息。 
  14.     show-sql: true 
  15.     hibernate: 
  16. #配置指明在程序启动的时候要删除并且创建实体类对应的表 
  17.       # validate 加载 Hibernate 时,验证创建数据库表结构 
  18.       # create 每次加载 Hibernate ,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 
  19.       # create-drop 加载 Hibernate 时创建,退出是删除表结构(退出是指退出sessionFactory) 
  20.       # update 加载 Hibernate 自动更新数据库结构 
  21.       # none 不启用 
  22.       ddl-auto: none 

 3、新建用户实体类 UserInfoDAO.java

  1. package my.springboot.jpa.entity; 
  2. import javax.persistence.*; 
  3. import java.util.Date
  4. /** 
  5.  * 用户表实体 
  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、仓库接口类 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.  * 仓库接口类 UserIfoRepository 
  7.  **/ 
  8. @Repository 
  9. public interface UserIfoRepository extends  
  10. JpaRepository<UserInfoDAO, Integer> { 

5、新建测试用户类 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.  * 测试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. //插入用户测试 
  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. // 查询单个记录 
  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、配置生成数据表

  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、项目结构图

本文转载自微信公众号「IT技术分享社区」,可以通过以下二维码关注。转载本文请联系IT技术分享社区公众号。

 

责任编辑:姜华 来源: IT技术分享社区
相关推荐

2021-06-05 07:34:00

SpringBootMybatis用法

2021-07-11 07:05:28

RedisSpringBoot用法

2021-09-26 05:02:00

缓存Ehcache用法

2009-06-01 16:18:30

SpringJPA集成

2023-09-08 09:10:33

SpringBoot微服务架构

2023-08-07 14:28:07

SpringBoot工具

2023-01-11 15:11:36

SpringEhcache

2010-09-14 16:20:19

DIV定位

2012-04-09 11:29:55

ibmdw

2010-08-16 15:11:02

DIV

2021-12-22 22:32:48

鸿蒙HarmonyOS应用

2010-08-26 16:40:35

DIV定位

2012-03-06 11:25:40

ibmdw

2021-04-21 09:04:43

开发SpringBoot框架

2023-02-14 07:47:20

SpringBootEhcache

2010-05-13 10:47:20

Collabnet S

2010-08-25 15:15:52

CSSclip

2010-07-26 10:09:01

Perl split函

2022-08-31 07:24:56

Docker日志命令

2009-07-29 17:36:55

ibmdwJava
点赞
收藏

51CTO技术栈公众号