摘要:本文介绍了如何在SpringBoot中整合Mybatis
整合MyBatis
添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
|
在application.properties中进行配置
1 2 3 4 5 6
| spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.username=root spring.datasource.password=11215858 spring.datasource.url=jdbc:mysql://localhost:3306/javaboy?serverTimezone=UTC
|
创建User实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.example.mybatis.bean;
public class User { private Integer id; private String name; private String address;
@Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", address='" + address + '\'' + '}'; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; } }
|
创建UserMapper接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.example.mybatis.mapper;
import com.example.mybatis.bean.User; import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface UserMapper { List<User> selectAllUser(); }
|
创建UserMapper.xml配置文件
1 2 3 4 5 6 7 8 9
| <?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.example.mybatis.mapper.UserMapper"> <select id="selectAllUser" resultType="com.example.mybatis.bean.User"> select * from user; </select> </mapper>
|
那么创建了XML文件后,如何告诉SpringBoot该配置文件在哪里呢?可以在主类中添加@MapperScan注解。
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.example.mybatis;
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @MapperScan(basePackages = "com.example.mybatis.mapper") public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } }
|
在Test中测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.example.mybatis;
import com.example.mybatis.bean.User; import com.example.mybatis.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest class MybatisApplicationTests {
@Autowired UserMapper userMapper;
@Test void contextLoads() { List<User> userList = userMapper.selectAllUser(); System.out.println(userList); } }
|
MyBatis多数据源
在application.properties中进行配置
1 2 3 4 5 6 7 8 9
| spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.one.username=root spring.datasource.one.password=11215858 spring.datasource.one.url=jdbc:mysql://localhost:3306/javaboy?serverTimezone=UTC
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.two.username=root spring.datasource.two.password=11215858 spring.datasource.two.url=jdbc:mysql://localhost:3306/javaboy2?serverTimezone=UTC
|
创建DataSource配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.example.mybatis2.config;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration public class DataSourceConfig {
@Bean @ConfigurationProperties(prefix = "spring.datasource.one") DataSource dataSourceOne(){ return DruidDataSourceBuilder.create().build(); }
@Bean @ConfigurationProperties(prefix = "spring.datasource.two") DataSource dataSourceTwo(){ return DruidDataSourceBuilder.create().build(); } }
|
创建MyBatis配置类
两个数据源需要分别创建一个配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package com.example.mybatis2.config;
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource; import javax.sql.DataSource;
@Configuration @MapperScan(basePackages = "com.example.mybatis2.mapperOne", sqlSessionFactoryRef = "sqlSessionFactoryOne", sqlSessionTemplateRef = "sqlSessionTemplateOne") public class MybatisConfigOne {
@Resource(name = "dataSourceOne") DataSource dataSource;
@Bean SqlSessionFactory sqlSessionFactoryOne(){ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); try { bean.setDataSource(dataSource); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return null; }
@Bean SqlSessionTemplate sqlSessionTemplateOne(){ return new SqlSessionTemplate(sqlSessionFactoryOne()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package com.example.mybatis2.config;
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource; import javax.sql.DataSource;
@Configuration @MapperScan(basePackages = "com.example.mybatis2.mapperTwo", sqlSessionFactoryRef = "sqlSessionFactoryTwo", sqlSessionTemplateRef = "sqlSessionTemplateTwo") public class MybatisConfigTwo {
@Resource(name = "dataSourceTwo") DataSource dataSource;
@Bean SqlSessionFactory sqlSessionFactoryTwo(){ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); try { bean.setDataSource(dataSource); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return null; }
@Bean SqlSessionTemplate sqlSessionTemplateTwo(){ return new SqlSessionTemplate(sqlSessionFactoryTwo()); } }
|
创建Mapper接口以及XML配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.example.mybatis2.mapperOne;
import com.example.mybatis2.bean.User; import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface UserMapperOne { List<User> selectAll(); }
|
1 2 3 4 5 6 7 8 9
| <?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.example.mybatis2.mapperOne.UserMapperOne"> <select id="selectAll" resultType="com.example.mybatis2.bean.User"> select * from user; </select> </mapper>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.example.mybatis2.mapperTwo;
import com.example.mybatis2.bean.User;
import java.util.List;
public interface UserMapperTwo { List<User> selectAll(); }
|
1 2 3 4 5 6 7 8 9
| <?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.example.mybatis2.mapperTwo.UserMapperTwo"> <select id="selectAll" resultType="com.example.mybatis2.bean.User"> select * from user; </select> </mapper>
|
在Test中进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.example.mybatis2;
import com.example.mybatis2.bean.User; import com.example.mybatis2.mapperOne.UserMapperOne; import com.example.mybatis2.mapperTwo.UserMapperTwo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest class Mybatis2ApplicationTests { @Autowired UserMapperOne userMapperOne;
@Autowired UserMapperTwo userMapperTwo;
@Test void contextLoads() { List<User> users1 = userMapperOne.selectAll(); System.out.println(users1);
List<User> users2 = userMapperTwo.selectAll(); System.out.println(users2); } }
|