jpa写原生sql-EntityManager
废话不多说
package com.meeno.trainsys.meeting.service;
import com.google.common.collect.Lists;
import com.meeno.framework.constants.Constants;
import com.meeno.framework.page.utils.PageUtils;
import com.meeno.framework.util.Constant;
import com.meeno.framework.util.DateUtils;
import com.meeno.framework.util.ErrEnum;
import com.meeno.framework.util.MeenoAssert;
import com.meeno.trainsys.employee.entity.Employee;
import com.meeno.trainsys.employee.repository.EmployeeRepository;
import com.meeno.trainsys.meeting.entity.Meeting;
import com.meeno.trainsys.meeting.entity.SignRecord;
import com.meeno.trainsys.meeting.repository.MeetingRepository;
import com.meeno.trainsys.meeting.repository.SignRecordRepository;
import com.meeno.trainsys.meeting.view.SignRecordView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;
/**
* @description: 签到ServiceImpl
* @author: Wzq
* @create: 2020-01-03 11:24
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class SignRecordServiceImpl implements SignRecordService {
@PersistenceContext
EntityManager entityManager;
@Override
public Object findMeetingSignRecoredPage(Long meetingId, Integer pageIndex, Integer pageSize) {
MeenoAssert.notNull(meetingId, ErrEnum.MEETING_ID_IS_EMPTY);
Meeting meeting = this.meetingRepository.getOne(meetingId);
MeenoAssert.notNull(meeting,ErrEnum.MEETING_IS_NOT_EXIST);
Pageable pageable = PageUtils.getPageable(pageIndex,pageSize);
StringBuilder sql = new StringBuilder();
sql.append(" select ep.name,MAX(r.sign) from mnt_emp_rel_meeting as e ");
sql.append(" left join mnt_sign_record as r on(r.employee_id=e.employee_id) ");
sql.append(" left join mnt_employee as ep on(ep.id = e.employee_id) ");
sql.append(" where e.meeting_id = ? ");
sql.append(" order by r.sign desc,r.create_date asc ");
Query query = entityManager.createNativeQuery(sql.toString());
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
query.setParameter(1,meetingId);
List<Object[]> list = query.getResultList();
List<SignRecordView> listView = Lists.newArrayList();
if(list != null && !list.isEmpty()){
for (Object[] objects : list) {
String empName = objects[0]==null?null:objects[0].toString();
Integer sign = objects[1]==null?Constants.SIGN_RECORD_NO:Integer.parseInt(objects[1].toString());
SignRecordView view = new SignRecordView();
view.setEmployeeName(empName);
view.setSign(sign);
listView.add(view);
}
}
//count
StringBuilder countSql = new StringBuilder();
countSql.append(" select count(distinct e.id) from mnt_emp_rel_meeting as e ");
countSql.append(" left join mnt_sign_record as r on(r.employee_id=e.employee_id) ");
countSql.append(" left join mnt_employee as ep on(ep.id = e.employee_id) ");
countSql.append(" where e.meeting_id = ? ");
countSql.append(" order by r.sign desc,r.create_date asc ");
Query countQuery = entityManager.createNativeQuery(countSql.toString());
countQuery.setParameter(1,meetingId);
Object singleResult = countQuery.getResultList().get(0);
Integer count = singleResult==null?0:Integer.valueOf(singleResult.toString());
Map<String, Object> resultPage = PageUtils.getResultPage(count, listView);
return resultPage;
}
}
jpa写原生sql-EntityManager的更多相关文章
- idea下spring boot jpa写原生sql的时候,报Cannot resolve table错误
错误如图 打开View→Tool Windows→Persistence选项 在弹出的Persistence窗口的项目上右键,选择Generate Persistence Mapping→By Dat ...
- spring data jpa使用原生sql查询
spring data jpa使用原生sql查询 @Repository public interface AjDao extends JpaRepository<Aj,String> { ...
- SpringData JPA 使用原生 SQL
在实现个人博客系统的归档功能的时候,遇上这样的需求: 先把数据库中所有条目的时间按照年月分组,并查询出年月(String)的列表 根据年月字符串查询符合条件的博客,并返回博客列表 由于数据访问层使用的 ...
- SpringDataJpa使用原生sql(EntityManager)动态拼接,分页查询
SpringDataJpa Spring Data JPA是较大的Spring Data系列的一部分,可轻松实现基于JPA的存储库.该模块处理对基于JPA的数据访问层的增强支持.它使构建使用数据访问技 ...
- CI中写原生SQL(封装查询)
封装查询 封装,通过让系统为你组装各个查询语句,能够简化你的查询语法.参加下面的范例: $sql = "SELECT * FROM some_table WHERE id = ? AND s ...
- jpa使用原生SQL查询数据库like的用法
jpa使用like查询,需要拼接字符串,如下 oracle用法: //dao层代码 @Query(value = "SELECT * FROM TABLENAME WHERE USER_NA ...
- django写原生sql语句
执行自定义SQL语言: from django.db import connection cursor=connection.cursor() # 插入操作 cursor.execute(&q ...
- jpa 联表查询 返回自定义对象 hql语法 原生sql 语法 1.11.9版本
-----业务场景中经常涉及到联查,jpa的hql语法提供了内连接的查询方式(不支持复杂hql,比如left join ,right join). 上代码了 1.我们要联查房屋和房屋用户中间表,通过 ...
- JPA框架下使用纯粹的原生SQL
最近遇到一个需求,查询数据库中对应表的字段是动态的,项目使用的框架使用JPA+Spring Boot,JPA自带原生SQL支持的传入参数是强类型的,无法用于查询语句的字段更改,因为插入字符串的话带有单 ...
随机推荐
- excle名字后面直接跟别的出来
名字后面直接跟别的出来 =IF($E6="","",VLOOKUP(E6,通讯录!$B$2:$D$1000,3,0)) $E6:是填写位置的地方 VLOOKUP ...
- python 遍历文件夹中所有文件
'''使用walk方法递归遍历目录文件,walk方法会返回一个三元组,分别是root.dirs和files. 其中root是当前正在遍历的目录路径:dirs是一个列表,包含当前正在遍历的目录下所有的子 ...
- urllib库中的URL编码解码和GETPOST请求
在urllib库的使用过程中,会在请求发送之前按照发送请求的方式进行编码处理,来使得传递的参数更加的安全,也更加符合模拟浏览器发送请求的形式.这就需要用urllib中的parse模块.parse的使用 ...
- robotframework - PO设计
1.添加新建好的资源 2.测试用例原始代码如下(未做任何分离的数据) *** Settings ***Library SeleniumLibraryResource UI分层一.txtResource ...
- 微信小程序云开发-数据库-更新数据
一.js文件代码使用.update更新数据 写一个更新数据的函数,函数内使用.update更新数据.一定要通过.doc指定修改哪一条数据. 二.wxml文件修改数据的按钮 在wxml文件中写[修改] ...
- Leetcode:面试题68 - II. 二叉树的最近公共祖先
Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...
- OVERLAPPED 结构
typedef struct _OVERLAPPED { ULONG_PTR Internal; ULONG_PTR InternalHigh; union { struct { DWORD Offs ...
- jquery版本更新后无live函数的处理.TypeError: $(...).live is not a function
jquery live函数语法 jquery版本更新, 发现一个问题: jq自带的live没有了.控制台下会有如下的提示:火狐: TypeError: $(...).live is not a fun ...
- PHP imap 远程命令执行漏洞(CVE-2018-19518)
影响版本 PHP:5.6.38 系统:Debian/ubuntu 构造数据包 成功执行命令echo '1234567890'>/tmp/test0001
- 使用ffmpeg给视频添加跑马灯效果(滚动字幕)
直接上命令 从左往右滚 ffmpeg -i input.mp4 -vf "drawtext=text=string1 string2 string3 string4 string5 stri ...