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