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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章