一、Spring Data JPA介绍
spring data:其实就是spring 提供的一个操作数据的框架。而spring data JPA 只是spring data 框架下的一个基于JPA标准操作数据的模块。
spring data jpa :基于JPA的标准对数据进行操作。简化操作持久层的代码,只需要编写接口就可以。
二、spring boot 整合spring data jpa
1、搭建整合环境
![](https://i-blog.csdnimg.cn/blog_migrate/fb06ee3f7fb7d62de2132d9e933a88db.png)
2、修改pom.xml文件添加所需依赖
<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>com.lxp</groupId>
<artifactId>framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<java.version>1.7</java.version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<!-- Web 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Junit测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 数据连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- spring data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>
3、在项目中添加application.properties文件,添加数据库配置以及JPA配置
# 数据库配置
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/framework
spring.datasource.username = root
spring.datasource.password = root
# SpringData JPA 配置
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql = true
4、添加实体类
@Entity
@Table(name = "sys_users")
public class SysUsers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
@Column(name = "addr")
private String addr;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
5、添加接口
public interface SysUsersRepository extends JpaRepository<SysUsers, Integer> {
/**
* JpaRepository<T,ID>
*
* T:当前需要映射的实体。 ID:当前映射实体中ID的类型
*/
}
6、创建启动类
@SpringBootApplication
public class AppStart {
public static void main(String[] args) {
SpringApplication.run(AppStart.class, args);
}
}
7、编写测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AppStart.class)
public class RepositoryTest {
@Autowired
private SysUsersRepository sysUsersRepository;
@Test
public void