Skip to content

Spring Boot(十) Spring Boot整合JPA

一、Spring Data JPA概述

SpringData:Spring 的一个子项目。用于简化数据库访问,支持NoSQL 和 关系数据存储。其主要目标是使数据库的访问变得方便快捷。

JPA:JPA(Java Persistence API,Java持久化API),定义了对象关系映射(Object Relation Mapping,ORM)以及实体对象持久化的标准接口。Hibernate实现了JPA的一个ORM框架。

JPA Spring Data:致力于减少数据访问层 (DAO) 的开发量,开发者唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来完成。Spring Data JPA 是Spring基于ORM框架、JPA规范的基础上封装的一套JPA应用框架。

二、整合jpa步骤

1.添加依赖文件

xml
		<!--jpa依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

2.jpa配置

yaml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  # 自动创建表结构的设置
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: create-drop
# ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空
# ddl-auto:create-drop----每次程序结束的时候会清空表
# ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
# ddl-auto:validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错

3.实体类

java
@Entity
public class User implements Serializable {
    public User() {
    }
    public User(Long id,String name, String password, String mail, String date) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.mail = mail;
        this.date = date;
    }
    // ID,用户名,密码,邮箱,注册时间
    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String mail;
    @Column(nullable = false)
    private String date;
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", mail='" + mail + '\'' +
                ", date='" + date + '\'' +
                '}';
    }
}

4.dao层

java
public interface UserDao extends JpaRepository<User, Long> {

}

5.Junit测试

java
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
    @Autowired
    private UserDao userDao;


    @Test
    public void testInsertUser() {
        User user = new User(1L,"w3cjava", "123456", "w3cjava@w3cjava.com", "2017-01-01");
        userDao.save(user);
    }
    @Test
    public void testUpdateUser() {
        User user = new User(1L, "w3cjava1", "1234561", "1w3cjava@w3cjava.com", "2017-01-01");
        userDao.save(user);
    }

    @Test
    public void testFindAll() {
        System.out.println(userDao.findAll());
    }
    @Test
    public void testDelete() {
        userDao.deleteById(1L);
    }

    @Test
    public void testCURD() {
        testInsertUser();
        testUpdateUser();
        testFindAll();
        testDelete();
    }

}

运行Junit测试

java
    @Test
    public void testCURD() {
        testInsertUser();
        testUpdateUser();
        testFindAll();
        testDelete();
    }

image.png

三、小结

整合jpa做简单的增删改查比较简单,这里只是为了快速的运用,不做复杂查询,如果需要做复杂查询或者多数据源等操作,可以通过搜索引擎解决。

四、源码地址

10-spring-boot-jpa