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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM