SpringBoot 整合 SpringDataJpa进行复杂查询
一、新建Springboot工程,并导入相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.cao</groupId>
<artifactId>JPA_Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 导入父依赖-->
<parent>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<!--链接数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!--整合SpringDataJpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<!--使用lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
</project>
二、配置文件
在创建一个SpringBoot项目时,会在resources目录下自动创建一个application.propreties,但是因为yml文件是以数据为中心,且可读性高,故新建一个yml文件来存储配置。
application.yml
数据源配置
#Datasource
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/company?userUnicode=true&characterEncoding=utf8&userSSl=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
username: root
password: 123456
jpa: #JPA
#
database: mysql
show-sql: true
hibernate:
ddl-auto: update
naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
三、entity及dao
Conpanny.java
@Entity //标注为实体类
@Table(name = "co_company") //指定表名,
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Company implements Serializable {
private static final long serialVersionUID = 594829320797158219L;
//ID
@Id
private String id;
/**
* 公司名称
*/
private String name;
/**
* 企业登录账号ID
*/
private String managerId;
/**
* 当前版本
*/
private String version;
/**
* 续期时间
*/
private Date renewalDate;
/**
* 到期时间
*/
private Date expirationDate;
/**
* 公司地区
*/
private String companyAre