- //首先是表的映射
- public class OperLog implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue
- private Integer id;
- @Column
- @JsonSerialize(using=DateJsonSerialize.class)
- private Date stamp;
- @Column
- private String oper;
- @Column
- private String memo;
- }
- //DAO只要接口就可以了,
- public interface OperLogRepository extends CrudRepository<OperLog, Integer>,JpaSpecificationExecutor<OperLog> {
- }
- //service层
- @Service("dataservice")
- public class DataServiceImpl implements DataService {
- public Pagenation<OperLog> pageOperLog(int page, int rows, String key, Date start, Date end) {
- Pageable pinfo = new PageRequest(page-1, rows,new Sort(Direction.DESC,"id"));
- Page<OperLog> infos = olDao.findAll(buildOperSpecification(key, start,end), pinfo);
- Pagenation<OperLog> pg = new Pagenation<OperLog>(page, rows);
- pg.setRows(infos.getContent());
- pg.setTotal(new Long(infos.getTotalElements()).intValue());
- return pg;
- }
- private Specification<OperLog> buildOperSpecification(final String key,
- final Date start, final Date end) {
- return new Specification<OperLog>() {
- public Predicate toPredicate(Root<OperLog> root, CriteriaQuery<?> q, CriteriaBuilder cb) {
- List<Predicate> predicates = new ArrayList<Predicate>();
- if(StringUtils.hasText(key)){
- Path<String> np = root.get("oper");
- predicates.add(cb.like(np, "%" + key + "%"));
- }
- if(start!=null){
- Path<Date> np = root.get("stamp");
- predicates.add(cb.greaterThanOrEqualTo(np, start));
- }
- if(end!=null){
- Path<Date> np = root.get("stamp");
- predicates.add(cb.greaterThanOrEqualTo(np, end));
- }
- if (predicates.size() > 0) {
- return cb.and(predicates.toArray(new Predicate[predicates.size()]));
- }
- return cb.conjunction();
- }
- };
- }
- @Autowired
- OperLogRepository olDao;
这里千万要注意的是PageRequest的页码是从0开始的,而我自己页码是从1开始的!所以导致分页的时候,只做了一个count语句,记录没有取!血的教训啊!
下面是一些配置文件
- <bean id="myEmf"
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="packagesToScan" value="com.fox.dataws.model" />
- <property name="jpaVendorAdapter">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
- <property name="showSql" value="${hibernate.show_sql}" />
- <property name="generateDdl" value="${jpa.generateDdl}" />
- <property name="databasePlatform" value="${persistence.dialect}" />
- </bean>
- </property>
- </bean>
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="${db.driver}" />
- <property name="url" value="${db.url}" />
- <property name="username" value="${db.uid}" />
- <property name="password" value="${db.pwd}" />
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="myEmf" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- <jpa:repositories base-package="com.fox.dataws.repo" />