当前位置:网站首页>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 online beggar function of Japanese shopping websites
- Notblank and notempty
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- [micro service sentinel] sentinel integrates openfeign
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- Glass mosaic
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
- 字典、哈希表、数组的概念
- 每日三题 6.30
- [applet] realize the left and right [sliding] list through the scroll view component
猜你喜欢

Matplotlib common settings

Airserver latest win64 bit personal screen projection software

Experience of practical learning of Silicon Valley products
![[must] bm41 output the right view of the binary tree [medium +]](/img/a5/00b2f0df5ab448665a2b062d145e52.png)
[must] bm41 output the right view of the binary tree [medium +]

Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four

距离度量 —— 汉明距离(Hamming Distance)

Future trend and development of neural network Internet of things

Postgresql源码(57)HOT更新为什么性能差距那么大?

云信小课堂 | IM及音视频中常见的认知误区

Redis master-slave synchronization
随机推荐
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
想请教股票开户要认识谁?在线开户是安全么?
常见的积分商城游戏类型有哪些?
SWT / anr problem - SWT causes low memory killer (LMK)
每日三题 6.30(2)
2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
ARP message header format and request flow
Future trend and development of neural network Internet of things
Yunxin small class | common cognitive misunderstandings in IM and audio and video
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
CADD课程学习(3)-- 靶点药物相互作用
VIM color the catalogue
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
Linux基础 —— CentOS7 离线安装 MySQL
STM32F030F4驱动TIM1637数码管芯片
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
Why is PHP called hypertext preprocessor
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
Linux foundation - centos7 offline installation of MySQL