最近刚刚学习SpringBoot,所以就尝试这个把原来Spring+SpringMVC+mybatis的项目(通过xml进行整合的)环境在springboot中进行搭建,今天就简单的吧SpringBoot和mybatis的整合实例简单记录下,方便以后进行查阅,我这里呢,用的工具是idea2017,正好和SpringBoot一起进行学习。
Mybatis和Spring Boot的整合有两种方式:
第一种:使用mybatis官方提供的Spring Boot整合包实现,参考SpringBoot整合Mybatis(二)
第二种:使用mybatis-spring整合的方式,也就是我们传统的方式,我这里介绍的就是传统的方式。
一、首先我们先来回顾一下,我们原来对Spring和Mybatis整合的方式都是通过xml的方式进行配置的如下所示
1.数据库基本配置文件(这个文件时肯定要有的,SpringBoot整合也是需要用到的)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testTable?characterEncoding=utf-8
jdbc.username=root
jdbc.password=****
2.mybatis整合spring的文件applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 数据库连接池 -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lpl.mapper" />
</bean>
</beans>
二、介绍一下Spring的java配置方式
1.从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean,现在我们就处于这个时代,并且Spring4.x和Spring boot都推荐使用java配置的方式。
2.Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的:
2.1、@Configuration 作用于类上,相当于一个xml配置文件;
2.2、@Bean 作用于方法上,相当于xml配置中的<bean>;
我相信看完这段红色标记的话以后大多数朋友就知道了springboot整合mybatis的方式了。
3.1准备maven的jar(我这里提到的jar,知识整合用到的jar)
<!-- 配置事务需要用到-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- pagehelper jar包 (很好用的一个插件)-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
3.2创建一个Mybatis的注解类
@Configuration
@PropertySource("classpath:jdbc.properties")//加载的这个配置文件和原来的配置信息是一样的
public class MybatisConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
// 数据库驱动
druidDataSource.setDriverClassName(jdbcDriverClassName);
// 相应驱动的jdbcUrl
druidDataSource.setUrl(jdbcUrl);
// 数据库的用户名
druidDataSource.setUsername(jdbcUsername);
// 数据库的密码
druidDataSource.setPassword(jdbcUsername);
// 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
//druidDataSource.setIdleMaxAgeInMinutes(30);
// 每个分区最大的连接数
druidDataSource.setMaxActive(10);
// 每个分区最小的连接数
druidDataSource.setMinIdle(5);
return druidDataSource;
}
@Bean
@ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象
public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource());
// 设置mybatis的主配置文件
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
//配置mapper路径
Resource[] mapperResource = resolver.getResources("classpath:mybatis/mappers/*.xml");
sqlSessionFactoryBean.setMapperLocations(mapperResource);
// 设置别名包?
// sqlSessionFactoryBean.setTypeAliasesPackage("com.lpl.entity");
return sqlSessionFactoryBean;
}
}
3.3mybatis的配置文件(我这里是通过xml文件配置的mybatis配置信息,有人可能会说SpringBoot中不是不需要配置xml文件吗?他是不要配置spring相关的配置文件,我们这里的配置文件只是mybatis的配置文件),我这里只是配置一个分页助手,当然你还可配置其他信息,比如通用Mapper等等。。
<?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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql" />
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true" />
</plugin>
</plugins>
</configuration>
3.4创建一个Mapper接口扫描器类
@Configuration
@AutoConfigureAfter(MybatisConfig.class) //保证在MyBatisConfig实例化之后再实例化该类
public class MapperScannerConfig {
// mapper接口的扫描器
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.lpl.mapper");
return mapperScannerConfigurer;
}
}
说到这里整个SpringBoot和Mybatis的整合就算完成了!下面是我的项目结构: