当前位置:网站首页>JPA handwritten SQL, received with user-defined entity classes
JPA handwritten SQL, received with user-defined entity classes
2022-07-01 23:31:00 【loliDapao】
use nativeQuery, Written in @Query Cannot be converted to a custom entity class
@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 The official recommended multi table Association query is inconvenient to use , Some of the projects contacted may use JPA Do a simple query ,Mybaits Do complex queries . So I want to find an easy-to-use solution .
JPA Implementation of multi table Association
1. Use Specification Realize mapping relationship matching , Such as @ManyToOne etc.
2. Use NativeQuery etc. sql or hql To achieve
Comparison of advantages and disadvantages
1. The mapping relationship is hibernate The basis of the introduction of , Many people will get used to using . I don't like this way very much , Reusability is too weak , And inflexible, especially in the case of multi table complex business .
2. Use Specification Methods need to be inherited JpaSpecificationExecutor Interface , Construct the corresponding method and then transfer it to the Specification object . Logically easy to understand , But construction Specification Objects need to be spliced. The format conditions are very cumbersome .
3. Use it directly NativeQuery And other ways to achieve complex queries. I prefer , Intuitive and convenient , The disadvantage is that you cannot return a custom entity class . You need to manually encapsulate tool classes to implement Object Reflection to the target object .
Use sql And return the custom entity class
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;
/**
* Sort personnel list
* @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;
}
}
边栏推荐
- Redis AOF日志
- What is mosaic?
- Practical application and extension of plain framework
- sql 优化
- Glass mosaic
- Linux foundation - centos7 offline installation of MySQL
- y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
- Future trend and development of neural network Internet of things
- plain framework的实际应用和扩展
- excel如何打开100万行以上的csv文件
猜你喜欢

PostgreSQL source code (57) why is the performance gap so large in hot update?

【微服务|Sentinel】sentinel整合openfeign

【必会】BM41 输出二叉树的右视图【中等+】

ARP message header format and request flow

What is mosaic?

Redis AOF log

Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term

What is the relationship between modeling and later film and television?

软件架构的本质

Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
随机推荐
ARP报文头部格式和请求流程
ConcurrentSkipListMap——跳表原理
SWT/ANR问题--SWT 导致 kernel fuse deadlock
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
Material Design组件 - 使用BottomSheet展现扩展内容(一)
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
常见的积分商城游戏类型有哪些?
Microservice stability management
What category does the Internet of things application technology major belong to
通过Go语言创建CA与签发证书
从第三次技术革命看企业应用三大开发趋势
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
Huisheng Huiying 2022 intelligent, fast and simple video editing software
Redis RDB snapshot
ShanDong Multi-University Training #3
SWT/ANR问题--SWT 导致 low memory killer(LMK)
小程序表单校验封装
Who do you want to know when opening a stock account? Is it safe to open an account online?
Matplotlib常用设置
What is mosaic?