一、依赖
导入部分依赖,其他的自己加
<!--springboot 版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot-start-version>2.2.13.RELEASE</spring-boot-start-version>
</properties>
<!-- Spring-Mybatis Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--MyBatis Pagehelper 分页组件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
<exclusions>
<exclusion>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 不知道为什么pagehelper-spring-boot-starter导不进了,只能另外导入了 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring-boot-start-version}</version>
</dependency>
<!-- MySQL数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
二、属性配置
@Configuration
@ConfigurationProperties(prefix = "spring.mysql.datasource")
@MapperScan(basePackages = "com.mapper.mysql",sqlSessionFactoryRef = "mysqlSqlSessionFactroy")
public class MySqlDataSourceConfig {
private String driverClass;
private String userName;
private String password;
private String url;
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUrl(String url) {
this.url = url;
}
//.....
三、配置数据源
//配置数据源
@Bean(name = "mysqlDataSource")
public DataSource getMysqlDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setPassword(password);
dataSource.setUsername(userName);
dataSource.setUrl(url);
System.out.println("mysql数据源:"+dataSource);
return dataSource;
}
四、配置会话
//配置会话管理
@Bean(name = "mysqlSqlSessionFactroy")
public SqlSessionFactory getMySqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
// sessionFactoryBean.setConfigLocation(new ClassPathResource("config/*.xml")); //配置文件
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysql/*.xml"));
return sessionFactoryBean.getObject();
}
//会话模板,令会话线程安全
@Bean(name = "mysqlSqlSessionTemplate")
public SqlSessionTemplate getMySqlSessionTemplate(@Qualifier("mysqlSqlSessionFactroy") SqlSessionFactory sessionFactory){
return new SqlSessionTemplate(sessionFactory);
}
五、事务管理
@Bean(name = "dataSourceTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
六、测试
@Test
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
public void test1(){
int row = mysqlMapper.updateObject();
System.out.println("查看返回结果:"+row);
throw new RuntimeException("异常");
}
七、备注
mybatis-spring:http://mybatis.org/spring/zh/index.html
基本配置可参考以上网址