SpringBoot整合Mybatis

1、添加依赖

 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2、编写mapper接口和mapper.xml

mapper接口主要代码

@Repository
public interface AccountMapper {

    List<Account> selectAccountList(Account account);
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzw.module.mapper.AccountMapper">
	<resultMap type="Account" id="AccountResult">
		<id     property="id"     column="id"     />
		<result property="name"   column="name"   />
		<result property="info"  column="info"   />
	</resultMap>
	<sql id="selectAccountVo">
        select a.id, a.name, a.info
        from account a
    </sql>
    
	<select id="selectAccountList" parameterType="Account" resultMap="AccountResult">
		<include refid="selectAccountVo"></include>
		<where>
			<if test="id != null"> and id = #{id} </if>
			<if test="name != null and name != ''"> and name = #{name} </if>
			<if test="info != null and info != ''"> and info = #{info} </if>
		</where>
	</select>
</mapper> 

3、配置mybatis

我们编写好接口和xml文件,需要通过配置让mybatis知道

如何配置呢

3.1 MapperScan

配置接口的路径,让mybatis能找到所有接口

@SpringBootApplication
@MapperScan("com.zzw.**.mapper")
public class SpringbootMybatistestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatistestApplication.class, args);
    }

}

com.zzw.**.mapper是指包名满足以com.zzw开头,mapper结尾的所有包下的所有接口文件

3.2 配置mapper-locations

配置xml路径,让mybatis能找到所有xml文件

mybatis:
  mapper-locations: classpath:mybatis/**/*Mapper.xml

3.3 其它配置

这些配置是为了方便xml编写,以及mybatis的一些功能性配置

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  type-aliases-package: com.zzw.**.domain

mybatis-config.xml

<?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>
	<settings>
		<setting name="cacheEnabled"             value="true" />  <!-- 全局映射器启用缓存 -->
		<setting name="useGeneratedKeys"         value="true" />  <!-- 允许 JDBC 支持自动生成主键 -->
		<setting name="defaultExecutorType"      value="REUSE" /> <!-- 配置默认的执行器 -->
		<setting name="logImpl"                  value="SLF4J" /> <!-- 指定 MyBatis 所用日志的具体实现 -->
		<!-- <setting name="mapUnderscoreToCamelCase" value="true"/>  驼峰式命名 -->
	</settings>
</configuration>

4、多数据源配置

4.1 添加druid数据源

依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.4</version>
</dependency>

druid配置

主要是配置多数据源。要启用多数据源,得添加多个DataSource。同时添加一个DynamicDataSource来管理多个DataSource, DynamicDataSource是通过map来管理多数据源的。所以要切数据源,只需要切数据源在DynamicDataSource里面的key就行了。而key单独用DynamicDataSourceContextHolder来管理。

  1. 在yml配置druid相关的内容
  2. 添加DruidProperties类与yml的配置内容对应,使yml的配置映射到java配置对象
  3. 使用这个配置对象配置的内容来初始化DataSource
  4. 添加DynamicDataSource类来管理多个DataSource
  5. 添加DynamicDataSourceContextHolder类来管理当前DataSource对应的key,以供切换

启动类配置:重要

由于使用我们自己定义的多数据源,所以需要排除掉SpringBoot配置的数据源
添加exclude = DataSourceAutoConfiguration.class

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@MapperScan("com.zzw.**.mapper")
public class SpringbootMybatistestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatistestApplication.class, args);
    }
}

以上是所有流程,由于具体实现的代码比较多,不便于写到文档,可以结合文档来理解代码。

项目地址:https://gitee.com/zzw_server/test_proj/tree/master/springboot-mybatistest

posted @ 2021-04-16 17:36  zzw_1987  阅读(74)  评论(0)    收藏  举报