Spring-data jpa学習


1つの朝に1つの昼をプラスしてここで消耗して、1つの小さい问题だけあって、私に血を吐かせて、私の暗い3も游ぶことを顾みていないで、気がふさぎます

//       
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からです!だからページを分ける時、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" />