Spring Data Jpa 学习——偷懒一定要做到极致

项目中用到了Spring Data Jpa,确实比之前用Hibernate sessionFactory顺手很多,大部分的Sql语句都由Spring帮我们自动生成。
之前的应用局限于Spring Data Jpa的基础,比如Crud操作,分页查询、排序之类。正好今天有空,对其文档仔仔细细的看了一遍,发现还有很多之前遗漏的瑰宝。

SpEl Expression

官方文档中是这样的:

@Entity
public class User {

  @Id
  @GeneratedValue
  Long id;

  String lastname;
}

public interface UserRepository extends JpaRepository<User,Long> {

  @Query("select u from #{#entityName} u where u.lastname = ?1")
  List<User> findByLastname(String lastname);
}

用#{#entityName} 代替本来实体的名称,而Spring data jpa会自动根据User实体上对应的@Entity(name = “MyUser”)或者是默认的@Entity,来自动将实体名称填入hql语句中。

这帮助我解决了项目中很多dao接口的方法除了实体类名称不同,其他操作都相同的问题。

public interface AnchorGroupDao extends PagingAndSortingRepository<AnchorGroup, Integer>,JpaSpecificationExecutor<AnchorGroup>{

    @Query("update AnchorGroup rule set rule.isEnable=1,rule.lastEnable= NOW() where rule.id= :id")
    @Modifying
    public int enable(@Param("id")int id);

    @Query("update AnchorGroup rule set rule.isEnable=0,rule.lastDisable = NOW() where rule.id= :id ")
    @Modifying
    public int disable(@Param("id")int id);

    @Query("update AnchorGroup rule set rule.isEnable=0,rule.lastDisable = NOW(),rule.isHistory=1 where rule.id= :id ")
    @Modifying
    public int setHistory(@Param("id")int id);

}
public interface AnchorRentDao extends PagingAndSortingRepository<AnchorRent, Integer>,JpaSpecificationExecutor<AnchorRent>{

    @Query("update AnchorRent rule set rule.isEnable=1,rule.lastEnable= NOW() where rule.id= :id")
    @Modifying
    public int enable(@Param("id")int id);

    @Query("update AnchorRent rule set rule.isEnable=0,rule.lastDisable = NOW() where rule.id= :id ")
    @Modifying
    public int disable(@Param("id")int id);

    @Query("update AnchorRent rule set rule.isEnable=0,rule.lastDisable = NOW(),rule.isHistory=1 where rule.id= :id ")
    @Modifying
    public int setHistory(@Param("id")int id);
}

这时只要定义一个接口,然后AnchorRentDao、AnchorGroupDao都继承它既可。
BaseDao.java

@NoRepositoryBean
public interface BaseDao<T extends IdEntity> extends Repository<T, Integer>{

    T save(T t);

    void delete(Integer id);

    public  Iterable<T> save(Iterable<T> entities);

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

    T findOne(Integer id);

    void delete(T entity);

}

BaseRuleDao.java

@NoRepositoryBean
public interface BaseRuleDao<T extends IdEntity> extends BaseDao<T>{

    @Query("update #{#entityName} rule set rule.isEnable=1,rule.lastEnable= NOW() where rule.id= :id")
    @Modifying
    public int enable(@Param("id")int id);

    @Query("update #{#entityName} rule set rule.isEnable=0,rule.lastDisable = NOW() where rule.id= :id ")
    @Modifying
    public int disable(@Param("id")int id);

    @Query("update #{#entityName} rule set rule.isEnable=0,rule.lastDisable = NOW(),rule.isHistory=1 where rule.id= :id ")
    @Modifying
    public int setHistory(@Param("id")int id);

}

AnchorGroupDao.java

public interface AnchorGroupDao extends BaseRuleDao<AnchorGroup>{
}
@QueryHint

@QueryHint目前没有用于项目,也只是初步了解其的作用。
具体用法参考:http://www.csdn123.com/html/mycsdn20140110/fc/fc7d2c0bc92333ca53e6ea920bd24bef.html

Stored procedures @Procedure

@Procedure是用于调用存储过程.
引用官网文档中的描述:

/;
DROP procedure IF EXISTS plus1inout
/;
CREATE procedure plus1inout (IN arg int, OUT res int)
BEGIN ATOMIC
 set res = arg ` 1;
END
/;
@Entity
@NamedStoredProcedureQuery(name = "User.plus1", procedureName = "plus1inout", parameters = {
  @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
  @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) })
public class User {}
@Procedure("plus1inout")
Integer explicitlyNamedPlus1inout(Integer arg);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值