说明:
这篇文章只是一个照葫芦画瓢的实现记录,因为本人对数据库和JPA都不甚了解,很多东西似懂非懂,所以这里的记录也没啥实质内容。等待以后研究透彻后再来完善这篇的内容,如果大家有好的实现方式,请不吝赐教,万分感谢。
数据库表单如下,需要根据部门(或者公司名,或者人员姓名)获取所有的考勤记录:
返回数据要求: 分页,包括公司名,部门名,人员姓名,考勤时间。
如何实现?
研究来,研究去,没有找到理想的动态查询方法。最终根据这篇博客,勉强实现了查询
http://blog.csdn.net/trntaken/article/details/77870803
@NoRepositoryBean //该注解表示 spring 容器不会创建该对象
public interface BaseRepository extends PagingAndSortingRepository,JpaRepository {
/**
* sql查询
*
* @param sql
* @param args
* @return
*/
List
--
public class BaseRepositoryImpl extends SimpleJpaRepository
implements BaseRepository {
// 实体管理类,对持久化实体做增删改查,自动义sq操作模板所需要的核心类
public final EntityManager entityManager;
public BaseRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Page findPageByParams(String sql, Pageable pageable, Object... args) {
Session session = (Session) entityManager.getDelegate();
org.hibernate.Query q = session.createSQLQuery(sql);
q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
int i = 0;
for (Object arg : args) {
q.setParameter(i++, arg);
}
List totalCount = q.list();
q.setFirstResult(pageable.getPageSize() * (pageable.getPageNumber() - 1));
q.setMaxResults(pageable.getPageSize());
List pageCount = q.list();
Page pages = new PageImpl<>(pageCount, pageable, totalCount.size());
return pages;
}
@Override
@Transactional(rollbackFor = Exception.class)
public List findAllByParams(String sql, Object... args) {
Session session = (Session) entityManager.getDelegate();
org.hibernate.Query q = session.createSQLQuery(sql);
// 查询结果转map
q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
int i = 0;
for (Object arg : args) {
q.setParameter(i++, arg);
}
return q.list();
}
--
public interface AccessRecordRespository extends BaseRepository,JpaSpecificationExecutor{
Page findByEmployeeId(String employeeid,Pageable pageable);
List findByAccessTimeBetween(long start ,long end);
Page findAll(Pageable pageable);
Page findByDeviceId(String deviceid,Pageable pageable);
Page findByDeviceName(String devicename,Pageable pageable);
Page findByDeviceIdAndDeviceName(String devicename,String name,Pageable pageable);
}
获取接口:
@PostMapping("/query_access_recordes3")
@ResponseBody
public BaseResponse test(@RequestBody QueryAccessRecordeRequest request ){
String sql= "select company.name as companyname,department.name as departmentname,employee.name as name,access.time as time from company, department,employee,access where access.employee_id=employee.id and employee.department_id = department.id and department.company_id=company.id and employee.name = ?";
Page orderLists = accessRecordRespository.findPageByParams(sql,new PageRequest(0,10),"张三");
BaseResponse rsp = new BaseResponse();
rsp.setBody(orderLists);
return rsp;
}
最终返回的结果如下:
{
"result": 0,
"body": {
"content": [
{
"companyname": "IBM",
"departmentname": "研发部",
"name": "张三",
"time": "2017-11-3 09:30"
},
{
"companyname": "IBM",
"departmentname": "研发部",
"name": "张三",
"time": "2017-11-4 09:30"
},
.......
],
"totalPages": 18,
"totalElements": 180,
"last": false,
"number": 0,
"size": 10,
"sort": null,
"first": true,
"numberOfElements": 10
}}
未实现部分: 动态搜索考勤记录 ,即 公司名,部门名 ,姓名,考勤时间 自由组合作为参数,如何处理?