Love yourself|

Kaspar_Choo

园龄:5年7个月粉丝:11关注:3

springboot多数据源使用EntityManager

多数据源使用jpa和em,要么报错使用默认的数据源报错找不到表,要么就是报错没有这样的实体,debug调试使用的还是第一个EM。被这个问题困扰了很久,记录记录解决方法:

entityManagerFactorySecondary指的是springboot项目中的第二个数据源,此处表示导入第二个数据源的实体管理器
@PersistenceContext(unitName="entityManagerFactorySecondary")
EntityManager em;

EntityManager的使用如下:

String sql = " SELECT COUNT(*) FROM msg_examine_producer"+day;
Query query =em.createNativeQuery(sql);
List outData = query.getResultList()

用完之后记得关闭实体管理器

em.close();
posted @   Kaspar_Choo  阅读(849)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起