springboot中对sqlSessionFactoryBean自定义
1.新建一个配置类,加上configuration注解
2.定制化SqlSessionFactoryBean,然后交给容器管理
代码如下:
@Configuration public class MybatisConfig { @Value("${mybatis.mapper-locations:mapper/**.xml}") private String mapperLocations; @Value("${pageHelper.dialect:mysql}") private String dialect; @Bean public SqlSessionFactoryBean configSqlSessionFactoryBean(DataSource dataSource) throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); // 时候输出mybatis相关日志 configuration.setLogImpl(StdOutImpl.class); // 开启驼峰命名 configuration.setMapUnderscoreToCamelCase(true); sqlSessionFactoryBean.setConfiguration(configuration); sqlSessionFactoryBean.setDataSource(dataSource); // 设置pageHelper分页插件 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); // 指定数据库方言 properties.setProperty("dialect", dialect); pageHelper.setProperties(properties); sqlSessionFactoryBean.setPlugins(pageHelper); // 设置mapper.xml路径 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));// 设置mapper文件扫描路径 return sqlSessionFactoryBean; } }
以上配置也可以通过properties文件配置,如:
1 mybatis.mapper-locations=classpath:mapper/*.xml 2 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl 3 mybatis.configuration.mapUnderscoreToCamelCase=true
4 mybatis.configuration.call-setters-on-nulls=true
探索不以困难而止,学习不以艰苦而终