当前位置:网站首页>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;
}
}
边栏推荐
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
- 为什么PHP叫超文本预处理器
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- CADD课程学习(3)-- 靶点药物相互作用
- Daily three questions 6.30
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- 通过Go语言创建CA与签发证书
- [micro service sentinel] @sentinelresource details
- 内存泄露和内存溢出的区别是什么?
猜你喜欢
2022 crane driver (limited to bridge crane) examination questions and simulation examination
为什么PHP叫超文本预处理器
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
[must] bm41 output the right view of the binary tree [medium +]
ARP message header format and request flow
物联网开发零基础教程
Material Design组件 - 使用BottomSheet展现扩展内容(一)
软件架构的本质
云信小课堂 | IM及音视频中常见的认知误区
Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
随机推荐
Stm32f030f4 drives tim1637 nixie tube chip
Daily three questions 6.28
What is the difference between memory leak and memory overflow?
【微服务|Sentinel】sentinel整合openfeign
图的遍历之深度优先搜索和广度优先搜索
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
MySQL binlog cleanup
Matplotlib common settings
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
Yunxin small class | common cognitive misunderstandings in IM and audio and video
Huisheng Huiying 2022 intelligent, fast and simple video editing software
Matplotlib common charts
Redis master-slave synchronization
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
Matplotlib常用图表
VIM color the catalogue
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c