今天闲余时间整合了下jfinal和mybatis,废话不多说,直接上代码
public class SysConfig extends JFinalConfig{ @Override public void configConstant(Constants me) { } @Override public void configRoute(Routes me) { me.add("/", IndexController.class); } @Override public void configEngine(Engine me) { } @Override public void configPlugin(Plugins me) { MyBatisPlugin myBatisPlugin = new MyBatisPlugin("mybatis.xml"); myBatisPlugin.addMapper(SystemAdminMapper.class); me.add(myBatisPlugin); } @Override public void configInterceptor(Interceptors me) { me.add(new MapperBeanInterceptor()); } @Override public void configHandler(Handlers me) { } } public class SqlSessionManager { private static final SqlSessionManager sqlSessionManager = new SqlSessionManager(); private SqlSession sqlSession; public SqlSession getSqlSession() { return sqlSession; } public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } public static SqlSessionManager getSqlsessionmanager() { return sqlSessionManager; } private SqlSessionManager(){ } public <T> T getMapper(Class<T> clazz){ return sqlSession.getMapper(clazz); } } public class MyBatisPlugin implements IPlugin{ private static final Log LOG = Log.getLog(MyBatisPlugin.class); private String mybatisConfigXml; private List<Class<?>> mapperClass = new ArrayList<Class<?>>(); private static final SqlSessionManager sqlSessionManager = SqlSessionManager.getSqlsessionmanager(); public MyBatisPlugin(String mybatisConfigXml){ this.mybatisConfigXml = mybatisConfigXml; } public boolean start() { SqlSession sqlSession = getSqlSession(); sqlSessionManager.setSqlSession(sqlSession); return true; } public void addMapper(Class<?> type){ mapperClass.add(type); } /** * 获取SqlSession * @return */ private SqlSession getSqlSession(){ Reader reader = null; try { reader = Resources.getResourceAsReader(mybatisConfigXml); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); Configuration configuration = sqlSessionFactory.getConfiguration(); for (Class<?> mapper: mapperClass) {//注册接口 //不知道此处是什么原因,服务器停止之后 configuration.getMapperRegistry()仍存在之前添加的mapper代理对象 //暂时没办法,只能这样搞了,希望有大神帮忙解决下 if(configuration.getMapperRegistry().hasMapper(mapper)){ continue; } configuration.addMapper(mapper); } // 通过sqlSessionFactory打开一个数据库会话 SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } catch (IOException e) { LOG.error("can not get sqlSession",e); } return null; } public boolean stop() { sqlSessionManager.getSqlSession().close(); return true; } } /** * mappr接口代理类注入 * @author zengjintao * @version 1.0 * @createTime 2018年6月3日上午11:09:21 */ public class MapperBeanInterceptor implements Interceptor{ private SqlSession sqlSession = SqlSessionManager.getSqlsessionmanager().getSqlSession(); @Override public void intercept(Invocation inv) { Controller controller = inv.getController(); Field[] fields = controller.getClass().getDeclaredFields(); for (Field field : fields) { Object bean = null; if (field.isAnnotationPresent(Inject.class)){ try { if(field.getType().isInterface()){ bean = sqlSession.getMapper(field.getType()); } } catch (Exception e) { throw new NullPointerException("can not get proxy bean"); } } try { if (bean != null) { field.setAccessible(true); field.set(controller, bean); } } catch (Exception e) { throw new RuntimeException(e); } } inv.invoke(); } } 好了,以上代码完成mybatis和jfinal整合,下面上使用实例,首先mybatis配置文件 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 打印查询语句 --> <settings> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <typeAliases> <package name="com.jfinal.mybatis.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/information"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="SystemAdminMapper.xml"/> </mappers> </configuration> <?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.jfinal.mybatis.mapper.SystemAdminMapper"> <resultMap type="SystemAdmin" autoMapping="true" id="SystemAdminResult"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="login_name" jdbcType="VARCHAR" property="loginName"/> </resultMap> <select id="getSystemAdmin" resultMap="SystemAdminResult" > select * from system_admin where 1=1 <if test="loginName != null and loginName !=''"> and login_name=#{loginName} </if> </select> </mapper> public class IndexController extends Controller{ @Inject private SystemAdminMapper systemAdminMapper; public void index(){ SystemAdmin systemAdmin = new SystemAdmin(); systemAdmin.setLoginName("admin"); renderJson(systemAdminMapper.getSystemAdmin(systemAdmin) ); } }