当前位置:网站首页>jpa手写sql,用自定义实体类接收
jpa手写sql,用自定义实体类接收
2022-07-01 22:54:00 【loliDapao】
用nativeQuery,写在@Query中无法转换为自定义的实体类
@Query(value = "SELECT name AS orderName,DATE_FORMAT(create_date, ?1) AS time from order_image GROUP BY time ORDER BY time", nativeQuery = true)
List<Map<String, Object>> findCountByTime(String time);
@Data @NoArgsConstructor
@AllArgsConstructor
public class orderDto {
private String orderName;
private String time;
}
JPA官方推荐的多表关联查询使用不便,接触的有些项目可能会使用JPA 做简单查询,Mybaits做复杂查询。所以想要寻找一种好用的解决方案。
JPA多表关联的实现方式
1.使用Specification实现映射关系匹配,如@ManyToOne等
2.使用NativeQuery等sql或hql来实现
优缺点对比
1.映射关系是hibernate的入门基础,很多人都会习惯去使用。个人不太喜欢这种方式,复用性太弱,且不灵活特别是在多表复杂业务情况下。
2.使用Specification方式需要继承JpaSpecificationExecutor接口,构造对应的方法后传入封装查询条件的Specification对象。逻辑上简单易懂,但是构造Specification对象需要拼接格式条件非常繁琐。
3.直接使用NativeQuery等方式实现复杂查询个人比较喜欢,直观且便利,弊端在于无法返回自定义实体类。需要手动封装工具类来实现Object到目标对象的反射。
使用sql并返回自定义实体类
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
@Repository
public class EntityManagerDAO {
@PersistenceContext
private EntityManager entityManager;
/**
* 人员列表排序
* @return
*/
@Transactional
public List<BackstageUserListDTO> listUser(){
String sql = "select a.create_time createTime," +
"a.mobilephone phoneNum," +
"a.email email,a.uid uid," +
"a.enabled enabled," +
"c.id_number idNumber," +
" (case b.`status` when 1 then 1 else 0 end) status " +
"from tbl_sys_user a " +
"LEFT JOIN user_high_qic b on a.uid=b.u_id" +
"LEFT JOIN user_qic c on a.uid=c.uid " +
"ORDER BY status desc";
SQLQuery sqlQuery = entityManager.createNativeQuery(sql).unwrap(SQLQuery.class);
Query query =
sqlQuery.setResultTransformer(Transformers.aliasToBean(BackstageUserListDTO.class));
List<BackstageUserListDTO> list = query.list();
entityManager.clear();
return list;
}
}
边栏推荐
- win 10 mstsc连接 RemoteApp
- [micro service sentinel] sentinel integrates openfeign
- 马赛克后挡板是什么?
- Daily three questions 6.30
- Zero foundation tutorial of Internet of things development
- Convergence and disposal suggestions of some Internet exposure surfaces
- 认识--Matplotlib
- [MySQL] database optimization method
- Linux基础 —— CentOS7 离线安装 MySQL
- 每日三题 6.30
猜你喜欢
CKS CKA CKAD 将终端更改为远程桌面
Istio, ebpf and rsocket Broker: in depth study of service grid
马赛克后挡板是什么?
纪念成为首个DAYUs200三方demo贡献者
Paramètres communs de matplotlib
CKS CKA ckad change terminal to remote desktop
2022 crane driver (limited to bridge crane) examination questions and simulation examination
Three development trends of enterprise application from the perspective of the third technological revolution
Jielizhi, production line assembly link [chapter]
MT manager test skiing Adventure
随机推荐
Airserver latest win64 bit personal screen projection software
2022安全员-C证考试题模拟考试题库及模拟考试
【小程序】通过scroll-view组件实现左右【滑动】列表
[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
flutter Unable to load asset: assets/images/888. png
“35岁,公司老总,月薪2万送外卖“:时代抛弃你,连声再见都没有
Three development trends of enterprise application from the perspective of the third technological revolution
实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”
每日三题 6.28
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
从第三次技术革命看企业应用三大开发趋势
Win 10 mstsc connect RemoteApp
dat.GUI
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Jerry's burning of upper version materials requires [chapter]
You probably haven't noticed the very important testing strategy in your work
纪念成为首个DAYUs200三方demo贡献者
flutter Unable to load asset: assets/images/888.png
rviz打开后如何显示实时2D地图
Why is PHP called hypertext preprocessor