Mybatis源码 Mybatis是如何与spring框架整合的
Posted Dream_it_possible!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis源码 Mybatis是如何与spring框架整合的相关的知识,希望对你有一定的参考价值。
Mybatis是现在最流行的数据库中间件之一,用mybatis的好处在于让开发者更专注在SQL本身,能与多种数据库兼容,而spring框架又是java最强大的框架,对于java来讲,万物是基于spring 的,因此我们有必要去理解mybatis 是如何整合spring框架,如果我们理解了怎么整合spring框架,那么怎么整合springboot框架就更容易理解, 那么本文就spring 整合mybatis原理进行深度解析。
一、环境准备
应用 | 版本号 |
spring-mybatis | 2.0.0 |
mybatis | 3.5.0 |
jdk | 1.8 |
mysql server | 5.7 |
mysql-connector | 5.1.17 |
spring-jdbc | 5.1.5.RELEASE |
spring-context | 5.1.5.RELEASE |
mybatis-spring-boot-starter | 1.3.2 |
github地址: GitHub - mybatis/spring: Spring integration for MyBatis 3
首先我用spring-mybatis 实现spring应用下整合mybatis的demo, 先会用再去学习源码。
二、Spring 整合mybatis
新建一个maven工程,不使用spring intializer创建,本篇通过注解来整合,也可以通过xml整合,工程目录:
添加依赖
在pom.xml文件里添加依赖:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
配置数据源、mapper和mapper.xml
配置数据源, 用db.properites文件记录即可:
spring.datasource.url=jdbc:mysql://localhost:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis需要通过配置@MapperScan扫描接口mapper,才能创建接口的代理对象, 通过SqlSessionFactoryBean 设置dataSource和mapperLocations才能映射数据库和xml文件。
package com.example.mybatis.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author bingbing
* @date 2023/2/7 0007 17:04
*/
@Configuration
@MapperScan(basePackages = "com.example.mybatis.mapper")
public class MybatisConfig
@Bean
public SqlSessionFactoryBean initBean()
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// 扫描的xml文件
factoryBean.setMapperLocations(new Resource[]new ClassPathResource("UserMapper.xml"));
// note running without SqlSessionFactoryBean.configLocation set => default configuration
factoryBean.setDataSource(initDataSource());
return factoryBean;
@Bean(name = "initDataSource")
public DataSource initDataSource()
Properties properties = new Properties();
InputStream is = ClassLoader.getSystemResourceAsStream("db.properties");
try
properties.load(is);
catch (IOException e)
e.printStackTrace();
String url = properties.getProperty("spring.datasource.url");
String username = properties.getProperty("spring.datasource.username");
String password = properties.getProperty("spring.datasource.password");
String driverClassName = properties.getProperty("spring.datasource.driver-class-name");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
启动报错:
Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/ibatis/logging/LogFactory
经检查,发现缺少mybatis依赖,添加mybatis依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
启动测试:
public static void main(String[] args)
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MybatisConfig.class);
context.start();
UserMapper userMapper = context.getBean(UserMapper.class);
Integer count = userMapper.findCountUser();
System.out.println("userMapper:" + userMapper + ", 数量为: " + count);
至此spring应用整合mybatis成功,代码附在最后。
三、Spring Boot 整合mybatis
新建springboot工程, 工程目录如下:
添加依赖
核心依赖mybatis-spring-boot-starter, 注意版本与mysql-connector版本的对应关系,其实还是mybatis与mysql-connector版本的对应关系。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis整合springboot-->
<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.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置数据源、xml路径和mapper
spring.datasource.url=jdbc:mysql://localhost:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=mapper/*.xml
配置mapper的BasePackages:
@SpringBootApplication
@MapperScan(basePackages = "com.sh.bing.mapper")
public class SpringbootMybatisMavenApplication
public static void main(String[] args)
SpringApplication.run(SpringbootMybatisMavenApplication.class, args);
启动应用, 访问 http://localhost:8080/user/test
以上是关于Mybatis源码 Mybatis是如何与spring框架整合的的主要内容,如果未能解决你的问题,请参考以下文章