标签:web项目通过listener直接加载mybatis配置文件
首先,webxml中配置listener,并配置配置文件的路径作为参数
<!-- 初始化mybatis配置文件路径 --> <context-param> <param-name>mybatisConfigLocation</param-name> <param-value>com/config/configuration.xml</param-value> </context-param> <!-- 初始化SessionFactory监听器 --> <listener> <listener-class>com.mybatis.listener.MybatisLoaderListener</listener-class> </listener>
第二步:
定义MybatisLoaderListener和MybatisLoader类,MybatisLoaderListener继承自MybatisLoader并且实现接口ServletContextListener
MybatisLoaderListener的代码如下:
package com.mybatis.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; public class MybatisLoaderListener extends MybatisLoader implements ServletContextListener { private static final Logger log = Logger.getLogger(MybatisLoaderListener.class); @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent event) { log.info("init mybatis configuration start ..."); initSqlSessionFatory(event.getServletContext()); log.info("init mybatis configuration success."); } }
MybatisLoader代码如下:
package com.mybatis.listener; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import javax.servlet.ServletContext; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.log4j.Logger; public class MybatisLoader { private static final Logger log = Logger.getLogger(MybatisLoader.class); public static final String MYBATIS_CONFIG_LOCATION = "mybatisConfigLocation"; //sql会话工厂 private static SqlSessionFactory sqlSessionFactory = null; public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } public void initSqlSessionFatory(ServletContext servletContext) { String configurationXml=servletContext.getInitParameter(MYBATIS_CONFIG_LOCATION); try { if(sqlSessionFactory == null){ //使用项目src路径(如果配置文件放到src目录下,使用以下代码加载) Reader reader = Resources.getResourceAsReader(configurationXml); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); log.debug("init sqlSessionFactory success"); //使用绝对路径(如果配置文件放到WEB-INF目录下,使用以下代码加载) //Reader reader = new FileReader(servletContext.getRealPath(configurationXml)); //sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } } catch (IOException ioex) { throw new RuntimeException("Get resource error:"+configurationXml, ioex); } } }
第三步:在Dao中使用
SqlSessionFactory sqlSessionFactory=MybatisLoader.getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); List list = sqlSession.selectList("com.foo.bean.BlogMapper.queryAllBlogInfo");
特别说明:
如果配置文件放到WEB-INF目录下,必须使用绝对路径去获取Reader,org.mybatis.io.Resources本身不不支持将配置文件放置到WEB-INF目录下;
举例
<!-- 初始化mybatis配置文件路径 -->
<context-param> <param-name>mybatisConfigLocation</param-name> <param-value>/WEB-INF/configuration.xml</param-value> </context-param>
在MybatisLoader的方法initSqlSessionFatory中使用
Reader reader = new FileReader(servletContext.getRealPath(configurationXml)); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
web项目通过listener直接加载Mybatis配置文件
标签:web项目通过listener直接加载mybatis配置文件
原文地址:http://4443915.blog.51cto.com/4433915/1827393