当前位置:网站首页>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;
}
}
边栏推荐
- 【小程序】通过scroll-view组件实现左右【滑动】列表
- 建模和影视后期有什么关联?
- Microservice stability management
- Practical application and extension of plain framework
- 距离度量 —— 汉明距离(Hamming Distance)
- 日本购物网站的网络乞丐功能
- Redis~02 缓存:更新数据时如何保证MySQL和Redis中的数据一致性?
- 物联网开发零基础教程
- 赵福全:短期解决保供,长期要打造安全、高效有韧性的供应链
- Convergence and disposal suggestions of some Internet exposure surfaces
猜你喜欢
Programming English vocabulary notebook
[micro service sentinel] sentinel integrates openfeign
软件架构的本质
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
Matplotlib常用设置
CKS CKA ckad change terminal to remote desktop
【小程序】通过scroll-view组件实现左右【滑动】列表
硅谷产品实战学习感触
MT manager test skiing Adventure
随机推荐
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
js——arguments的使用
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Microservice stability management
证券开户选哪个证券公司比较好,哪个更安全
赵福全:短期解决保供,长期要打造安全、高效有韧性的供应链
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
Redis data types and application scenarios
Paramètres communs de matplotlib
AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
通过Go语言创建CA与签发证书
SWT / anr problem - SWT causes kernel fuse deadlock
从第三次技术革命看企业应用三大开发趋势
CKS CKA ckad change terminal to remote desktop
Istio、eBPF 和 RSocket Broker:深入研究服务网格
Matplotlib常用設置
认识--Matplotlib
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
MT manager test skiing Adventure