SpringBoot集成MyBatis的Bean配置方式
SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis
springboot的yml文件
spring:
profiles:
active: dev
application:
name: service-fishkk
##mysql
datasource:
url: jdbc:mysql://47.94.200.0:3306/mybatis?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&userUnicode=true
username: fishkk
password: Root.123
driver-class-name: com.mysql.jdbc.Driver
##redis路径配置
redis:
host: 47.94.200.0
port: 6379
database: 0
password: 123456
pool:
max-active:8
##cloud 的配置 配置
cloud:
config:
uri: http://localhost:8888/
profile: dev
label: master
##注册微服务
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
##Mybatis配置
mybatis:
type-aliases-package: com.boss.hr.train.fishkkmybatis.entity
配置SessionFactory和数据库连接池
package com.boss.hr.train.fishkkmybatis.config.dao;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.validation.Valid;
import java.beans.PropertyVetoException;
/**
*数据库资源连接的配置
*
*@author fishkk
*@version 1.0.0
*@since
*
* 修改人信息
*@author
*@version
*@since
*
*/
@Configuration
@MapperScan("com.boss.hr.train.fishkkmybatis.dao")
public class DataSourceConfiguration {
/**
* 数据库驱动
*/
@Value("${spring.datasource.driver-class-name}")
private String jdbcDriver;
/**
* 数据库地址
*/
@Value("${spring.datasource.url}")
private String jdbcUrl;
/**
* 数据库连接名
*/
@Value("${spring.datasource.username}")
private String jdbcUsername;
/**
* 数据库连接密码
*/
@Value("${spring.datasource.password}")
private String jdbcPassword;
/**
* 返回数据库连接池
*/
@Bean(name = "dataSource")
public ComboPooledDataSource createDateSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUsername);
dataSource.setPassword(jdbcPassword);
//关闭连接后不自动commit
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
}
@Value是通过从yml文件获取参数信息来配置
package com.boss.hr.train.fishkkmybatis.config.dao;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
/**
* Session工厂的配置文件
*
*@author fishkk
*@version 1.0.0
*@since
*
* 修改人信息
*@author
*@version
*@since
*
*/
@Configuration
public class SessionFactoryConfiguration {
/**
* 自动装在数据库资源bean
*/
@Resource
private DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactory() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//Mybatis的参数配置
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//启用Mybatis的全部xml文件,就不需要一个个去打开
String packageSerchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/mapper/**.xml";
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSerchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
//实体类所在的包
sqlSessionFactoryBean.setTypeAliasesPackage("com.boss.hr.train.fishkkmybatis.entity");
return sqlSessionFactoryBean;
}
}
通过上面的两个Bean就可以成功的使用Mybatis来对数据库进行操作了
mapper下就是和dao层对于的数据库操作语句
mybatis-config.xml是mybatis的参数配置,可以开启驼峰,设置编码等等。