当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
![[micro service sentinel] sentinel integrates openfeign](/img/8b/46156255fd980eb422c7e05d5af7ee.png)
[micro service sentinel] sentinel integrates openfeign

2022 crane driver (limited to bridge crane) examination questions and simulation examination

纪念成为首个DAYUs200三方demo贡献者
![Jerry's burning of upper version materials requires [chapter]](/img/65/fcd804e00dc08a2bd056e8e6493829.png)
Jerry's burning of upper version materials requires [chapter]

Glass mosaic

You probably haven't noticed the very important testing strategy in your work

Zero foundation tutorial of Internet of things development

【小程序】通过scroll-view组件实现左右【滑动】列表

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘

2022 R1 fast opening pressure vessel operation test questions and answers
随机推荐
内存泄露和内存溢出的区别是什么?
rviz打开后如何显示实时2D地图
Jerry's burning of upper version materials requires [chapter]
【Swoole系列1】在Swoole的世界中,你将学习到什么?
[micro service sentinel] @sentinelresource details
Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
Behind sharing e-commerce: the spirit of CO creation, symbiosis, sharing, CO prosperity and win-win
[MySQL] basic use of explain and the function of each column
Switch to software testing, knowing these four points is enough!
Daily three questions 6.30
每日三题 6.30
Why is PHP called hypertext preprocessor
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
CKS CKA CKAD 将终端更改为远程桌面
The digital summit is popular, and city chain technology has triggered a new round of business transformation
“35岁,公司老总,月薪2万送外卖“:时代抛弃你,连声再见都没有
2022年起重机司机(限桥式起重机)考试试题及模拟考试
从第三次技术革命看企业应用三大开发趋势
Linux foundation - centos7 offline installation of MySQL
Linux基础 —— CentOS7 离线安装 MySQL