Mybatis详细描述:
MyBatis 本是apache的一个开源项目iBatis改名而来,是属于基于JAVA的一个持久层框架。框架包括sql maps和DAO (Data Access Objects) 数据访问对象‘的’第一个面向对象接口。DAO显露了 Microsoft Jet 数据库引擎(由 Microsoft Access 所使用),并允许 Visual Basic 开发者通过 ODBC 象直接连接到其他数据库一样,直接连接到 Access 表。
MyBatis参考资料官网:https://mybatis.github.io/mybatis-3/zh/index.html
直接上代码,虚的太多也不好!
项目结构:
pom,xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
首先我们先来了解单数据的配置,直接配置yml文件即可:
spring:
application:
name: consolemybatis
datasource:
url: jdbc:mysql://127.0.0.1:13306/apcons?autoReconnect=true&useUnicode=true&characterEncoding=utf8
username: root
password: a
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
resources:
add-mappings: false
mvc:
throw-exception-if-no-handler-found: true
#mysql
mybatis:
mapperLocations: classpath:mapper/**.xml
executorType: SIMPLE
具体意思我们来看看图吧
说完单数据源配置,我们来看下多数据源的配置:
@Configuration
@EnableTransactionManagement(order = 2)//由于引入多数据源,所以让spring事务的aop要在多数据源切换aop的后面
@MapperScan(basePackages = {"com.tom.guns.dao.auth"}, sqlSessionTemplateRef = "authSqlSessionTemplate")
public class AuthDatasourceConfig {
@Autowired
AuthProperties authProperties;
@Bean("authDataSource")
public DataSource authDataSource(){
DruidDataSource datasource = new DruidDataSource();
authProperties.config(datasource);
return datasource;
}
@Bean("authTransactionManager")
public PlatformTransactionManager authTransactionManager(@Qualifier("authDataSource") DataSource dataSource) throws SQLException {
return new DataSourceTransactionManager(dataSource);
}
@Bean("authSqlSessionFactory")
public SqlSessionFactory authSqlSessionFactory(@Qualifier("authDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/auth/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "authSqlSessionTemplate")
public SqlSessionTemplate authSqlSessionTemplate(@Qualifier("authSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
配置类在这里(至于变量什么的就自己看着办了):
@Component
@ConfigurationProperties(prefix = "auth.datasource")
public class AuthProperties {
private String url;
private String username;
private String password;
private String driverClassName = "com.mysql.jdbc.Driver";
private Integer initialSize = 2;
private Integer minIdle = 1;
private Integer maxActive = 20;
private Integer maxWait = 60000;
private Integer timeBetweenEvictionRunsMillis = 60000;
private Integer minEvictableIdleTimeMillis = 300000;
private String validationQuery = "SELECT 'x'";
private Boolean testWhileIdle = true;
private Boolean testOnBorrow = false;
private Boolean testOnReturn = false;
private Boolean poolPreparedStatements = true;
private Integer maxPoolPreparedStatementPerConnectionSize = 20;
private String filters = "stat";
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void config(DruidDataSource dataSource) {
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
dataSource.setInitialSize(initialSize); //定义初始连接数
dataSource.setMinIdle(minIdle); //最小空闲
dataSource.setMaxActive(maxActive); //定义最大连接数
dataSource.setMaxWait(maxWait); //最长等待时间
// 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// 配置一个连接在池中最小生存的时间,单位是毫秒
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
// 打开PSCache,并且指定每个连接上PSCache的大小
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
如果是多数据源,那就按这个模板再来一套,但是mapper的对应目录要找对:
调用方式我就不多说了,自行领会。
本章也到此完结了,是不是很简单呀,其中的坑还是要自行去踩,踩踩更健康哈哈哈。