当前位置:网站首页>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;
}
}
边栏推荐
- Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
- 微信个人小商店一键开通助手小程序开发
- [MySQL] index creation, viewing and deletion
- YOGA27多维一体电脑,兼具出色外观与高端配置
- CKS CKA CKAD 将终端更改为远程桌面
- 关于游戏性能优化的一些感想
- What category does the Internet of things application technology major belong to
- Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
- [kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
- Rank ranking with MySQL 5.7
猜你喜欢

Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry

Three development trends of enterprise application from the perspective of the third technological revolution

从第三次技术革命看企业应用三大开发趋势

问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c

What professional classification does the application of Internet of things technology belong to

2022 safety officer-c certificate examination question simulation examination question bank and simulation examination

CKS CKA ckad change terminal to remote desktop

Some abilities can't be learned from work. Look at this article, more than 90% of peers

Summary of "performance testing" of software testing, novice will know the knowledge points on the road
![[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party](/img/ad/dcb993c3e1e79d2c3663f031dfd1b3.png)
[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
随机推荐
每日三题 6.28
vim给目录加颜色
【微服务|Sentinel】@SentinelResource详解
Detailed explanation of twenty common software testing methods (the most complete in History)
mysql binlog的清理
openresty 负载均衡
Matplotlib常用设置
Treatment of insufficient space in the root partition of armbain system
"35 years old, the boss of the company, with a monthly salary of 20000, give away takeout": the times abandoned you, not even saying goodbye
距离度量 —— 汉明距离(Hamming Distance)
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
软件架构的本质
想请教股票开户要认识谁?在线开户是安全么?
Huisheng Huiying 2022 intelligent, fast and simple video editing software
Stm32f030f4 drives tim1637 nixie tube chip
[micro service sentinel] sentinelresourceaspect details
YOGA27多维一体电脑,兼具出色外观与高端配置
What category does the Internet of things application technology major belong to
数字峰会人气火爆,城链科技引发新一轮商业变革
CKS CKA ckad change terminal to remote desktop