当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘

Current situation and future development trend of Internet of things

CADD课程学习(3)-- 靶点药物相互作用

Commemorate becoming the first dayus200 tripartite demo contributor

from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

What is the mosaic tailgate?

物联网应用技术专业是属于什么类

Win 10 mstsc connect RemoteApp

mt管理器测试滑雪大冒险
随机推荐
Redis AOF日志
Postgresql源码(57)HOT更新为什么性能差距那么大?
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
神经网络物联网的未来趋势与发展
云信小课堂 | IM及音视频中常见的认知误区
sql 优化
SQL optimization
Redis 主从同步
认识--Matplotlib
转行软件测试,知道这四点就够了!
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
[micro service sentinel] @sentinelresource details
openwrt 开启KV漫游
Current situation and future development trend of Internet of things
[micro service sentinel] sentinelresourceaspect details
MT manager test skiing Adventure
Huisheng Huiying 2022 intelligent, fast and simple video editing software
The digital summit is popular, and city chain technology has triggered a new round of business transformation
Airserver latest win64 bit personal screen projection software
What professional classification does the application of Internet of things technology belong to