设为首页 加入收藏

TOP

mybatis配置使用多个数据源(三)
2015-07-24 10:24:28 来源: 作者: 【 】 浏览:2
Tags:mybatis 配置 使用 多个 数据源
n); } /** * Mapper Proxy * executing mapper method and close sqlsession * @author boyce * @version 2014-4-9 */ private static class MapperProxy implements InvocationHandler { private Mapper mapper; private SqlSession sqlSession; private MapperProxy(Mapper mapper, SqlSession sqlSession) { this.mapper = mapper; this.sqlSession = sqlSession; } private static Mapper bind(Mapper mapper, SqlSession sqlSession) { return (Mapper) Proxy.newProxyInstance(mapper.getClass().getClassLoader(), mapper.getClass().getInterfaces(), new MapperProxy(mapper, sqlSession)); } /** * execute mapper method and finally close sqlSession */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object object = null; try { object = method.invoke(mapper, args); } catch(Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return object; } } }
客户端使用场景:
Java代码
UserMapper mapper = MapperFactory.HO.createMapper(UserMapper.class);  
User user = mapper.getUserById(162L);  
System.out.println(user);  

ok,如果大家有什么更优雅的设计方案请不吝分享分享。
首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Delete删除表数据时对性能的影响.. 下一篇Redis的持久化-AOF

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)