項目中用到了Spring Data Jpa,確實比之前用Hibernate sessionFactory順手很多,大部分的Sql語句都由Spring幫我們自動產生。
之前的應用局限於Spring Data Jpa的基礎,比如Crud操作,分頁查詢、排序之類。正好今天有空,對其文檔仔仔細細的看了一遍,發現還有很多之前遺漏的瑰寶。 SpEl Expression
官方文檔中是這樣的:
@Entitypublic 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
@NoRepositoryBeanpublic 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
@NoRepositoryBeanpublic 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);