推荐的文件分层结构如下
com.XXX.lib.action 主要放置Action,控制前台跳转
com.XXX.lib.cservice 主要放置Service,实现业务逻辑
com.XXX.lib.dao 主要放置dao,实现数据库操作
com.XXX.lib.emodel 主要放置Model,实现模型操作
com.XXX.lib.fintface 主要放置interface,定义接口
com.XXX.lib.global 主要放置全局共享类和管理者
com.XXX.lib.global.constant 主要放置全局变量
com.XXX.lib.global.proxy 主要放置全局代理
com.XXX.lib.mybatis 主要放置Mybatis对象
接着上面的,完成mybatis链接
globalManager如下
SqlSesFacBuilder代码如下
MybatisConstant如下
SqlSessionProxy如下
一个mybatis配置实例如下
com.XXX.lib.action 主要放置Action,控制前台跳转
com.XXX.lib.cservice 主要放置Service,实现业务逻辑
com.XXX.lib.dao 主要放置dao,实现数据库操作
com.XXX.lib.emodel 主要放置Model,实现模型操作
com.XXX.lib.fintface 主要放置interface,定义接口
com.XXX.lib.global 主要放置全局共享类和管理者
com.XXX.lib.global.constant 主要放置全局变量
com.XXX.lib.global.proxy 主要放置全局代理
com.XXX.lib.mybatis 主要放置Mybatis对象
接着上面的,完成mybatis链接
globalManager如下
package com.XXX.lib.global;
import org.apache.ibatis.session.SqlSessionFactory;
public class GlobalManager {
private static GlobalManager instance = null;
public static void setInstance(GlobalManager instance) {
GlobalManager.instance = instance;
}
public static GlobalManager getInstance() {//synchronized防止多线程错误 synchronized
if(instance == null){//第一次调用被实例化,以后再也不会被实例化了
instance = new GlobalManager();
instance.init();
}
return instance;
}
public SqlSessionFactory sqlSessionFactory;//全局sqlSessionFactory
public void init(){
sqlSessionFactory = (new SqlSesFacBuilder()).GetFactory();//获得一个SqlSessionFactory,对全局可见
}
}
SqlSesFacBuilder代码如下
package com.XXX.lib.global;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.XXX.lib.global.constant.MybatisConstant;
public class SqlSesFacBuilder {
public SqlSessionFactory GetFactory(){
SqlSessionFactory sqlSessionFactory=null;
try{
//配置路径
String resource = MybatisConstant.MybatisResource;
//加载配置文件
Reader reader = Resources.getResourceAsReader(resource);
//生成一个SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
e.printStackTrace();
}
return sqlSessionFactory;
}
}
MybatisConstant如下
package com.XXX.lib.global.constant;
public class MybatisConstant {
public final static String MybatisResource = "com/XXX/lib/mybatis/Configuration.xml";
}
SqlSessionProxy如下
package com.XXX.lib.global.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.XXX.lib.global.GlobalManager;
public class SqlSessionProxy implements InvocationHandler{
private Object proxyobj;
public SqlSessionProxy(Object obj){
proxyobj = obj;
}
/**
* @param obj
* @return
* 静态工厂方法
*/
public static Object factory(Object obj){
Object result = null;
Class<?> cls = obj.getClass();
result = Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new SqlSessionProxy(obj));
return result;
}
/**
* Java反射方法
*/
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
Object result;
try{
SqlSessionFactory sqlSessionFactory= GlobalManager.getInstance().sqlSessionFactory;
SqlSession sqlSession = sqlSessionFactory.openSession(true);//打开一个SqlSession,true代表自动提交
args[0] = sqlSession;//改变第一个参数的值
result = method.invoke(proxyobj, args);
sqlSession.close();//关闭一个SqlSession
}catch(InvocationTargetException e){
e.printStackTrace();
throw e.getTargetException();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException("unexpected invocation exception: " + e.getMessage());
}
return result;
}
}
一个mybatis配置实例如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.XXX.lib.fintface.common.CommonIntface">
<select id="lodgCateList" parameterType="HashMap" resultType="HashMap">
SELECT * FROM EMP
</select>
</mapper>