一、mybatis 的mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="dialect" value="mysql"/>
</properties>
<settings>
<!--使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!-- 开启驼峰匹配 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 这个配置使全局的映射器启用或禁用缓存。系统默认值是true,设置只是为了展示出来 -->
<setting name="cacheEnabled" value="true"/>
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 系统默认值是true,设置只是为了展示出来 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)。 系统默认值是true,设置只是为了展示出来 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!--使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。 系统默认值是true,设置只是为了展示出来 -->
<setting name="useColumnLabel" value="true"/>
<!--允许 JDBC 支持生成的键。需要适合的驱动。如果设置为 true 则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如
Derby)。 系统默认值是false,设置只是为了展示出来 -->
<setting name="useGeneratedKeys" value="false"/>
<!--配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 系统默认值是SIMPLE,设置只是为了展示出来 -->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!--设置超时时间,它决定驱动等待一个数据库响应的时间。 系统默认值是null,设置只是为了展示出来 -->
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<!-- 分页助手 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 数据库方言 -->
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="true"/>
<!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 -->
<property name="rowBoundsWithCount" value="true"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="true"/>
</plugin>
</plugins>
</configuration>
二、引入依赖jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
三、mybatis在application.properties中配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wechat?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=root
#Mybatis
# 加载全局的配置文件
mybatis.configLocation=classpath:mybatis-config.xml
# 配置类型别名
mybatis.typeAliasesPackage=com.hhy.entity
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mybatis.mapperLocations=classpath:mapper/*.xml
四、C3P0数据源DataSource配置
@Configuration
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.username}")
private String jdbcUserName;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(name = "dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUserName);
dataSource.setPassword(jdbcPassword);
//关闭连接后不自动commit
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
}
五、mybatis 相关配置
@Configuration
@Slf4j
@EnableTransactionManagement
@MapperScan(basePackages = {MybatisConfiguration.PACKAGE}, sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfiguration implements TransactionManagementConfigurer {
static final String PACKAGE = "com.hhy.dao";
@Autowired
private DataSource dataSource;
//配置类型别名
@Value("${mybatis.typeAliasesPackage}")
private String typeAliasesPackage;
//配置mapper的扫描,找到所有的mapper.xml映射文件
@Value("${mybatis.mapperLocations}")
private String mapperLocations;
//加载全局的配置文件
@Value("${mybatis.configLocation}")
private String configLocation;
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactoryBean sqlSessionFactoryMaster() throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
// 读取配置
sessionFactoryBean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
//添加插件 (改为使用配置文件加载了)
// sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper()});
return sessionFactoryBean;
}
/**
* 开启事务注解
* @return
*/
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}