JAP使用createNativeQuery做原始的字段映射处理
- //方法一:
- publicvoid testNativeQuery(){
- Query query = entityManager.createNativeQuery("select id, name, age from t_user");
- query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
- List rows = query.getResultList();
- for(Object obj : rows){
- Map row =(Map) obj;
- System.out.println("id = "+ row.get("ID"));
- System.out.println("name = "+ row.get("NAME"));
- System.out.println("age = "+ row.get("AGE"));
- }
- }
- //方法二:
- @PersistenceContext
- private EntityManager manager;
- @Override
- public UserEntity findById(int id) {
- String sql = "select id,name from users where id = :id";
- Query q = manager.createNativeQuery(sql);
- q.unwrap(NativeQuery.class)
- .setResultTransformer(Transformers.aliasToBean(UserEntity.class));
- q.setParameter("id", id);
- List<UserEntity> results = q.getResultList();
- UserEntity user = results.get(0);
- return user;
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · PCF常见命令
- · Java基础问题13个,你都会哪些?
- · 不重新打包项目并替换部分jar包
- · 接收企微事件回调 Content type ‘text/xml;charset=UTF-8’ not supported
- · springboot使用lock4j实现并发控制
- · springboot全局增加sentinel
- · linux快速搭建轻量级efk日志系统
- · Springboot整合Swagger常用注解
- · swagger隐藏authentication参数
- · Spring Security 中的自定义PreAuthorize 注解
- · Expected one result (or null) to be returned by selectOne(), but found: 2
- · JACKSON和FASTJSON处理返回JSON数据中为NULL字段不显示