private @Autowired EntityManager em;
//private @PersistenceContext EntityManager defaultEm;
@SuppressWarnings("unchecked")
public List<Book> findByName1(String bookName, String expressName) {
String sql = "select * from t_book b left join t_express e on b.exp_id = e.id where "
+ "b.name like '" + bookName + "' and e.name like '" + expressName +"'";
Query query = em.createNativeQuery(sql, Book.class);
//query 有executeUpdate、getFirstResult等方法,可以根据sql调用
List<Book> bookList = query.getResultList();
return bookList;
}
//@Query(value="select * from t_book b left join t_express e on b.exp_id = e.id where "
// + "b.name like ?1 and e.name like ?2", nativeQuery=true)
//List<Book> findByName(String bookName, String expressName);
public List<Book> findByName2(String bookName, String expressName) {
return this.repository.findByName(bookName, expressName);
}
@SuppressWarnings("unchecked")
public List<BookExpress> findBookExpress1(String bookName, String expressName) {
String sql = "select b.id as bookId,b.name as bookName, e.id as expressId, e.name as expressName from t_book b left join t_express e "
+ "on b.exp_id = e.id where b.name like '" + bookName + "' and e.name like '" + expressName +"'";
//如果不设置自定义的ResultTransformer转换器,则Hibernate将每行返回结果的数据按照结果列的顺序装入Object数组中。
//关联表映射的时候,每个字段都需要addScalar
Query query = em.createNativeQuery(sql);
List<BookExpress> result = query.unwrap(SQLQuery.class)
.addScalar("bookId", LongType.INSTANCE).addScalar("bookName").addScalar("expressId", LongType.INSTANCE).addScalar("expressName")
.setResultTransformer(Transformers.aliasToBean(BookExpress.class)).list();
return result;
}
//注意sql里边的双引号转义
@SuppressWarnings("unchecked")
public List<BookExpress> findBookExpress2(String bookName, String expressName) throws IllegalAccessException, InvocationTargetException {
String sql = "select b.id as \"bookId\",b.name as \"bookName\", e.id as \"expressId\", e.name as \"expressName\" from t_book b left join t_express e "
+ "on b.exp_id = e.id where b.name like '" + bookName + "' and e.name like '" + expressName +"'";
Query query = em.createNativeQuery(sql);
List<Map<String, Object>> resultList = query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
if(CollectionUtils.isEmpty(resultList)) {
return null;
}
List<BookExpress> result = new ArrayList<>(resultList.size());
for(Map<String,Object> map : resultList) {
BookExpress exp = new BookExpress();
BeanUtils.populate(exp, map);
result.add(exp);
}
return result;
}
//getDelegate不用事务就能获取到session,但是dowork查询的时候为什么报错(org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@10cf3d8 is closed)
public List<BookExpress> findBookExpress3(String bookName, String expressName) {
Session session = (Session)em.getDelegate();//为什么getDelegate不需要事物,unwrap需要事务
//Session session = em.unwrap(Session.class);
BookExpressQueryWork1 bookExpressQueryWork = new BookExpressQueryWork1();
bookExpressQueryWork.setBookName(bookName);
bookExpressQueryWork.setExpressName(expressName);
session.doWork(bookExpressQueryWork);
session.disconnect();
return bookExpressQueryWork.getBookExpresses();
}
JPA下的原生sql查询
最新推荐文章于 2024-08-07 21:08:36 发布