在单元测试或者非框架环境中手动装载mybatis
【摘要】 public static SqlSessionFactory SqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); org.apache.commons.dbcp.BasicDataSource ds = ...
public static SqlSessionFactory SqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); org.apache.commons.dbcp.BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource(); ds.setDriverClassName("net.sf.log4jdbc.sql.jdbcapi.DriverSpy"); ds.setUrl("jdbc:log4jdbc:mysql://xxx:port/xxx"); ds.setUsername("root"); ds.setPassword("xxxx"); sqlSessionFactoryBean.setDataSource(ds); sqlSessionFactoryBean.setMapperLocations( new Resource[] { new ClassPathResource("mappers\\xxx\\robotMapper.xml") } ); sqlSessionFactoryBean.setConfigLocation( new ClassPathResource("mybatis-config.xml") ); sqlSessionFactoryBean.setTypeAliasesPackage("com.pineone.icbms.so.interfaces.database.model"); return sqlSessionFactoryBean.getObject(); } SqlSessionFactory sqlSessionFactory = SqlSessionFactory(); try (SqlSession sqlSession = sqlSessionFactory.openSession()) { XXXMapper robotMapper = sqlSession.getMapper(XXXMapper.class); }
参考:
https://github.com/mybatis/mybatis-3/wiki/Unit-Test
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)