mybatis怎么配置使用多个数据源
展开全部
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);
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);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询