mybatis怎么配置使用多个数据源

 我来答
就烦条0o
2016-07-25 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
Java代码 UserMapper mapper = MapperFactory.createMapper(UserMapper.class, DataSourceEnvironment.HD);
User user = mapper.getUserById(162L);
System.out.println(user);

OK,到此基本上所有的工作就完成了,以上的方式就可以支持多个数据源了。
但是代码还不够优雅,以上代码我们发现DataSourceEnvironment这个枚举变量在客户端,MapperFactory以及DataSourceEnvironment
中传来传去,我们应该尽量减少一个类对外界类的耦合,也就是不符合Java编程原则中的迪米特法则。
好了,那我们来改良一下。
将MapperFactory设计成枚举策略模式:

Java代码
/**
* Mapper Creator
* @author boyce
* @version 2014-3-28
*/
public enum MapperFactory {

HD {
private SqlSessionFactory sqlSessionFactory;

@Override
public <T> T createMapper(Class<? extends Mapper> clazz) {
return createMapper(clazz, this);
}

@Override
protected void createSqlSessionFactory() {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, this.name());
}

@Override
public SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}

},
HO {
private SqlSessionFactory sqlSessionFactory;
@Override
public <T> T createMapper(Class<? extends Mapper> clazz) {
return createMapper(clazz, this);
}

@Override
protected void createSqlSessionFactory() {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, this.name());
}

@Override
public SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
};

/**
* Create a mapper of environment by Mapper class
* @param clazz Mapper class
* @param environment A datasource environment
* @return a Mapper instance
*/
public abstract <T> T createMapper(Class<? extends Mapper> clazz);

/**
* Create SqlSessionFactory of environment
*/
protected abstract void createSqlSessionFactory();

/**
* get SqlSessionFactory
*/
public abstract SqlSessionFactory getSqlSessionFactory();

private static InputStream inputStream = null;
static {
try {
inputStream = Resources.getResourceAsStream("mybatis/mybatis.xml");
HO.createSqlSessionFactory();
HD.createSqlSessionFactory();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inputStream);
}
}

@SuppressWarnings("unchecked")
private static <T> T createMapper(Class<? extends Mapper> clazz, MapperFactory MapperFactory) {
SqlSession sqlSession = MapperFactory.getSqlSessionFactory().openSession();
Mapper mapper = sqlSession.getMapper(clazz);
return (T)MapperProxy.bind(mapper, sqlSession);
}

/**
* 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);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式