在config包中建一个类mybatisconfig放
package com.hgkj.post.config;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.transaction.ChainedTransactionManager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import com.jolbox.bonecp.BoneCPDataSource;
@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:jdbc.properties" })
public class MyBatisConfig implements TransactionManagementConfigurer{
@Value("${mysql.jdbc.url}")
private String mysqlJdbcUrl;
@Value("${mysql.jdbc.driverClassName}")
private String mysqlJdbcDriverClassName;
@Value("${mysql.jdbc.username}")
private String mysqlJdbcUsername;
@Value("${mysql.jdbc.password}")
private String mysqlJdbcPassword;
@Bean(destroyMethod = "close")
@Primary
public DataSource mysqlDataSource() throws Exception {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
// 数据库驱动
boneCPDataSource.setDriverClass(mysqlJdbcDriverClassName);
// 相应驱动的jdbcUrl
boneCPDataSource.setJdbcUrl(mysqlJdbcUrl);
// 数据库的用户名
boneCPDataSource.setUsername(mysqlJdbcUsername);
// 数据库的密码
boneCPDataSource.setPassword(mysqlJdbcPassword);
// 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
// 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
boneCPDataSource.setIdleMaxAgeInMinutes(30);
// 每个分区最大的连接数
boneCPDataSource.setMaxConnectionsPerPartition(10);
// 每个分区最小的连接数
boneCPDataSource.setMinConnectionsPerPartition(2);
boneCPDataSource.setIdleConnectionTestPeriodInSeconds(30);
boneCPDataSource.setConnectionTestStatement(" SELECT 1 FROM DUAL ");
return boneCPDataSource;
}
@Bean(name="mysqlSqlSessionFactoryBean")
@Primary
public SqlSessionFactoryBean mysqlSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(mysqlDataSource());
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mybatis/mappers/*.xml"));
return sqlSessionFactoryBean;
}
public PlatformTransactionManager annotationDrivenTransactionManager() {
DataSourceTransactionManager mysql_dtm = null;
try {
mysql_dtm = new DataSourceTransactionManager(mysqlDataSource());
} catch (Exception e) {
e.printStackTrace();
}
ChainedTransactionManager ctm = new ChainedTransactionManager(mysql_dtm);
return ctm;
}
}
里面的@value{值}是从jdbc.properties中取的
jdbc.properties:
# 告诉java去com.mysql.jdbc路径下将Driver类加载到jvm内存中com.mysql.jdbc.Driver/com.mysql.cj.jdbc.Driver
#驱动
mysql.jdbc.driverClassName=com.mysql.jdbc.Driver
mysql.jdbc.url=jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&serverTimezone=Asia/Shanghai&useSSL=false
mysql.jdbc.username=数据库账户
mysql.jdbc.password=数据库密码
也是在config文件夹中新建类MyBatisMapperScannerConfig来扫描接口并自动装配
package com.hgkj.post.config;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mysqlMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.***.***.dao");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("mysqlSqlSessionFactoryBean");
mapperScannerConfigurer.setAnnotationClass(MysqlRepository.class);
return mapperScannerConfigurer;
}
}
也是在config文件夹中新建类MysqlRepository要在接口上调用
public @interface MysqlRepository {
}
如果只是仅连接,前面两个就够了,如果要写mapper就加上后面两个截图,然后在每个接口上写一个@MysqlRepository,就可以直接在mapper写语句了