springdata jpa入门

本文讲述的内容是是在springboot 2.1.3.RELEASE和springdata 2.1.5.RELEASE的基础上进行的,开发工具为Spring Tool Suite,数据库使用的是mysql。
1、创建一个springboot工程,添加jpa等依赖


springdata jpa入门_第1张图片
image.png

2、配置数据源及JPA配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jpa?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 1234
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true

说明:
spring.jpa.hibernate.ddl-auto可选参数 :
create 启动时删数据库中的表,然后创建,退出时不删除数据表 ;
create-drop 启动时删数据库中的表,然后创建,退出时删除数据表 如果表不存在报错 ;
update 如果启动时表格式不一致则更新表,原有数据保留 ;
validate 项目启动表结构进行校验 如果不一致则报错;
建议使用update

3、创建实体类

@Entity
@Table(name="t_school")
public class School implements Serializable{

    private static final long serialVersionUID = 3624350753446288205L;
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer schoolId;
    
    private String schoolNo;
    
    private String schoolName;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date createDate;

    public Integer getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }

    public String getSchoolNo() {
        return schoolNo;
    }

    public void setSchoolNo(String schoolNo) {
        this.schoolNo = schoolNo;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Override
    public String toString() {
        return "School [schoolId=" + schoolId + ", schoolNo=" + schoolNo + ", schoolName=" + schoolName + ", createDate=" + createDate + "]";
    }


}

@Entity表明为实体,@Table(name="t_class")指定数据库表名,@Id用于指定主键,
@GeneratedValue指定主键生成策略,@Temporal用于指定日期类型
此处使用自动创建表

@RunWith(SpringRunner.class)
@SpringBootTest
public class Jpa3ApplicationTests {

    @Test
    public void contextLoads() {
    }

}

执行SpringBoot Junit测试会自动创建表。
4、创建DAO

public interface SchoolRepository extends JpaRepository{

}

JpaRepository为spring data封装的类,其中包含了很多增删改查的方法,实际开发过程中继承这个类即可。
5、测试
新建:

    @Autowired
    private SchoolRepository schoolRepository;
    
    
    @Test
    public void insert() {
        School school = new School();
        school.setSchoolNo("S001");
        school.setSchoolName("第一中学");
        school.setCreateDate(new Date());
        schoolRepository.save(school);
    }

执行结果:


springdata jpa入门_第2张图片
image.png

查询(jpa的查询方式比较多,此处只做简单演示)

    @Test
    public void select() {
        School school = schoolRepository.findById(1).get();
        System.out.println(school);
    }

执行结果:


springdata jpa入门_第3张图片
image.png

更新

    @Test
    public void testUpdate() {
        School school = schoolRepository.findById(1).get();
        school.setSchoolName("第一中学=====");
        schoolRepository.save(school);
        System.out.println(school);
    }

执行结果:


springdata jpa入门_第4张图片
image.png

删除

    @Test
    public void testRemove() {
        schoolRepository.deleteById(1);
    }

执行结果:


springdata jpa入门_第5张图片
image.png

如果要输出sql参数的话,增加如下日志配置:

logging:
  level:
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE

你可能感兴趣的:(springdata jpa入门)