且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SpringData JPA就是这么简单(上)

更新时间:2022-10-14 10:12:12

一、SpringData入门


在上次学SpringBoot的时候,那时候的教程就已经涉及到了一点SpringData JPA的知识了。当时还是第一次见,觉得也没什么大不了,就是封装了Hibernate的API而已。

然后在慕课网上又看到了SpringData的教程了。于是就进去学习了一番。

教程地址:https://www.imooc.com/learn/821

源码下载地址:https://img.mukewang.com/down/58e60b910001594b00000000.zip

在教程中是以原始JDBC和Spring JDBC Template来进行引入SpringData的。

由于原始的JDBC和Spring JDBC Template需要书写的代码量还是比较多的,于是我们就有了SpringData这么一个框架了。


1.1SpringDataJPA入门

SpringData JPA只是SpringData中的一个子模块

JPA是一套标准接口,而Hibernate是JPA的实现

SpringData JPA 底层默认实现是使用Hibernate

SpringDataJPA 的首个接口就是Repository,它是一个标记接口。只要我们的接口实现这个接口,那么我们就相当于在使用SpringDataJPA了。

只要我们实现了这个接口,我们就可以使用"按照方法命名规则"来进行查询。我第一次见到他的时候觉得他贼神奇。

SpringData JPA就是这么简单(上)


1.2项目配置

  1. 在pom.xml中添加相关依赖
  2. 在yml或者properties文件种配置对应的属性
  3. 创建实体和Repository测试

参考资源:

例子:

比如:定义下面这么一个方法,就可以在外界使用了。

Employee findByName(String name);

也就是说,上面的方法会被解析成SQL语句:select * from Employee where name = ?

是不是觉得很方便!!!!

如果是简单的操作的话,直接定义这么一个方法,就能够使用了。确确实实很好。

简直比Mytais不知道好到哪里去了。Mybatis还要去写映射文件,专门写一个sql语句。

同时,创建了实体就能够自动帮我们创建数据库表了,修改了实体字段也能够将数据表一起修改。顿时就觉得很好用了。

/**
 * 雇员:  先开发实体类===>自动生成数据表
 */
@Entity
public class Employee {
    private Integer id;
    private String name;
    private Integer age;
    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Column(length = 20)
    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;
    }
}

当然了,上面根据方法名来使用是有弊端的:

  • 1)方法名会比较长: 约定大于配置
  • 2)对于一些复杂的查询,是很难实现

比如:


// where name like ?% and age <?
    public List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age);
    // where name like %? and age <?
    public List<Employee> findByNameEndingWithAndAgeLessThan(String name, Integer age);
    // where name in (?,?....) or age <?
    public List<Employee> findByNameInOrAgeLessThan(List<String> names, Integer age);
    // where name in (?,?....) and age <?
    public List<Employee> findByNameInAndAgeLessThan(List<String> names, Integer age);


因此,对于这种情况下还是要写SQL语句简单得多。


@Query("select o from Employee o where id=(select max(id) from Employee t1)")
    public Employee getEmployeeByMaxId();
    @Query("select o from Employee o where o.name=?1 and o.age=?2")
    public List<Employee> queryParams1(String name, Integer age);
    @Query("select o from Employee o where o.name=:name and o.age=:age")
    public List<Employee> queryParams2(@Param("name")String name, @Param("age")Integer age);
    @Query("select o from Employee o where o.name like %?1%")
    public List<Employee> queryLike1(String name);
    @Query("select o from Employee o where o.name like %:name%")
    public List<Employee> queryLike2(@Param("name")String name);
    @Query(nativeQuery = true, value = "select count(1) from employee")
    public long getCount();


学过Hibernate的都知道上面的不是原生的SQL语句,是HQL/JPQL语句。不过他用起来还是比Mybatis简洁很多

对于修改数据,需要增加Modify注解、并且一定要在事务的管理下才能修改数据


@Modifying
    @Query("update Employee o set o.age = :age where o.id = :id")
    public void update(@Param("id")Integer id, @Param("age")Integer age);

1.3Repository子类接口

SpringData JPA就是这么简单(上)

CURDRepository接口的实现方法:

SpringData JPA就是这么简单(上)

排序、分页接口:


SpringData JPA就是这么简单(上)SpringData JPA就是这么简单(上)SpringData JPA就是这么简单(上)SpringData JPA就是这么简单(上)

增加过滤条件的接口:

SpringData JPA就是这么简单(上)SpringData JPA就是这么简单(上)