乐趣区

Spring-Data-JPA-使用探究

这里就滤过 Spring Data JPA 我的项目部署,不分明的能够看一下开源我的项目 AutoAdmin。

Entity(表实体类)

一个表对应一个实体类,这里以 AutoAdmin 的 sys_user 表为例:

import cn.songhaiqing.autoadmin.base.BaseEntity;
import org.hibernate.annotations.Where;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;


@Entity
@Table(name = "sys_user")
@Where(clause = "deleted=0")
public class SysUser extends BaseEntity {
    // 账号
    @Column(name = "account")
    private String account;
    // 明码
    @Column(name = "password")
    private String password;
    // 姓名
    @Column(name = "name")
    private String name;
    // 明码加密盐
    @Column(name = "salt")
    private String salt;

    public String getAccount() {return account;}

    public void setAccount(String account) {this.account = account;}

    public String getPassword() {return password;}

    public void setPassword(String password) {this.password = password;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public String getSalt() {return salt;}

    public void setSalt(String salt) {this.salt = salt;}
}

备注

  • @Table 注解:写表名,如果表是刚好应用驼峰命名的话能够不写,倡议写上。
  • @Where:查问该表时会主动加个的条件,用来做软删除过滤是十分好的抉择,然而在 @ManyToOne get 该实体时如果数据已删除就会有问题,留神区别应用。
  • @Column 字段申明,能够写在不同的地位,但集体习惯写在字段上一行。属性名能够和数据库表字段名不一样。

BaseEntity
实体类继承了 BaseEntity,该类写一些通用的字段,像 ID、创立工夫、更新工夫、删除标注等。

import javax.persistence.*;
import java.util.Date;


@MappedSuperclass
public class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "create_time", nullable = false,updatable = false)
    private Date createTime = new Date();

    @Column(name = "update_time", nullable = false)
    private Date updateTime = new Date();

    @Column(name = "deleted", nullable = false)
    private boolean deleted = false;

    public Date getCreateTime() {return createTime;}

    public Date getUpdateTime() {return updateTime;}

    public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}

    public Long getId() {return id;}

    public void setId(Long id) {this.id = id;}

    public void setCreateTime(Date createTime) {this.createTime = createTime;}

    public boolean isDeleted() {return deleted;}

    public void setDeleted(boolean deleted) {this.deleted = deleted;}
}

备注
* @Id @GeneratedValue(strategy = GenerationType.AUTO) ID 自增
* 默认值:间接给变量设置初始值就能够了,create_time(创立工夫)有点不一样,因为我不想当前扭转值,因为须要设置 updatable = false。

Repository(表操作接口)

有人习惯叫 DAO 或其余,JPA 很重要的中央,应用 JPA 的语法简略间接的代替了大部分 SQL。

示例

import cn.songhaiqing.autoadmin.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface SysUserRepository extends JpaRepository<SysUser, Long>,JpaSpecificationExecutor<SysUser> {SysUser findByAccount(String account);

    long countByAccount(String account);

    long countByAccountAndIdNot(String account, Long id);
}

查问

依据单查问(一条后果)

SysUser findByAccount(String account);

须要确保返回的后果最多只有一条,否则会报错

依据单查问(多条后果)

List<SysUser> findByAccount(String account);

多条件查问

SysUser findByAccountAndPassword(String account, String password);

查问第一条

SysUser findFirstByAccount(String account);

查问前 10 条

SysUser findTop10ByAccount(String account);

排序

SysUser findTop10ByAccount(String account);

JPA 语法

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 … where x.firstname = ?1
findByFirstnameIs
findByFirstnameEquals
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(parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1(parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1(parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection ages) … 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)

分页简单查问

示例

List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(Sort.Direction.DESC,"id"));
Sort sort = new Sort(orders);
Page<SysUser> page = sysUserRepository.findAll(new Specification<SysUser>(){
    @Override
    public Predicate toPredicate(Root<SysUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {List<Predicate> predicate = new ArrayList<>();
        // 此处增加过滤条件
        Predicate[] pre = new Predicate[predicate.size()];
        return criteriaQuery.where(predicate.toArray(pre)).getRestriction();}
},new PageRequest(query.getPage() - 1, query.getLimit(),sort));
BaseResponseList<SysUserViewModel> result = new BaseResponseList<>();
result.setCount(page.getTotalElements());
List<SysUserViewModel> models = new ArrayList<>();
for (SysUser sysUser : page.getContent()) {SysUserViewModel model = new SysUserViewModel();
    model.setId(sysUser.getId());
    model.setAccount(sysUser.getAccount());
    model.setName(sysUser.getName());
    models.add(model);
}
result.setData(models);
return result;

此例中联合了分页、排序、过滤性能。

参考

  • Spring Data JPA 官网文档
退出移动版