Spring Data Jpa 學習——偷懶一定要做到極致__jpa

來源:互聯網
上載者:User

項目中用到了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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.