Spring技术Spring源码分析

二十四、Spring Boot使用spring-data-jpa

2019-04-28  本文已影响2人  木石前盟Caychen

一、JPA

Spring Data JPA 是Spring Data 的一个子项目,它通过提供基于JPA的Repository极大了减少了操作JPA的代码。Spring Data JPA旨在通过减少实际需要的数量来显着提高数据访问层的实现。

在Spring环境中需要配置大量的XML配置,但是SpringBoot基本上帮助我们配置好了,我们只需要简单地配置一下DataSource和几项jpa额外的配置就完成了整个配置持久化层的工作。EntityManagerFactory 那些都不用配置了。

JPA不是一种框架,而只是一种ORM规范,而这种规范具体由其他厂商来实现,其中最熟悉的莫过于Hibernate了。

网络上对JPA的褒贬不一,特别是用JPA进行多表操作的时候,确实是比较繁琐。当然任何技术有好的方面也有坏的方面,本章重点不在于此。

二、添加依赖

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

三、数据源和JPA配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=admin

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

四、JPA查询语法

三、数据源和JPA配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=admin

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=admin

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

4.1、JPA内置方法

Repository JPA基层接口

public interface Repository<T, ID extends Serializable> {}

CrudRepository 专门用于crud 接口

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
    <S extends T> S save(S entity);

    <S extends T> Iterable<S> save(Iterable<S> entities);

    T findOne(ID id);

    boolean exists(ID id);

    Iterable<T> findAll();

    Iterable<T> findAll(Iterable<ID> ids);

    long count();

    void delete(ID id);

    void delete(T entity);

    void delete(Iterable<? extends T> entities);

    void deleteAll();
}

PagingAndSortingRepository 带有简单的分页和排序的接口

public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {

    Iterable<T> findAll(Sort sort);

    Page<T> findAll(Pageable pageable);
}

PagingAndSortingRepository内部有两个重要的类,分别是Sort类和Pageable接口,定义如下:

//排序类
public class Sort implements Iterable<org.springframework.data.domain.Sort.Order>, Serializable {

    //默认以升序排序
    public static final Direction DEFAULT_DIRECTION = Direction.ASC;

    private final List<Order> orders;

    public Sort(Order... orders) {
        this(Arrays.asList(orders));
    }
    
    public Sort(List<Order> orders) {
        if (null == orders || orders.isEmpty()) {
            throw new IllegalArgumentException("You have to provide at least one sort property to sort by!");
        }

        this.orders = orders;
    }

    public Sort(String... properties) {
        this(DEFAULT_DIRECTION, properties);
    }

    public Sort(Direction direction, String... properties) {
        this(direction, properties == null ? new ArrayList<String>() : Arrays.asList(properties));
    }

    public Sort(Direction direction, List<String> properties) {
        if (properties == null || properties.isEmpty()) {
            throw new IllegalArgumentException("You have to provide at least one property to sort by!");
        }

        this.orders = new ArrayList<Order>(properties.size());

        for (String property : properties) {
            this.orders.add(new Order(direction, property));
        }
    }

    public Sort and(Sort sort) {
        if (sort == null) {
            return this;
        }

        ArrayList<Order> these = new ArrayList<Order>(this.orders);

        for (Order order : sort) {
            these.add(order);
        }

        return new Sort(these);
    }

    public Order getOrderFor(String property) {
        for (Order order : this) {
            if (order.getProperty().equals(property)) {
                return order;
            }
        }

        return null;
    }

    //other...

    //内部定义了一个Direction的枚举类
    public static enum Direction {

        ASC, DESC;
        
        //other code...
    }

    //内部定义了Order类,用于字段排序
    public static class Order implements Serializable {

        private static final boolean DEFAULT_IGNORE_CASE = false;

        private final Direction direction;
        private final String property;
        private final boolean ignoreCase;
        private final NullHandling nullHandling;

        public Order(Direction direction, String property) {
            this(direction, property, DEFAULT_IGNORE_CASE, null);
        }

        public Order(Direction direction, String property, NullHandling nullHandlingHint) {
            this(direction, property, DEFAULT_IGNORE_CASE, nullHandlingHint);
        }

        public Order(String property) {
            this(DEFAULT_DIRECTION, property);
        }

        private Order(Direction direction, String property, boolean ignoreCase, NullHandling nullHandling) {

            if (!StringUtils.hasText(property)) {
                throw new IllegalArgumentException("Property must not null or empty!");
            }

            this.direction = direction == null ? DEFAULT_DIRECTION : direction;
            this.property = property;
            this.ignoreCase = ignoreCase;
            this.nullHandling = nullHandling == null ? NullHandling.NATIVE : nullHandling;
        }

        //other...
    }
}
//分页接口
public interface Pageable {

    int getPageNumber();

    int getPageSize();

    int getOffset();

    Sort getSort();

    Pageable next();

    Pageable previousOrFirst();

    Pageable first();

    boolean hasPrevious();
}
//分页抽象类
public abstract class AbstractPageRequest implements Pageable, Serializable {

    private final int page;
    private final int size;

    public AbstractPageRequest(int page, int size) {

        //other code...
        
        this.page = page;
        this.size = size;
    }

    public int getPageSize() {
        return size;
    }

    public int getPageNumber() {
        return page;
    }

    public int getOffset() {
        return page * size;
    }

    public boolean hasPrevious() {
        return page > 0;
    }

    public Pageable previousOrFirst() {
        return hasPrevious() ? previous() : first();
    }

    public abstract Pageable next();

    public abstract Pageable previous();

    public abstract Pageable first();

    //other code...
}
//分页实现类
public class PageRequest extends AbstractPageRequest {

    private final Sort sort;

    public PageRequest(int page, int size) {
        this(page, size, null);
    }

    public PageRequest(int page, int size, Direction direction, String... properties) {
        this(page, size, new Sort(direction, properties));
    }

    public PageRequest(int page, int size, Sort sort) {
        super(page, size);
        this.sort = sort;
    }

    public Sort getSort() {
        return sort;
    }

    public Pageable next() {
        return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
    }

    public PageRequest previous() {
        return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
    }

    public Pageable first() {
        return new PageRequest(0, getPageSize(), getSort());
    }

    //other code...
}

JpaRepository 带有crud接口和分页、排序的接口

public interface JpaRepository<T, ID extends Serializable>
        extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {

    List<T> findAll();

    List<T> findAll(Sort sort);

    List<T> findAll(Iterable<ID> ids);

    <S extends T> List<S> save(Iterable<S> entities);

    void flush();

    <S extends T> S saveAndFlush(S entity);

    void deleteInBatch(Iterable<T> entities);

    void deleteAllInBatch();

    T getOne(ID id);

    @Override
    <S extends T> List<S> findAll(Example<S> example);

    @Override
    <S extends T> List<S> findAll(Example<S> example, Sort sort);

}

JpaSpecificationExecutor 用于复杂的条件查询并分页、排序功能

public interface JpaSpecificationExecutor<T> {

    T findOne(Specification<T> spec);

    List<T> findAll(Specification<T> spec);

    Page<T> findAll(Specification<T> spec, Pageable pageable);

    List<T> findAll(Specification<T> spec, Sort sort);

    long count(Specification<T> spec);
}
public interface Specification<T> {

    Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb);
}
public class Specifications<T> implements Specification<T>, Serializable {

    private final Specification<T> spec;

    private Specifications(Specification<T> spec) {
        this.spec = spec;
    }

    public static <T> Specifications<T> where(Specification<T> spec) {
        return new Specifications<T>(spec);
    }

    public Specifications<T> and(Specification<T> other) {
        return new Specifications<T>(new ComposedSpecification<T>(spec, other, AND));
    }

    public Specifications<T> or(Specification<T> other) {
        return new Specifications<T>(new ComposedSpecification<T>(spec, other, OR));
    }

    public static <T> Specifications<T> not(Specification<T> spec) {
        return new Specifications<T>(new NegatedSpecification<T>(spec));
    }

    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        return spec == null ? null : spec.toPredicate(root, query, builder);
    }

    //other code...
}

以上一大堆JPA内置方法,可以直接使用,而无需再声明定义了。当然如果想重载或者自定义的话,可以重新声明定义下。

5.2、通过解析方法名创建查询

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,****findByFirstnameIs,****findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age <= ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,****NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1
EndingWith findByFirstnameEndingWith … where x.firstname like ?1
Containing findByFirstnameContaining … where x.firstname like ?1
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection<Age> ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection<Age> age) … where x.age not in ?1
True findByActiveTrue … where x.active = true
False findByActiveFalse … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

例如:

4.3、使用自定义的@Query注解进行查询

//使用自定义的@Query注解,实现sql语句
@Query("select count(*) from Person")
Long findAllCount();

@Query("from Person where id = ?1")
List<Person> selectWhereById(Integer id);

@Query("delete from Person where id = ?1")
@Modifying
void deleteById(Integer id);

@Query(value="select distinct id from person", nativeQuery = true)
List<Integer> getIds();

注意:

五、JPA常用注解

具体注解使用请参看我的GitHub-java-jpa或者码云-java-jpa

六、使用JPA操作数据库

附上代码:

实体类Person

@Entity
@Table
public class Person implements Serializable{

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    private int age;
    
    //getter和setter
}

控制器类PersonController

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private IPersonService personService;

    @RequestMapping("/find")
    public Person findPersonByName(String name){
        return personService.findPersonByName(name);
    }

    @PostMapping("/")
    public String insertIntoPerson(Person person){
        personService.insertIntoPerson(person);
        return "success";
    }

    @GetMapping("/age")
    public List<Person> getPersonAgeBetween(int min, int max){
        return personService.getPersonAgeBetween(min, max);
    }

    @GetMapping("/page")
    public Page<Person> getPersonsByPage(int pageNum, int pageSize){
        return personService.getPersonsByPage(pageNum, pageSize);
    }

    @GetMapping("/sort")
    public Page<Person> getPersonsBySortAge(){
        return personService.getPersonsBySortAge();
    }

    @GetMapping("/count")
    public Long getAllCount(){
        return personService.findAllCount();
    }

    @PostMapping("/update")
    public void updateOnePerson(Person person){
        personService.updateOnePerson(person.getAge(), person.getName(), person.getId());
    }

    @GetMapping("/ids")
    public List<Integer> getIds(){
        return personService.getIds();
    }
}

业务层接口IPersonService

public interface IPersonService {

    Person findPersonByName(String name);

    void insertIntoPerson(Person person);

    List<Person> getPersonAgeBetween(int min, int max);

    Page<Person> getPersonsByPage(int pageNum, int pageSize);

    Page<Person> getPersonsBySortAge();

    Long findAllCount();

    void updateOnePerson(int age, String name, int id);

    List<Integer> getIds();
}

业务层接口实现类PersonServiceImpl

@Service
public class PersonServiceImpl implements IPersonService {

    @Autowired
    private IPersonRepository personRepository;

    @Override
    public Person findPersonByName(String name) {
        return personRepository.findPersonByName(name);
    }

    @Override
    public void insertIntoPerson(Person person) {
        personRepository.save(person);
    }

    @Override
    public List<Person> getPersonAgeBetween(int min, int max) {
        return personRepository.findPersonByAgeBetween(min, max);
    }

    @Override
    public Page<Person> getPersonsByPage(int pageNum, int pageSize) {
        Pageable pageable = new PageRequest(pageNum, pageSize);
        return personRepository.findAll(pageable);
    }

    @Override
    public Page<Person> getPersonsBySortAge() {
        Sort sort = new Sort(new Sort.Order(Sort.Direction.ASC, "age"));
        Pageable pageable = new PageRequest(0,5, sort);
        return personRepository.findAll(pageable);
    }

    @Override
    public Long findAllCount() {
        return personRepository.findAllCount();
    }

    @Transactional
    @Override
    public void updateOnePerson(int age, String name, int id){
        personRepository.updateOnePerson(age, name, id);
    }

    @Override
    public List<Integer> getIds() {
        return personRepository.getIds();
    }
}

持久化接口IPersonRepository

public interface IPersonRepository extends JpaRepository<Person, Integer>{

    Person findPersonByName(String name);

    List<Person> findPersonByAgeBetween(int min, int max);

    //使用自定义的@Query注解,实现sql语句
    @Query("select count(*) from Person")
    Long findAllCount();

    //使用@Modifying注解表示该hql是个增删改的操作,需要事务管理
    @Query("update Person set age = ?1, name = ?2 where id = ?3")
    @Modifying
    void updateOnePerson(int age, String name, int id);

    //原生SQL
    @Query(value="select distinct id from person", nativeQuery = true)
    List<Integer> getIds();
}
上一篇 下一篇

猜你喜欢

热点阅读