Spring Boot整合Spring Data JPA

首先说明一下,这里使用的是Springboot2.2.6.RELEASE版本,由于Springboot迭代很快,所以要注意版本问题。

1、Spring Data是Spring提供的帮助操作数据的框架,Spring Data中的一个模块叫做Spring Data JPA,Spring Data JPA只是Spring Data框架下的一个基于JPA标准操作数据的模块,Spring Data JPA底层默认的使用的是Hibernate来做的JPA实现。Spring Data JPA核心能力就是基于JPA的标准对数据进行操作,极大简化了代码的编写,简化操作持久层的代码,直接编写接口就可以了。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.6.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.bie</groupId>
12     <artifactId>springboot-jpa</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>springboot-jpa</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <!-- springBoot 的启动器 -->
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <!-- thymeleaf 的启动器 -->
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-thymeleaf</artifactId>
31         </dependency>
32         <!-- springBoot测试的启动器 -->
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-test</artifactId>
36             <scope>test</scope>
37             <!--<exclusions>
38                 <exclusion>
39                     <groupId>org.junit.vintage</groupId>
40                     <artifactId>junit-vintage-engine</artifactId>
41                 </exclusion>
42             </exclusions>-->
43         </dependency>
44 
45         <!-- Spring Data JPA 的启动器 -->
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-data-jpa</artifactId>
49         </dependency>
50 
51         <!-- mysql驱动包 -->
52         <dependency>
53             <groupId>mysql</groupId>
54             <artifactId>mysql-connector-java</artifactId>
55         </dependency>
56 
57         <!-- druid连接池 -->
58         <dependency>
59             <groupId>com.alibaba</groupId>
60             <artifactId>druid</artifactId>
61             <version>1.1.10</version>
62         </dependency>
63     </dependencies>
64 
65     <build>
66         <plugins>
67             <plugin>
68                 <groupId>org.springframework.boot</groupId>
69                 <artifactId>spring-boot-maven-plugin</artifactId>
70             </plugin>
71         </plugins>
72     </build>
73 
74 </project>

在application.properties配置文件中配置数据库链接信息、数据库链接池、Spring Data JPA开启正向工程、在控制台打印sql语句。

 1 # mysql的数据库驱动
 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 3 # mysql的链接
 4 spring.datasource.url=jdbc:mysql://localhost:3306/ssm
 5 # mysql的账号
 6 spring.datasource.username=root
 7 # mysql的密码
 8 spring.datasource.password=123456
 9 
10 # druid连接池的配置
11 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
12 
13 # Spring Data JPA,此配置可以在实体类中使用注解来创建数据表,开启正向工程
14 spring.jpa.hibernate.ddl-auto=update
15 # 在控制台打印sql语句
16 spring.jpa.show-sql=true

创建实体类Users。

 1 package com.bie.springboot.po;
 2 
 3 import javax.persistence.*;
 4 
 5 @Entity // 表示该类是实体类
 6 @Table(name = "tb_users") // 表示该实体类和数据表进行映射,name表示实体类和数据表进行映射
 7 // 如果使用的是正向工程的话,name属性的值表示的是数据表的表名称。
 8 public class Users {
 9 
10     @Id // 表示该字段是主键
11     @GeneratedValue(strategy = GenerationType.IDENTITY) // 主键的生成策略
12     @Column(name = "id") // 表示实体类的字段和数据表的字段进行映射的关系,如果是正向工程的话,name的值就是数据表的字段名称
13     private Integer id;// 用户编号
14 
15     @Column(name = "name")
16     private String name;// 用户姓名
17 
18     @Column(name = "age")
19     private Integer age;// 用户年龄
20 
21     @Column(name = "address")
22     private String address;// 用户地址
23 
24     // alt + insert来生成构造器、setter\getter等等方法
25     public Integer getId() {
26         return id;
27     }
28 
29     public void setId(Integer id) {
30         this.id = id;
31     }
32 
33     public String getName() {
34         return name;
35     }
36 
37     public void setName(String name) {
38         this.name = name;
39     }
40 
41     public Integer getAge() {
42         return age;
43     }
44 
45     public void setAge(Integer age) {
46         this.age = age;
47     }
48 
49     public String getAddress() {
50         return address;
51     }
52 
53     public void setAddress(String address) {
54         this.address = address;
55     }
56 
57     @Override
58     public String toString() {
59         return "Users{" +
60                 "id=" + id +
61                 ", name='" + name + '\'' +
62                 ", age=" + age +
63                 ", address='" + address + '\'' +
64                 '}';
65     }
66 
67     public Users(String name, Integer age, String address) {
68         this.name = name;
69         this.age = age;
70         this.address = address;
71     }
72 
73     public Users() {
74     }
75 }

创建数据交互层接口继承JpaRepository<T,ID>。

泛型参数1,T表示的是当前需要映射的实体类类型,当前需要映射的实体。

泛型参数2,ID表示需要映射的实体中的主键的类型,当前映射的实体中的OID的类型。

 1 package com.bie.springboot.dao;
 2 
 3 import com.bie.springboot.po.Users;
 4 import org.springframework.data.jpa.repository.JpaRepository;
 5 
 6 /**
 7  * JpaRepository<T,ID>
 8  * 泛型参数1,T表示的是当前需要映射的实体类类型,当前需要映射的实体。
 9  * 泛型参数2,ID表示需要映射的实体中的主键的类型,当前映射的实体中的OID的类型。
10  */
11 public interface UsersDao extends JpaRepository<Users, Integer> {
12 
13 }

开始测试,测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersDao;
 4 import com.bie.springboot.po.Users;
 5 import org.junit.jupiter.api.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.SpringJUnit4ClassRunner;
10 
11 @RunWith(SpringJUnit4ClassRunner.class)
12 @SpringBootTest(classes = SpringbootJpaApplication.class) // 启动器,将测试类和启动器整合在一起
13 class SpringbootJpaApplicationTests {
14 
15     @Autowired
16     private UsersDao usersDao;
17 
18     @Test
19     public void testSave() {
20         Users users = new Users();
21         users.setAddress("北京市海淀");
22         users.setAge(20);
23         users.setName("张三");
24         this.usersDao.save(users);
25     }
26 
27 }

如果下面报错,就在数据库链接URL后面根据下面所示的内容即可。

?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC

 1   .   ____          _            __ _ _
 2  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 4  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 5   '  |____| .__|_| |_|_| |_\__, | / / / /
 6  =========|_|==============|___/=/_/_/_/
 7  :: Spring Boot ::        (v2.2.6.RELEASE)
 8 
 9 2020-05-20 13:40:41.734  INFO 14492 --- [           main] c.b.s.SpringbootJpaApplicationTests      : Starting SpringbootJpaApplicationTests on DESKTOP-V37QSSE with PID 14492 (started by biehl in D:\program\idea\IntelliJ IDEA 2019.1.3\workspace_idea\springboot-jpa)
10 2020-05-20 13:40:41.736  INFO 14492 --- [           main] c.b.s.SpringbootJpaApplicationTests      : No active profile set, falling back to default profiles: default
11 2020-05-20 13:40:43.891  INFO 14492 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
12 2020-05-20 13:40:44.000  INFO 14492 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 96ms. Found 1 JPA repository interfaces.
13 2020-05-20 13:40:45.245  INFO 14492 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
14 2020-05-20 13:40:45.451  INFO 14492 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
15 2020-05-20 13:40:45.813  INFO 14492 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
16 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
17 2020-05-20 13:40:47.129  INFO 14492 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
18 2020-05-20 13:40:47.326 ERROR 14492 --- [reate-173197870] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://127.0.0.1:3306/biehl?useUnicode=true&characterEncoding=utf8, errorCode 0, state 01S00
19 
20 java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
21     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.19.jar:8.0.19]
22     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.19.jar:8.0.19]
23     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.19.jar:8.0.19]
24     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.19.jar:8.0.19]
25     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.19.jar:8.0.19]
26     at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) ~[mysql-connector-java-8.0.19.jar:8.0.19]
27     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.19.jar:8.0.19]
28     at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.19.jar:8.0.19]
29     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.19.jar:8.0.19]
30     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.19.jar:8.0.19]
31     at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1558) ~[druid-1.1.10.jar:1.1.10]
32     at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1623) ~[druid-1.1.10.jar:1.1.10]
33     at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2468) ~[druid-1.1.10.jar:1.1.10]
34 Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
35     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_191]
36     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_191]
37     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_191]
38     at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_191]
39     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar:8.0.19]
40     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) ~[mysql-connector-java-8.0.19.jar:8.0.19]
41     at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132) ~[mysql-connector-java-8.0.19.jar:8.0.19]
42     at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2118) ~[mysql-connector-java-8.0.19.jar:8.0.19]
43     at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2142) ~[mysql-connector-java-8.0.19.jar:8.0.19]
44     at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1310) ~[mysql-connector-java-8.0.19.jar:8.0.19]
45     at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967) ~[mysql-connector-java-8.0.19.jar:8.0.19]
46     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.19.jar:8.0.19]
47     ... 6 common frames omitted
48 
49 2020-05-20 13:40:47.330 ERROR 14492 --- [reate-173197870] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://127.0.0.1:3306/biehl?useUnicode=true&characterEncoding=utf8, errorCode 0, state 01S00

 

2、Spring Data JPA提供的核心接口。

2.1)、Repository接口,提供了方法名称命名查询方式。

 1 package com.bie.springboot.dao;
 2 
 3 import com.bie.springboot.po.Users;
 4 import org.springframework.data.repository.Repository;
 5 
 6 import java.util.List;
 7 
 8 /**
 9  * Repository接口的方法名称命名查询。
10  */
11 public interface UsersRepositoryByName extends Repository<Users, Integer> {
12 
13     /**
14      * 方法命名必须以findBy开头的,后面跟的是属性的名称,属性的名称必须大写,遵循驼峰式写法。
15      * findBy开头的,后面跟的就是查询条件。
16      * <p>
17      * 方法的名称必须要遵循驼峰式命名规则,findBy(关键字)+属性名称(首字母要大写)+查询条件(首字母大写)。
18      * 查询条件默认是等于。
19      * <p>
20      * 只要遵循findBy规则,有很多可以进行查询的条件。
21      *
22      * @param name
23      * @return
24      */
25     public List<Users> findByName(String name);
26 
27     /**
28      * @param name
29      * @return
30      */
31     public List<Users> findByNameEquals(String name);
32 
33     /**
34      * 根据姓名和年龄进行查询
35      *
36      * @param name
37      * @param age
38      * @return
39      */
40     public List<Users> findByNameAndAge(String name, Integer age);
41 
42     /**
43      * 根据姓名模糊查询
44      *
45      * @param name
46      * @return
47      */
48     public List<Users> findByNameLike(String name);
49 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersDao;
 4 import com.bie.springboot.dao.UsersRepositoryByName;
 5 import com.bie.springboot.po.Users;
 6 import org.junit.jupiter.api.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11 
12 import java.util.List;
13 
14 @RunWith(SpringJUnit4ClassRunner.class)
15 @SpringBootTest(classes = SpringbootJpaApplication.class)
16         // 启动器,将测试类和启动器整合在一起
17 class SpringbootJpaApplicationTests {
18 
19     @Autowired
20     private UsersRepositoryByName usersRepositoryByName;
21 
22 
23     @Test
24     public void testFindByName() {
25         List<Users> byName = this.usersRepositoryByName.findByName("张三");
26         System.out.println("Name: " + byName);
27     }
28 
29 
30     @Test
31     public void testFindByNameEquals() {
32         List<Users> byName = this.usersRepositoryByName.findByNameEquals("张三");
33         System.out.println("Name: " + byName);
34     }
35 
36 
37     @Test
38     public void testFindByNameAndAge() {
39         List<Users> byName = this.usersRepositoryByName.findByNameAndAge("张三", 20);
40         System.out.println("Name: " + byName);
41     }
42 
43     @Test
44     public void testFindByNameLike() {
45         List<Users> byName = this.usersRepositoryByName.findByNameLike("张%");
46         System.out.println("Name: " + byName);
47     }
48 
49 }

Repository接口,提供了基于@Query注解的查询与更新方式。

 1 package com.bie.springboot.dao;
 2 
 3 import com.bie.springboot.po.Users;
 4 import org.springframework.data.jpa.repository.Modifying;
 5 import org.springframework.data.jpa.repository.Query;
 6 import org.springframework.data.repository.Repository;
 7 
 8 import javax.transaction.Transactional;
 9 import java.util.List;
10 
11 /**
12  * Repository接口,提供了基于@Query注解的查询与更新方式。
13  */
14 public interface UsersRepositoryQueryAnnotation extends Repository<Users, Integer> {
15 
16     /**
17      * 默认支持的是HQL语句,不是标准的sql语句,底层需要进行转换,转换为标准的sql语句
18      *
19      * @param name
20      * @return
21      */
22     @Query(value = "from Users where name = :name")
23     public List<Users> queryByNameUseHQL(String name);
24 
25     /**
26      * 如果是SQL语句需要,nativeQuery = true表示的就是底层不需要进行sql语句的转换
27      *
28      * @param name
29      * @return
30      */
31     @Query(value = "select * from tb_users where name = ?", nativeQuery = true)
32     public List<Users> queryByNameUseSQL(String name);
33 
34     /**
35      * 更新操作,注意update后面是实体类的名称
36      *
37      * @param name
38      * @param id
39      */
40     @Query(value = "update Users set name = :name where id = :id")
41     @Modifying // 该注解表示需要执行一个更新操作。注意此方法调用需要开通事务的
42     @Transactional // 开启事务
43     public void updateUsersNameById(String name, Integer id);
44 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersRepositoryQueryAnnotation;
 4 import com.bie.springboot.po.Users;
 5 import org.junit.jupiter.api.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.SpringJUnit4ClassRunner;
10 
11 import java.util.List;
12 
13 @RunWith(SpringJUnit4ClassRunner.class)
14 @SpringBootTest(classes = SpringbootJpaApplication.class)
15         // 启动器,将测试类和启动器整合在一起
16 class SpringbootJpaApplicationTests {
17 
18     @Autowired
19     private UsersRepositoryQueryAnnotation usersRepositoryQueryAnnotation;
20 
21     @Test
22     public void testFindByNameUsrHQL() {
23         List<Users> byName = this.usersRepositoryQueryAnnotation.queryByNameUseHQL("张三");
24         System.out.println("Name: " + byName);
25     }
26 
27     @Test
28     public void testFindByNameUseSQL() {
29         List<Users> byName = this.usersRepositoryQueryAnnotation.queryByNameUseSQL("张三");
30         System.out.println("Name: " + byName);
31     }
32 
33     @Test
34     // @Transactional // 此方法调用需要添加事务,如果@Transactional注解和@Test注解配合使用,事务自动回滚
35     // @Rollback(value = false) // 取消自动回滚
36     public void testUpdateUsersNameById() {
37         this.usersRepositoryQueryAnnotation.updateUsersNameById("张飒飒2号", 1);
38     }
39 
40 }

如果报如下所示的错误,需要将@Query(value = "from Users where name = ?")修改为@Query(value = "from Users where name = :name")。

  1  .   ____          _            __ _ _
  2  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  5   '  |____| .__|_| |_|_| |_\__, | / / / /
  6  =========|_|==============|___/=/_/_/_/
  7  :: Spring Boot ::        (v2.2.6.RELEASE)
  8 
  9 2020-05-20 15:40:24.270  INFO 3352 --- [           main] c.b.s.SpringbootJpaApplicationTests      : Starting SpringbootJpaApplicationTests on DESKTOP-V37QSSE with PID 3352 (started by biehl in D:\program\idea\IntelliJ IDEA 2019.1.3\workspace_idea\springboot-jpa)
 10 2020-05-20 15:40:24.273  INFO 3352 --- [           main] c.b.s.SpringbootJpaApplicationTests      : No active profile set, falling back to default profiles: default
 11 2020-05-20 15:40:25.690  INFO 3352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
 12 2020-05-20 15:40:25.786  INFO 3352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 77ms. Found 3 JPA repository interfaces.
 13 2020-05-20 15:40:26.477  INFO 3352 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
 14 2020-05-20 15:40:26.569  INFO 3352 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
 15 2020-05-20 15:40:26.776  INFO 3352 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
 16 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
 17 2020-05-20 15:40:27.736  INFO 3352 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
 18 2020-05-20 15:40:27.895  INFO 3352 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
 19 2020-05-20 15:40:28.764  INFO 3352 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
 20 2020-05-20 15:40:28.771  INFO 3352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
 21 2020-05-20 15:40:29.064  WARN 3352 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
 22 2020-05-20 15:40:29.492  WARN 3352 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
 23 Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
 24 2020-05-20 15:40:29.492  INFO 3352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
 25 2020-05-20 15:40:29.506  INFO 3352 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
 26 2020-05-20 15:40:29.514  INFO 3352 --- [           main] ConditionEvaluationReportLoggingListener : 
 27 
 28 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
 29 2020-05-20 15:40:29.520 ERROR 3352 --- [           main] o.s.boot.SpringApplication               : Application run failed
 30 
 31 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
 32     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 33     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 34     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 35     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 36     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 37     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 38     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 39     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 40     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 41     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 42     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 43     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 44     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 45     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 46     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 47     at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 48     at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126) [spring-boot-test-2.2.6.RELEASE.jar:2.2.6.RELEASE]
 49     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 50     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 51     at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 52     at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 53     at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 54     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 55     at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:98) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 56     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$5(ClassBasedTestDescriptor.java:337) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 57     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:342) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 58     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:337) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 59     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_191]
 60     at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_191]
 61     at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) ~[na:1.8.0_191]
 62     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_191]
 63     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_191]
 64     at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312) ~[na:1.8.0_191]
 65     at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743) ~[na:1.8.0_191]
 66     at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742) ~[na:1.8.0_191]
 67     at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) ~[na:1.8.0_191]
 68     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:336) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 69     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:259) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 70     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:252) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 71     at java.util.Optional.orElseGet(Optional.java:267) ~[na:1.8.0_191]
 72     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:251) [junit-jupiter-engine-5.5.2.jar:5.5.2]
 73     at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:29) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
 74     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:106) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
 75     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 76     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:105) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
 77     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:69) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
 78     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 79     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 80     at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 81     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 82     at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_191]
 83     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 84     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 85     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 86     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 87     at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 88     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 89     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 90     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 91     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 92     at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_191]
 93     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 94     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 95     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 96     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 97     at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 98     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) ~[junit-platform-engine-1.5.2.jar:1.5.2]
 99     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
100     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) ~[junit-platform-engine-1.5.2.jar:1.5.2]
101     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) ~[junit-platform-engine-1.5.2.jar:1.5.2]
102     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) ~[junit-platform-engine-1.5.2.jar:1.5.2]
103     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.5.2.jar:1.5.2]
104     at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) ~[junit-platform-engine-1.5.2.jar:1.5.2]
105     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
106     at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
107     at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
108     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
109     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
110     at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69) ~[junit5-rt.jar:na]
111     at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) ~[junit-rt.jar:na]
112     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) ~[junit-rt.jar:na]
113     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) ~[junit-rt.jar:na]
114 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
115     at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
116     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
117     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
118     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
119     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
120     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
121     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
122     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
123     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
124     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
125     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
126     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
127     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
128     at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
129     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
130     ... 81 common frames omitted
131 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
132     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
133     at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
134     ... 95 common frames omitted
135 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
136     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
137     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
138     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
139     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
140     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
141     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
142     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
143     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1114) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
144     at org.springframework.data.repository.support.Repositories.cacheRepositoryFactory(Repositories.java:99) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
145     at org.springframework.data.repository.support.Repositories.populateRepositoryFactoryInformation(Repositories.java:92) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
146     at org.springframework.data.repository.support.Repositories.<init>(Repositories.java:85) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
147     at org.springframework.data.repository.support.DomainClassConverter.setApplicationContext(DomainClassConverter.java:109) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
148     at org.springframework.data.web.config.SpringDataWebConfiguration.addFormatters(SpringDataWebConfiguration.java:131) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
149     at org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.addFormatters(WebMvcConfigurerComposite.java:81) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
150     at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.addFormatters(DelegatingWebMvcConfiguration.java:78) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
151     at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.mvcConversionService(WebMvcAutoConfiguration.java:430) ~[spring-boot-autoconfigure-2.2.6.RELEASE.jar:2.2.6.RELEASE]
152     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
153     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
154     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
155     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
156     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
157     ... 96 common frames omitted
158 Caused by: java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
159     at org.springframework.util.Assert.isTrue(Assert.java:118) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
160     at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:72) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
161     at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:61) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
162     at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:76) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
163     at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:56) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
164     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:140) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
165     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:207) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
166     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:78) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
167     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:574) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
168     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:567) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
169     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_191]
170     at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_191]
171     at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049) ~[na:1.8.0_191]
172     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_191]
173     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_191]
174     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_191]
175     at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_191]
176     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_191]
177     at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_191]
178     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:569) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
179     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$0(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
180     at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_191]
181     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
182     at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:332) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
183     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
184     at org.springframework.data.util.Lazy.getNullable(Lazy.java:212) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
185     at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
186     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
187     at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:121) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
188     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
189     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
190     ... 116 common frames omitted
191 
192 2020-05-20 15:40:29.524 ERROR 3352 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@1de76cc7] to prepare test instance [com.bie.springboot.SpringbootJpaApplicationTests@4607d78b]
193 
194 java.lang.IllegalStateException: Failed to load ApplicationContext
195     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
196     at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
197     at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
198     at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
199     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
200     at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:98) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
201     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$5(ClassBasedTestDescriptor.java:337) [junit-jupiter-engine-5.5.2.jar:5.5.2]
202     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:342) [junit-jupiter-engine-5.5.2.jar:5.5.2]
203     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:337) [junit-jupiter-engine-5.5.2.jar:5.5.2]
204     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_191]
205     at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_191]
206     at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) ~[na:1.8.0_191]
207     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_191]
208     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_191]
209     at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312) ~[na:1.8.0_191]
210     at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743) ~[na:1.8.0_191]
211     at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742) ~[na:1.8.0_191]
212     at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) ~[na:1.8.0_191]
213     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:336) [junit-jupiter-engine-5.5.2.jar:5.5.2]
214     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:259) [junit-jupiter-engine-5.5.2.jar:5.5.2]
215     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:252) [junit-jupiter-engine-5.5.2.jar:5.5.2]
216     at java.util.Optional.orElseGet(Optional.java:267) ~[na:1.8.0_191]
217     at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:251) [junit-jupiter-engine-5.5.2.jar:5.5.2]
218     at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:29) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
219     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:106) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
220     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
221     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:105) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
222     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:69) ~[junit-jupiter-engine-5.5.2.jar:5.5.2]
223     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107) ~[junit-platform-engine-1.5.2.jar:1.5.2]
224     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
225     at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107) ~[junit-platform-engine-1.5.2.jar:1.5.2]
226     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75) ~[junit-platform-engine-1.5.2.jar:1.5.2]
227     at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_191]
228     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) ~[junit-platform-engine-1.5.2.jar:1.5.2]
229     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) ~[junit-platform-engine-1.5.2.jar:1.5.2]
230     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
231     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) ~[junit-platform-engine-1.5.2.jar:1.5.2]
232     at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) ~[junit-platform-engine-1.5.2.jar:1.5.2]
233     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) ~[junit-platform-engine-1.5.2.jar:1.5.2]
234     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
235     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) ~[junit-platform-engine-1.5.2.jar:1.5.2]
236     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) ~[junit-platform-engine-1.5.2.jar:1.5.2]
237     at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_191]
238     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) ~[junit-platform-engine-1.5.2.jar:1.5.2]
239     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) ~[junit-platform-engine-1.5.2.jar:1.5.2]
240     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
241     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) ~[junit-platform-engine-1.5.2.jar:1.5.2]
242     at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) ~[junit-platform-engine-1.5.2.jar:1.5.2]
243     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) ~[junit-platform-engine-1.5.2.jar:1.5.2]
244     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.5.2.jar:1.5.2]
245     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) ~[junit-platform-engine-1.5.2.jar:1.5.2]
246     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) ~[junit-platform-engine-1.5.2.jar:1.5.2]
247     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) ~[junit-platform-engine-1.5.2.jar:1.5.2]
248     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.5.2.jar:1.5.2]
249     at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) ~[junit-platform-engine-1.5.2.jar:1.5.2]
250     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
251     at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
252     at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
253     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
254     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
255     at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69) ~[junit5-rt.jar:na]
256     at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) ~[junit-rt.jar:na]
257     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) ~[junit-rt.jar:na]
258     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) ~[junit-rt.jar:na]
259 Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
260     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
261     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
262     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
263     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
264     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
265     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
266     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
267     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
268     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
269     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
270     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
271     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
272     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
273     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
274     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
275     at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
276     at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126) ~[spring-boot-test-2.2.6.RELEASE.jar:2.2.6.RELEASE]
277     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
278     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
279     ... 63 common frames omitted
280 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
281     at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
282     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
283     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
284     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
285     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
286     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
287     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
288     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
289     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
290     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
291     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
292     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
293     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
294     at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
295     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
296     ... 81 common frames omitted
297 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
298     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
299     at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
300     ... 95 common frames omitted
301 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepositoryQueryAnnotation': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
302     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
303     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
304     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
305     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
306     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
307     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
308     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
309     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1114) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
310     at org.springframework.data.repository.support.Repositories.cacheRepositoryFactory(Repositories.java:99) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
311     at org.springframework.data.repository.support.Repositories.populateRepositoryFactoryInformation(Repositories.java:92) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
312     at org.springframework.data.repository.support.Repositories.<init>(Repositories.java:85) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
313     at org.springframework.data.repository.support.DomainClassConverter.setApplicationContext(DomainClassConverter.java:109) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
314     at org.springframework.data.web.config.SpringDataWebConfiguration.addFormatters(SpringDataWebConfiguration.java:131) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
315     at org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.addFormatters(WebMvcConfigurerComposite.java:81) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
316     at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.addFormatters(DelegatingWebMvcConfiguration.java:78) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
317     at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.mvcConversionService(WebMvcAutoConfiguration.java:430) ~[spring-boot-autoconfigure-2.2.6.RELEASE.jar:2.2.6.RELEASE]
318     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
319     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
320     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
321     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
322     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
323     ... 96 common frames omitted
324 Caused by: java.lang.IllegalArgumentException: JDBC style parameters (?) are not supported for JPA queries.
325     at org.springframework.util.Assert.isTrue(Assert.java:118) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
326     at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:72) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
327     at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:61) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
328     at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:76) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
329     at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:56) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
330     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:140) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
331     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:207) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
332     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:78) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
333     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:574) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
334     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:567) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
335     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_191]
336     at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_191]
337     at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049) ~[na:1.8.0_191]
338     at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_191]
339     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_191]
340     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_191]
341     at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_191]
342     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_191]
343     at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_191]
344     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:569) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
345     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$0(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
346     at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_191]
347     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
348     at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:332) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
349     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
350     at org.springframework.data.util.Lazy.getNullable(Lazy.java:212) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
351     at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
352     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
353     at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:121) ~[spring-data-jpa-2.2.6.RELEASE.jar:2.2.6.RELEASE]
354     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
355     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
356     ... 116 common frames omitted
357 
358 
359 Process finished with exit code -1

 

2.2)、CrudRepository<T, ID>接口。CrudRepository接口,主要是完成一些增删改查的操作,注意,CrudRepository接口继承了Repository接口,所以CrudRepository接口可以使用Repository接口的功能。

 1 package com.bie.springboot.dao;
 2 
 3 import com.bie.springboot.po.Users;
 4 import org.springframework.data.repository.CrudRepository;
 5 
 6 /**
 7  * CrudRepository接口,主要是完成一些增删改查的操作,注意,CrudRepository接口继承了Repository接口,
 8  * 所以CrudRepository接口可以使用Repository接口的功能。
 9  */
10 public interface UsersRepositoryCrudRepository extends CrudRepository<Users, Integer> {
11 
12 
13 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersRepositoryCrudRepository;
 4 import com.bie.springboot.po.Users;
 5 import org.junit.jupiter.api.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.SpringJUnit4ClassRunner;
10 
11 import java.util.Iterator;
12 import java.util.Optional;
13 
14 @RunWith(SpringJUnit4ClassRunner.class)
15 @SpringBootTest(classes = SpringbootJpaApplication.class)
16         // 启动器,将测试类和启动器整合在一起
17 class SpringbootJpaApplicationTests {
18 
19     @Autowired
20     private UsersRepositoryCrudRepository usersRepositoryCrudRepository;
21 
22     /**
23      * 插入测试
24      */
25     @Test
26     public void testSave() {
27         // 默认开启了事务
28         Users users = new Users("亚瑟", 25, "上单");
29         Users save = this.usersRepositoryCrudRepository.save(users);
30         System.out.println("Users: " + save);
31     }
32 
33     /**
34      * 更新测试
35      */
36     @Test
37     public void testUpdate() {
38         // 默认开启了事务
39         Users users = new Users("亚瑟瑟", 26, "我上单");
40         users.setId(3);
41         // save方法,如果存在就更新,如果不存在就插入
42         Users save = this.usersRepositoryCrudRepository.save(users);
43         System.out.println("Users: " + save);
44     }
45 
46     /**
47      * 查询单个测试
48      */
49     @Test
50     public void testSelect() {
51         Optional<Users> usersOptional = this.usersRepositoryCrudRepository.findById(3);
52         Users users = usersOptional.orElse(null);
53         System.out.println("Users: " + users);
54     }
55 
56     /**
57      * 查询全部测试
58      */
59     @Test
60     public void testSelectAll() {
61         Iterable<Users> usersIterable = this.usersRepositoryCrudRepository.findAll();
62         Iterator<Users> iterator = usersIterable.iterator();
63         while (iterator.hasNext()) {
64             Users user = iterator.next();
65             System.out.println("Users: " + user);
66         }
67     }
68 
69     /**
70      * 删除测试
71      */
72     @Test
73     public void testDelete() {
74         // 默认开启了事务
75         this.usersRepositoryCrudRepository.deleteById(4);
76     }
77 
78 }

 

2.3)、PagingAndSortingRepository<T, ID>接口。该接口提供了分页和排序的操作,该接口继承了CrudRepository,则该接口可以复用CrudRepository接口的功能。

 1 package com.bie.springboot.dao;
 2 
 3 import com.bie.springboot.po.Users;
 4 import org.springframework.data.repository.PagingAndSortingRepository;
 5 
 6 /**
 7  * PagingAndSortingRepository该接口提供了分页和排序的操作。
 8  */
 9 public interface UsersRepositoryPagingAndSortingRepository extends PagingAndSortingRepository<Users, Integer> {
10 
11 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersRepositoryPagingAndSortingRepository;
 4 import com.bie.springboot.po.Users;
 5 import org.junit.jupiter.api.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.data.domain.Page;
10 import org.springframework.data.domain.PageRequest;
11 import org.springframework.data.domain.Pageable;
12 import org.springframework.data.domain.Sort;
13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 
15 import java.util.Iterator;
16 
17 @RunWith(SpringJUnit4ClassRunner.class)
18 @SpringBootTest(classes = SpringbootJpaApplication.class)
19         // 启动器,将测试类和启动器整合在一起
20 class SpringbootJpaApplicationTests {
21 
22     @Autowired
23     private UsersRepositoryPagingAndSortingRepository usersRepositoryPagingAndSortingRepository;
24 
25     @Test
26     public void testPagingAndSortingRepositorySort() {
27         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
28         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
29         // Sort对象封装了排序的规则。
30         Sort sort = Sort.by(order);
31         Iterable<Users> iterable = this.usersRepositoryPagingAndSortingRepository.findAll(sort);
32         Iterator<Users> iterator = iterable.iterator();
33         while (iterator.hasNext()) {
34             Users users = iterator.next();
35             System.out.println(users);
36         }
37     }
38 
39     @Test
40     public void testPagingAndSortingRepositoryPaging() {
41         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
42         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
43         // Sort对象封装了排序的规则。
44         Sort sort = Sort.by(order);
45 
46         // Pageable封装了分页的参数,当前页、每页显示的条数。
47         // 注意,Pageable当前页的是从0开始的。
48         // PageRequest类继承了Pageable接口。
49         int page = 0;// 当前页
50         int size = 2;// 每页显示的条数
51         Pageable pageable = PageRequest.of(page, size, sort);
52         Page<Users> usersPage = this.usersRepositoryPagingAndSortingRepository.findAll(pageable);
53         Iterator<Users> iterator = usersPage.iterator();
54         while (iterator.hasNext()) {
55             Users users = iterator.next();
56             System.out.println(users);
57         }
58         System.out.println("总条数:" + usersPage.getTotalElements());
59         System.out.println("总页数:" + usersPage.getTotalPages());
60     }
61 
62 
63 }

 

2.4)、JpaRepository<T, ID>接口。JpaRepository该接口继承了PagingAndSortingRepository接口,JpaRepository该接口是开发过程中经常使用的接口,主要的特点提供了对继承父接口中的方法的返回值进行了适配。

1 package com.bie.springboot.dao;
2 
3 import com.bie.springboot.po.Users;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 
6 public interface UsersRepository extends JpaRepository<Users, Integer> {
7 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersRepository;
 4 import com.bie.springboot.po.Users;
 5 import org.junit.jupiter.api.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.data.domain.Page;
10 import org.springframework.data.domain.PageRequest;
11 import org.springframework.data.domain.Pageable;
12 import org.springframework.data.domain.Sort;
13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 
15 import java.util.Iterator;
16 import java.util.List;
17 
18 @RunWith(SpringJUnit4ClassRunner.class)
19 @SpringBootTest(classes = SpringbootJpaApplication.class)
20         // 启动器,将测试类和启动器整合在一起
21 class SpringbootJpaApplicationTests {
22 
23     @Autowired
24     private UsersRepository usersRepository;
25 
26     @Test
27     public void testJpaRepositorySort() {
28         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
29         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
30         // Sort对象封装了排序的规则。
31         Sort sort = Sort.by(order);
32         List<Users> usersList = this.usersRepository.findAll(sort);
33         // 不用做强制类型转换了,相对于JpaRepository的父接口。
34         Iterator<Users> iterator = usersList.iterator();
35         while (iterator.hasNext()) {
36             Users users = iterator.next();
37             System.out.println(users);
38         }
39     }
40 
41     @Test
42     public void testJpaRepositorySortPaging() {
43         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
44         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
45         // Sort对象封装了排序的规则。
46         Sort sort = Sort.by(order);
47 
48         // Pageable封装了分页的参数,当前页、每页显示的条数。
49         // 注意,Pageable当前页的是从0开始的。
50         // PageRequest类继承了Pageable接口。
51         int page = 0;// 当前页
52         int size = 2;// 每页显示的条数
53         Pageable pageable = PageRequest.of(page, size, sort);
54         Page<Users> usersPage = this.usersRepository.findAll(pageable);
55         Iterator<Users> iterator = usersPage.iterator();
56         while (iterator.hasNext()) {
57             Users users = iterator.next();
58             System.out.println(users);
59         }
60         System.out.println("总条数:" + usersPage.getTotalElements());
61         System.out.println("总页数:" + usersPage.getTotalPages());
62     }
63 
64 
65 }

 

2.5)、JpaSpecificationExecutor接口。JpaSpecificationExecutor接口,该接口主要提供了多条件查询的支持,并且可以在查询中添加分页和排序。

注意:JpaSpecificationExecutor该接口是单独存在的,不是从上面的接口中做的继承。需要配合上面的四个接口之一进行使用,不然报错。

 1 package com.bie.springboot.dao;
 2 
 3 import com.bie.springboot.po.Users;
 4 import org.springframework.data.jpa.repository.JpaRepository;
 5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 6 
 7 /**
 8  * 注意:JpaSpecificationExecutor该接口是单独存在的,不是从上面的接口中做的继承。
 9  * <p>
10  * <p>
11  * JpaSpecificationExecutor接口,该接口主要提供了多条件查询的支持,并且可以在查询中添加分页和排序。
12  */
13 public interface UsersJpaSpecificationExecutor extends JpaRepository<Users, Integer>, JpaSpecificationExecutor<Users> {
14 }

测试代码,如下所示:

  1 package com.bie.springboot;
  2 
  3 import com.bie.springboot.dao.UsersJpaSpecificationExecutor;
  4 import com.bie.springboot.dao.UsersRepository;
  5 import com.bie.springboot.po.Users;
  6 import org.junit.jupiter.api.Test;
  7 import org.junit.runner.RunWith;
  8 import org.springframework.beans.factory.annotation.Autowired;
  9 import org.springframework.boot.test.context.SpringBootTest;
 10 import org.springframework.data.domain.Sort;
 11 import org.springframework.data.jpa.domain.Specification;
 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 13 
 14 import javax.persistence.criteria.*;
 15 import java.util.ArrayList;
 16 import java.util.Iterator;
 17 import java.util.List;
 18 
 19 @RunWith(SpringJUnit4ClassRunner.class)
 20 @SpringBootTest(classes = SpringbootJpaApplication.class)
 21         // 启动器,将测试类和启动器整合在一起
 22 class SpringbootJpaApplicationTests {
 23 
 24     @Autowired
 25     private UsersRepository usersRepository;
 26 
 27     @Autowired
 28     private UsersJpaSpecificationExecutor usersJpaSpecificationExecutor;
 29 
 30     /**
 31      * 单条件查询。
 32      */
 33     @Test
 34     public void testJpaSpecificationExecutorOne() {
 35         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
 36         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
 37         // Sort对象封装了排序的规则。
 38         Sort sort = Sort.by(order);
 39 
 40         // Specification接口封装了查询信息的接口。
 41         // Specification<Users>用户封装查询条件的。
 42         Specification<Users> specification = new Specification<Users>() {
 43 
 44             /**
 45              * Predicate封装了单个的查询条件。
 46              * @param root 对查询对象的属性的封装。
 47              * @param criteriaQuery 封装了我们要执行的各个部分的信息,比如select、from、order by等等信息。
 48              * @param criteriaBuilder 理解为查询条件的构造器。帮助我们定义不同的查询条件的。
 49              * @return
 50              */
 51             @Override
 52             public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
 53                 // Root对象封装了所有对象的属性
 54                 Path<Object> name = root.get("name");
 55                 String selectName = "亚瑟";
 56 
 57                 // 参数一表示当前要查询的属性。
 58                 // 参数二表示查询条件的值。
 59                 Predicate predicate = criteriaBuilder.equal(name, selectName);
 60                 return predicate;
 61             }
 62         };
 63 
 64         List<Users> usersList = this.usersJpaSpecificationExecutor.findAll(specification, sort);
 65         // 不用做强制类型转换了,相对于JpaRepository的父接口。
 66         Iterator<Users> iterator = usersList.iterator();
 67         while (iterator.hasNext()) {
 68             Users users = iterator.next();
 69             System.out.println(users);
 70         }
 71     }
 72 
 73 
 74     /**
 75      * 单条件查询。
 76      */
 77     @Test
 78     public void testJpaSpecificationExecutorMore() {
 79         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
 80         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
 81         // Sort对象封装了排序的规则。
 82         Sort sort = Sort.by(order);
 83 
 84         // Specification接口封装了查询信息的接口。
 85         // Specification<Users>用户封装查询条件的。
 86         Specification<Users> specification = new Specification<Users>() {
 87 
 88             /**
 89              * Predicate封装了单个的查询条件。
 90              * @param root 对查询对象的属性的封装。
 91              * @param criteriaQuery 封装了我们要执行的各个部分的信息,比如select、from、order by等等信息。
 92              * @param criteriaBuilder 理解为查询条件的构造器。帮助我们定义不同的查询条件的。
 93              * @return
 94              */
 95             @Override
 96             public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
 97                 // 多条件查询进行拼接操作。
 98                 List<Predicate> list = new ArrayList<>();
 99                 list.add(criteriaBuilder.equal(root.get("name"), "亚瑟"));
100                 list.add(criteriaBuilder.equal(root.get("age"), 25));
101                 Predicate[] predicates = new Predicate[list.size()];
102                 Predicate predicate = criteriaBuilder.and(list.toArray(predicates));
103                 return predicate;
104             }
105         };
106 
107         List<Users> usersList = this.usersJpaSpecificationExecutor.findAll(specification, sort);
108         // 不用做强制类型转换了,相对于JpaRepository的父接口。
109         Iterator<Users> iterator = usersList.iterator();
110         while (iterator.hasNext()) {
111             Users users = iterator.next();
112             System.out.println(users);
113         }
114     }
115 
116     /**
117      * 多条件查询的另外一种形式。
118      */
119     @Test
120     public void testJpaSpecificationExecutorThree() {
121         // Order定义了排序规则。参数一是根据倒叙还是正序,参数二是根据那个字段进行排序。
122         Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
123         // Sort对象封装了排序的规则。
124         Sort sort = Sort.by(order);
125 
126         // Specification接口封装了查询信息的接口。
127         // Specification<Users>用户封装查询条件的。
128         Specification<Users> specification = new Specification<Users>() {
129 
130             /**
131              * Predicate封装了单个的查询条件。
132              * @param root 对查询对象的属性的封装。
133              * @param criteriaQuery 封装了我们要执行的各个部分的信息,比如select、from、order by等等信息。
134              * @param criteriaBuilder 理解为查询条件的构造器。帮助我们定义不同的查询条件的。
135              * @return
136              */
137             @Override
138             public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
139                 // 多条件查询进行拼接操作。
140                 return criteriaBuilder.or(criteriaBuilder.and(criteriaBuilder.equal(root.get("name"), "亚瑟"), criteriaBuilder.equal(root.get("age"), 20)), criteriaBuilder.equal(root.get("id"), 2));
141                 // return criteriaBuilder.and(criteriaBuilder.equal(root.get("name"), "亚瑟"), criteriaBuilder.equal(root.get("age"), 25));
142             }
143         };
144 
145         List<Users> usersList = this.usersJpaSpecificationExecutor.findAll(specification, sort);
146         // 不用做强制类型转换了,相对于JpaRepository的父接口。
147         Iterator<Users> iterator = usersList.iterator();
148         while (iterator.hasNext()) {
149             Users users = iterator.next();
150             System.out.println(users);
151         }
152     }
153 
154 
155 }

 

3、SpringBoot整合Spring Data JPA建立关系映射。

3.1、SpringBoot整合Spring Data JPA-建立双向一对多关联映射。需求说明,角色与用户的一对多的关联关系,角色是一方,用户是多方,即一个角色可以有多个用户。

 1 package com.bie.springboot.po;
 2 
 3 import javax.persistence.*;
 4 
 5 @Entity // 表示该类是实体类
 6 @Table(name = "tb_users") // 表示该实体类和数据表进行映射,name表示实体类和数据表进行映射
 7 // 如果使用的是正向工程的话,name属性的值表示的是数据表的表名称。
 8 public class Users {
 9 
10     @Id // 表示该字段是主键
11     @GeneratedValue(strategy = GenerationType.IDENTITY) // 主键的生成策略
12     @Column(name = "id") // 表示实体类的字段和数据表的字段进行映射的关系,如果是正向工程的话,name的值就是数据表的字段名称
13     private Integer id;// 用户编号
14 
15     @Column(name = "name")
16     private String name;// 用户姓名
17 
18     @Column(name = "age")
19     private Integer age;// 用户年龄
20 
21     @Column(name = "address")
22     private String address;// 用户地址
23 
24     // 用户是多方,角色是一方,所以一个用户只能分配一个角色
25     @ManyToOne // 用户对角色,多对一关系
26     @JoinColumn(name = "roles_id") // 该注解的作用就是为了维护一个外键,外键在Users这一侧
27     // 可以通过正向工程在数据表新增一个字段。
28     private Roles roles;
29 
30 
31     // alt + insert来生成构造器、setter\getter等等方法
32     public Integer getId() {
33         return id;
34     }
35 
36     public void setId(Integer id) {
37         this.id = id;
38     }
39 
40     public String getName() {
41         return name;
42     }
43 
44     public void setName(String name) {
45         this.name = name;
46     }
47 
48     public Integer getAge() {
49         return age;
50     }
51 
52     public void setAge(Integer age) {
53         this.age = age;
54     }
55 
56     public String getAddress() {
57         return address;
58     }
59 
60     public void setAddress(String address) {
61         this.address = address;
62     }
63 
64     @Override
65     public String toString() {
66         return "Users{" +
67                 "id=" + id +
68                 ", name='" + name + '\'' +
69                 ", age=" + age +
70                 ", address='" + address + '\'' +
71                 '}';
72     }
73 
74     public Users(String name, Integer age, String address) {
75         this.name = name;
76         this.age = age;
77         this.address = address;
78     }
79 
80 
81     public Users() {
82     }
83 
84     public Roles getRoles() {
85         return roles;
86     }
87 
88     public void setRoles(Roles roles) {
89         this.roles = roles;
90     }
91 
92 }

角色信息表实体类,如下所示:

 1 package com.bie.springboot.po;
 2 
 3 import javax.persistence.*;
 4 import java.util.HashSet;
 5 import java.util.Set;
 6 
 7 @Entity
 8 @Table(name = "tb_roles")
 9 public class Roles {
10 
11     @Id
12     @GeneratedValue(strategy = GenerationType.IDENTITY)
13     @Column(name = "roleId")
14     private Integer roleId;// 角色编号
15 
16     @Column(name = "roleName")
17     private String roleName;// 角色名称
18 
19     // 角色是多的方式,一个角色可以分配给多个用户
20     @OneToMany(mappedBy = "roles") //表示一对多的关系,mappedBy表示向Set集合放Users,放的是当前roles相同的,外键和主键相同的
21     // 表示外键和主键相同的,做在角色表里面角色和那个用户之间的描述。
22     private Set<Users> usersSet = new HashSet<Users>();
23 
24     public Integer getRoleId() {
25         return roleId;
26     }
27 
28     public void setRoleId(Integer roleId) {
29         this.roleId = roleId;
30     }
31 
32     public String getRoleName() {
33         return roleName;
34     }
35 
36     public void setRoleName(String roleName) {
37         this.roleName = roleName;
38     }
39 
40     @Override
41     public String toString() {
42         return "Roles{" +
43                 "roleId=" + roleId +
44                 ", roleName='" + roleName + '\'' +
45                 '}';
46     }
47 
48     public Roles(String roleName) {
49         this.roleName = roleName;
50     }
51 
52     public Roles() {
53     }
54 
55     public Set<Users> getUsersSet() {
56         return usersSet;
57     }
58 
59     public void setUsersSet(Set<Users> usersSet) {
60         this.usersSet = usersSet;
61     }
62 
63 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.UsersRepository;
 4 import com.bie.springboot.po.Roles;
 5 import com.bie.springboot.po.Users;
 6 import org.junit.jupiter.api.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.data.domain.Example;
11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12 
13 import java.util.Optional;
14 
15 @RunWith(SpringJUnit4ClassRunner.class)
16 @SpringBootTest(classes = SpringbootJpaApplication.class)
17         // 启动器,将测试类和启动器整合在一起
18 class SpringbootJpaApplicationTests {
19 
20     @Autowired
21     private UsersRepository usersRepository;
22 
23     /**
24      * 一对多关联关系的添加
25      */
26     @Test
27     public void testSave() {
28         // 创建一个用户
29         Users users = new Users("admin", 22, "北京市西城区");
30 
31         // 创建一个角色
32         Roles roles = new Roles("管理员");
33 
34         // 关联,建立双向关系
35         roles.getUsersSet().add(users);
36         users.setRoles(roles);
37 
38         // 新增,插入用户的时候进行级联操作。
39         this.usersRepository.save(users);
40     }
41 
42     /**
43      * 一对多关联关系的查询
44      */
45     @Test
46     public void testSelect() {
47         Users users = new Users();
48         users.setId(1);
49         Example<Users> example = Example.of(users);
50         Optional<Users> usersOptional = this.usersRepository.findOne(example);
51         Users users1 = usersOptional.orElse(null);
52         System.out.println(users1.toString());
53         System.out.println(users1.getRoles().toString());
54     }
55 
56 }

 

3.2、SpringBoot整合Spring Data JPA-建立双向多对多关联映射。多对多的关联关系,需求,角色与菜单多对多的关联关系,角色是多方,菜单也是多方。

 1 package com.bie.springboot.po;
 2 
 3 import javax.persistence.*;
 4 import java.util.HashSet;
 5 import java.util.Set;
 6 
 7 @Entity
 8 @Table(name = "tb_menus")
 9 public class Menus {
10 
11     @Id
12     @GeneratedValue(strategy = GenerationType.IDENTITY)
13     @Column(name = "menuId")
14     private Integer menuId;// 菜单编号
15 
16     @Column(name = "menuName")
17     private String menuName;// 菜单名称
18 
19     @Column(name = "menuUrl")
20     private String menuUrl;// 菜单名称
21 
22     @Column(name = "menuParentId")
23     private Integer menuParentId;// 菜单名称
24 
25     // mappedBy的属性值必须是Roles属性字段private Set<Menus> menusSet = new HashSet<Menus>();的值名称
26     @ManyToMany(mappedBy = "menusSet") // 多对多的关联关系,表示菜单和角色是多对多的关系
27     private Set<Roles> rolesSet = new HashSet<Roles>();
28 
29     public Integer getMenuId() {
30         return menuId;
31     }
32 
33     public void setMenuId(Integer menuId) {
34         this.menuId = menuId;
35     }
36 
37     public String getMenuName() {
38         return menuName;
39     }
40 
41     public void setMenuName(String menuName) {
42         this.menuName = menuName;
43     }
44 
45     public String getMenuUrl() {
46         return menuUrl;
47     }
48 
49     public void setMenuUrl(String menuUrl) {
50         this.menuUrl = menuUrl;
51     }
52 
53     public Integer getMenuParentId() {
54         return menuParentId;
55     }
56 
57     public void setMenuParentId(Integer menuParentId) {
58         this.menuParentId = menuParentId;
59     }
60 
61     public Menus(String menuName, String menuUrl, Integer menuParentId) {
62         this.menuName = menuName;
63         this.menuUrl = menuUrl;
64         this.menuParentId = menuParentId;
65     }
66 
67     public Menus() {
68     }
69 
70     @Override
71     public String toString() {
72         return "Menus{" +
73                 "menuId=" + menuId +
74                 ", menuName='" + menuName + '\'' +
75                 ", menuUrl='" + menuUrl + '\'' +
76                 ", menuParentId=" + menuParentId +
77                 '}';
78     }
79 
80     public Set<Roles> getRolesSet() {
81         return rolesSet;
82     }
83 
84     public void setRolesSet(Set<Roles> rolesSet) {
85         this.rolesSet = rolesSet;
86     }
87 }

角色信息实体类,如下所示:

 1 package com.bie.springboot.po;
 2 
 3 import javax.persistence.*;
 4 import java.util.HashSet;
 5 import java.util.Set;
 6 
 7 @Entity
 8 @Table(name = "tb_roles")
 9 public class Roles {
10 
11     @Id
12     @GeneratedValue(strategy = GenerationType.IDENTITY)
13     @Column(name = "roleId")
14     private Integer roleId;// 角色编号
15 
16     @Column(name = "roleName")
17     private String roleName;// 角色名称
18 
19     // 角色是多的方式,一个角色可以分配给多个用户
20     // mappedBy的属性值必须是Users属性字段private Roles roles;的值名称
21     @OneToMany(mappedBy = "roles") //表示一对多的关系,mappedBy表示向Set集合放Users,放的是当前roles相同的,外键和主键相同的
22     // 表示外键和主键相同的,做在角色表里面角色和那个用户之间的描述。
23     private Set<Users> usersSet = new HashSet<Users>();
24 
25     // 立即加载fetch = FetchType.EAGER
26     @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) // 多对多的关联关系,表示菜单和角色是多对多的关系
27     // joinColumns当前表中的主键所关联的中间中的外键字段.
28     // inverseJoinColumns那一侧的中间的外键的名称叫什么
29     @JoinTable(name = "tb_roles_menus", joinColumns = @JoinColumn(name = "roleId"), inverseJoinColumns = @JoinColumn(name = "menuId"))
30     // 该注解表示映射中间表之间的信息,多对多需要一个中间表
31     // name表示数据表的名称,joinColumns表示当前表中需要拿主键和中间表的那个外键做关联。
32     // 中间表有两个外键,一个中间表关联一个外键,其实就是两个一对多的关系.
33     private Set<Menus> menusSet = new HashSet<Menus>();
34 
35     public Integer getRoleId() {
36         return roleId;
37     }
38 
39     public void setRoleId(Integer roleId) {
40         this.roleId = roleId;
41     }
42 
43     public String getRoleName() {
44         return roleName;
45     }
46 
47     public void setRoleName(String roleName) {
48         this.roleName = roleName;
49     }
50 
51     @Override
52     public String toString() {
53         return "Roles{" +
54                 "roleId=" + roleId +
55                 ", roleName='" + roleName + '\'' +
56                 '}';
57     }
58 
59     public Roles(String roleName) {
60         this.roleName = roleName;
61     }
62 
63     public Roles() {
64     }
65 
66     public Set<Users> getUsersSet() {
67         return usersSet;
68     }
69 
70     public void setUsersSet(Set<Users> usersSet) {
71         this.usersSet = usersSet;
72     }
73 
74     public Set<Menus> getMenusSet() {
75         return menusSet;
76     }
77 
78     public void setMenusSet(Set<Menus> menusSet) {
79         this.menusSet = menusSet;
80     }
81 }

测试代码,如下所示:

 1 package com.bie.springboot;
 2 
 3 import com.bie.springboot.dao.RolesRepository;
 4 import com.bie.springboot.po.Menus;
 5 import com.bie.springboot.po.Roles;
 6 import org.junit.jupiter.api.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.data.domain.Example;
11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12 
13 import java.util.Optional;
14 import java.util.Set;
15 
16 @RunWith(SpringJUnit4ClassRunner.class)
17 @SpringBootTest(classes = SpringbootJpaApplication.class)
18         // 启动器,将测试类和启动器整合在一起
19 class SpringbootJpaApplicationTests {
20 
21     @Autowired
22     private RolesRepository rolesRepository;
23 
24     /**
25      * 多对多关联关系的添加
26      */
27     @Test
28     public void testSave() {
29         // 创建一个菜单对象
30         Menus menus = new Menus("菜单一", "菜单一", 0);
31         Menus menus2 = new Menus("菜单二", "菜单二", 2);
32 
33         // 创建一个角色
34         Roles roles = new Roles("项目经理");
35 
36         // 关联,建立双向关系
37         roles.getMenusSet().add(menus);
38         roles.getMenusSet().add(menus2);
39         menus.getRolesSet().add(roles);
40         menus2.getRolesSet().add(roles);
41 
42         // 新增,插入角色的时候进行级联操作。
43         this.rolesRepository.save(roles);
44     }
45 
46     /**
47      * 一对多关联关系的查询
48      */
49     @Test
50     public void testSelect() {
51         Roles roles = new Roles();
52         roles.setRoleId(1);
53         Example<Roles> example = Example.of(roles);
54         Optional<Roles> optionalRoles = this.rolesRepository.findOne(example);
55         Roles roles1 = optionalRoles.orElse(null);
56         System.out.println(roles1.getRoleName());
57         Set<Menus> menus = roles.getMenusSet();
58         for (Menus menus2 : menus) {
59             System.out.println(menus2);
60         }
61     }
62 
63 }

 

posted on 2020-05-20 23:23  别先生  阅读(1533)  评论(0编辑  收藏  举报