当前位置:网站首页>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;
}
}
边栏推荐
- Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- 纪念成为首个DAYUs200三方demo贡献者
- vim给目录加颜色
- YOGA27多维一体电脑,兼具出色外观与高端配置
- 【微服务|Sentinel】sentinel整合openfeign
- 物联网现状及未来发展趋势
- rviz打开后如何显示实时2D地图
- Matplotlib common settings
- Redis data types and application scenarios
猜你喜欢

The online beggar function of Japanese shopping websites

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
![Jerry's burning of upper version materials requires [chapter]](/img/65/fcd804e00dc08a2bd056e8e6493829.png)
Jerry's burning of upper version materials requires [chapter]

MT manager test skiing Adventure

CKS CKA ckad change terminal to remote desktop

rviz打开后如何显示实时2D地图

Why is PHP called hypertext preprocessor

认识--Matplotlib

SWT/ANR问题--SWT 导致 kernel fuse deadlock

Airserver latest win64 bit personal screen projection software
随机推荐
上海炒股开户选择手机办理安全吗?
每日三题 6.30
Linux foundation - centos7 offline installation of MySQL
Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
Compare the version number [double pointer to intercept the string you want]
通过Go语言创建CA与签发证书
plain framework的实际应用和扩展
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
微服务服务稳定性治理
转行软件测试,知道这四点就够了!
Distance measurement - Hamming distance
mysql ---- Oracle中的rownum转换成MySQL
What is mosaic?
You probably haven't noticed the very important testing strategy in your work
神经网络物联网的发展趋势和未来方向
Daily three questions 6.29
URL 介绍
Jerry's question about long press boot detection [chapter]
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
Switch to software testing, knowing these four points is enough!