springboot最佳实践springdatajpa操作mysql8

     2022-04-11     516

关键词:

一、Spring Data JPA 介绍

JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Spring Data JPA是在 Hibernate 基础上封装的一款框架。

开发环境

  • Spring Boot 2.0.4
  • Spring Data JPA 2.0.4
  • MySQL 8.0.12
  • JDK 8
  • IDEA 2018.2
  • Windows 10
## 二、集成步骤 ### 2.1 配置依赖 添加Spring Data JPA 和 MySQL Connector,配置pom.xml文件,代码如下: ```xml org.springframework.boot spring-boot-starter-data-jpa 2.0.4.RELEASE mysql mysql-connector-java 8.0.12 ``` 更多JPA版本:http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa 更多Mysql版本:http://mvnrepository.com/artifact/mysql/mysql-connector-java ### 2.2 application.properties 设置配置文件 ```xml ## 数据源配置 spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true ``` - hbm2ddl.auto:自动创建|更新|验证数据库表结构 - dialect:设置数据库引擎为InnoDB - show-sql:打印sql语句,方便调试 hbm2ddl.auto有四个属性: - create:每次加载 hibernate 时都会删除上一次的生成的表,然后根据你的 model 类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。[删除-创建-操作] - create-drop :每次加载 hibernate 时根据 model 类生成表,但是 sessionFactory 一关闭,表就自动删除。[删除-创建-操作-再删除] - update:最常用的属性,第一次加载 hibernate 时根据 model 类会自动建立起表的结构(前提是先建立好数据库),以后加载 hibernate 时根据 model 类自动更新表结构,即使表结构改变了,但表中的行仍然存在,不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。[没表-创建-操作 | 有表-更新没有的属性列-操作] - validate:每次加载 hibernate 时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。[启动验证表结构,验证不成功,项目启动失败] ### 2.3 增加实体类(Entity) ```java @Entity public class User implements Serializable { @Id @GeneratedValue private Long id; @Column(name = "name", nullable = false) private String name; @Column(nullable = false) private int age; @Column(nullable = false) private String pwd; public User(){} public User(String name, int age, String pwd) { this.name = name; this.age = age; this.pwd = pwd; } //...忽略set、get方法 } ``` - @GeneratedValue 自动生成id - @Column 设置列属性(name="数据库列名") - @Transient 不会映射到数据库 ### 2.4 创建 Repository 接口构建业务方法 ```java public interface UserRepository extends JpaRepository { public User findByName(String name); } ``` 继承JpaRepository之后就继承了: - Repository.save(user); // 插入或保存 - Repository.saveFlush(user); // 保存并刷新 - Repository.exists(1) // 主键查询是否存在 - Repository.findOne(1); // 主键查询单条 - Repository.delete(1); // 主键删除 - Repository.findByUsername("stone"); // 查询单条 - Repository.findAll(pageable); // 带排序和分页的查询列表 - Repository.saveState(1, 0); // 更新单个字段 这些方法,可以不写一行代码就可以实现对一个表的操作,当然你也可以扩展一些自己的方法,只需要在UserRepository里面添加方法即可。 ### 2.5 添加、查询数据库 ```java @Controller @RequestMapping("/") public class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/") public ModelAndView index() { userRepository.save(new User("老王",18,"123456")); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("dataSize", userRepository.findAll().size()); return modelAndView; } } ``` 到现在为止,集成 Spring Data JPA 已经全部完成了,启动调试,查看运行效果吧。 ## 三、高级使用 本节高级使用将会涉及的知识点如下: - 事务实现 - 根据名称自动生成SQL - 自定义Sql语句查询 ### 3.1 事务实现 #### 3.1.1 Spring事务实现步骤 实现事务,只需要两步即可: 步骤一、在application.properties配置数据库引擎为InnoDB: > spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect 步骤二、在方法或类上标识事务@Transactional 示例代码: ``` @Transactional public void saveGroup(){ userRepository.save(user); userRepository.save(user2); } ``` 如果出现错误,就会进行事务回滚。 #### 3.1.2 事务不生效的原因 ##### 3.1.2.1 确认数据库引擎 在application.properties配置数据库引擎为InnoDB: > spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect ##### 3.1.2.2 查看表的引擎必须为InnoDB 通过命令: > show table status from mytestdb; ![](http://icdn.apigo.cn/blog/mysql-select-table.png) 修改表的引擎: > alter table table_name engine=innodb; ##### 3.1.2.3 注意引入@Transactional的命名空间 @Transactional注解来自org.springframework.transaction.annotation包,而不是javax.transaction. ### 3.2 根据名称自动生成SQL JPA支持根据简单的关键字自动生成Sql查询的方法,比如根据name和age的组合查询,代码如下: > public User findByNameAndAge(String name,int age); 使用关键字“And”即可,或者查询时间区间的: > public User findByStartDateBetween(Long startDate); 使用关键字“Between”即可。 更多内部支持的关键字,如下表: | Keyword | Sample | JPQL snippet | | ------------------- | -------------------------------------- | ------------------------------------------------------------ | | And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 | | Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 | | Is,Equals | findByFirstname,findByFirstnameIs | … where x.firstname = ?1 | | Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 | | LessThan | findByAgeLessThan | … where x.age < ?1 | | LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 | | GreaterThan | findByAgeGreaterThan | … where x.age > ?1 | | GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 | | After | findByStartDateAfter | … where x.startDate > ?1 | | Before | findByStartDateBefore | … where x.startDate < ?1 | | IsNull | findByAgeIsNull | … where x.age is null | | IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null | | Like | findByFirstnameLike | … where x.firstname like ?1 | | NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 | | StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1(parameter bound with appended %) | | EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1(parameter bound with prepended %) | | Containing | findByFirstnameContaining | … where x.firstname like ?1(parameter bound wrapped in %) | | OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc | | Not | findByLastnameNot | … where x.lastname <> ?1 | | In | findByAgeIn(Collection ages) | … where x.age in ?1 | | NotIn | findByAgeNotIn(Collection ages) | … where x.age not in ?1 | | True | findByActiveTrue() | … where x.active = true | | False | findByActiveFalse() | … where x.active = false | | IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) | 官方文档:https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#jpa.repositories ### 3.3 自定义Sql语句查询 对于用户自己编写sql,Spring Boot JPA也有很好的支持,只需要添加@Query(sql)即可。 示例代码: ```java @Transactional @Modifying @Query("update User set name=?1 where id=?2") public int modifyName(String name,Long id); ``` 注意:在执行修改和删除的时候必须添加@Modifying注解,ORM才知道要执行写操作,update/delete query 的时候,也必须需要加上@Transactional(事务)才能正常操作。 ## 四、常见错误 在 Spring Data JPA 的使用当中,可能会遇到如下的一些错误。 ### 1.No default constructor for entity 实体类Entity没有空参数的默认构造函数,新增即可解决。 ### 2.java.sql.SQLException: Access denied for user ‘‘@‘172.17.0.1‘ (using password: NO) 启动项目报错,用户名和密码配置的key有误,MySQL8的用户名和密码配置和之前的不一样,MySQL 8 正确的用户名密码配置如下: ``` spring.datasource.username=root spring.datasource.password=123456 # 以下为配置老数据库驱动配置 #spring.datasource.data-username=root #spring.datasource.data-password=123456 ``` ### 3.Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver MySQL 8 的spring.datasource.driver-class-name配置需要改为“com.mysql.cj.jdbc.Driver”而不是“com.mysql.jdbc.Driver”,正确配置如下: ``` spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ```

springboot生产中16条最佳实践(代码片段)

...限时折扣中,一本深入讲解Java基础的干货笔记!SpringBoot是最流行的用于开发微服务的Java框架。在本文中,我将与你分享自2016年以来我在专业开发中使用SpringBoot所采用的最佳实践。这些内容是基于我的个人经验和一... 查看详情

springboot整合dubbo-服务化最佳实践

分包:公共的模型、接口、异常都放在此处(springboot-interface-api)将springboot-meeting-service、springboot-user-service系统pojo和service提取到springboot-interface-api1.分包——新建普通maven项目      springboot 查看详情

springboot2.x最佳实践《一》之springboot2.x初体验

SpringBoot2.X最佳实践前言本系列文章,从零基础接触 SpringBoot2.x新版本,基础入门使用,热部署,到整合各个主流框架Redis4.x,消息队列AciveMQ,RocketMQ等,搜索框架ElasticSearch5.6版本,到web-flux反应式编程,到Actuator监控应用信息... 查看详情

springboot与mybatis整合最佳实践

前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见。但是今天我会从实战的角度出发,谈谈我对二者结合与使用的最佳实践。一、依... 查看详情

springboot自定义kafka消费者配置containerfactory最佳实践

SpringBoot自定义kafka消费者配置ContainerFactory最佳实践本篇博文主要提供一个在SpringBoot中自定义kafka配置的实践,想象这样一个场景:你的系统需要监听多个不同集群的消息,在不同的集群中topic冲突了,所以你需要分别定义kafka消... 查看详情

springboot之logback日志最佳实践

一、SpringBoot日志介绍  SpringBoot对所有内部日志记录使用了CommonsLogging,但是底层日志实现是开放的。为JavaUtil日志记录、Log4J2和Logback提供了缺省配置。在每种情况下,日志记录器都预先配置为使用控制台输出和可选的文件输... 查看详情

[springboot]springboot最佳实践模板引擎thymeleaf集成

1Thymeleaf介绍  Thymeleaf是一种JavaXML/XHTML/HTML5模板引擎,可以在Web和非Web环境中使用。它更适合在基于MVC的Web应用程序的视图层提供XHTML/HTML5,但即使在脱机环境中,它也可以处理任何XML文件。它提供了完整的SpringFramework集成。... 查看详情

[springboot]springboot最佳实践模板引擎thymeleaf集成

1Thymeleaf介绍  Thymeleaf是一种JavaXML/XHTML/HTML5模板引擎,可以在Web和非Web环境中使用。它更适合在基于MVC的Web应用程序的视图层提供XHTML/HTML5,但即使在脱机环境中,它也可以处理任何XML文件。它提供了完整的SpringFramework集成。... 查看详情

springboot最佳实践,它来了,它来了(代码片段)

前言上篇博文(SpringBoot自动配置原理,你真的懂吗?)我们详细的介绍了SpringBoot是如何完成自动配置功能的,以及具体的原理。SpringBoot的强大,大家也有所了解了,SpringBoot更加强大的地方是提供了很... 查看详情

springboot实践总结(转)

...服务的Java框架。在本文主要分享的是在专业开发中使用SpringBoot所采用的最佳实践。这些内容是基于个人经验和一些熟知的SpringBoot专家的文章。 在本文中重点介绍SpringBoot特有的实践(大多数时候,也适用于Spring项目)。以... 查看详情

springboot最佳实践集成jsp与生产环境部署

...市面上仍有很多的公司在使用JSP,所以本文就来介绍一下SpringBoot怎么集成JSP开发,以及生产环境的详细部署方法。二、集成JSP开发环境SpringBoot2.0.4RELEASETomcat9.0.10IDEA(IntellijIDEA简称,下文统 查看详情

springboot最佳实践springdatajpa操作mysql8

...SpringDataJPA是在Hibernate基础上封装的一款框架。开发环境SpringBoot2.0.4SpringDataJPA2.0.4MySQL8.0.12JDK8IDEA2018.2Windows10##二、集成步 查看详情

springboot生产中16条最佳实践

...依赖2、使用自动配置3、使用SpringInitializr来开始一个新的SpringBoot项目4、考虑为常见的组织问题创建自己的自动配置5、正确设计代码目 查看详情

springboot最佳实践模板引擎thymeleaf集成

一、Thymeleaf介绍Thymeleaf是一种JavaXML/XHTML/HTML5模板引擎,可以在Web和非Web环境中使用。它更适合在基于MVC的Web应用程序的视图层提供XHTML/HTML5,但即使在脱机环境中,它也可以处理任何XML文件。它提供了完整的SpringFramework集成。关... 查看详情

springboot最佳实践模板引擎thymeleaf集成

一、Thymeleaf介绍Thymeleaf是一种JavaXML/XHTML/HTML5模板引擎,可以在Web和非Web环境中使用。它更适合在基于MVC的Web应用程序的视图层提供XHTML/HTML5,但即使在脱机环境中,它也可以处理任何XML文件。它提供了完整的SpringFramework集成。关... 查看详情

Spring Boot 中 JWT 的最佳实践是啥?

】SpringBoot中JWT的最佳实践是啥?【英文标题】:WhatisthebestpracticeforJWTinSpringBoot?SpringBoot中JWT的最佳实践是什么?【发布时间】:2022-01-0711:24:14【问题描述】:我想用SpringBoot和JWT启动一个项目,我看到了一些将刷新令牌保存在数... 查看详情

最佳实践|springboot应用如何快速接入prometheus监控

简介:SpringBoot微服务的开发、发布与部署只占其生命周期的一小部分,应用和系统运维才是重中之重。而运维过程中,监控工作更是占据重要位置。那么,为了对系统的状态进行持续地观测,面向SpringBoot应用... 查看详情

最佳实践|springboot应用如何快速接入prometheus监控(代码片段)

<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><groupId>io.micrometer</groupId><artifactId>micrometer-registr 查看详情