一,新建一个Maven项目搭建SpringBoot框架,引入MyBatis持久层与Druid连接池
**1,新建一个Maven项目,继承springboot框架,引入所需依赖pom.xml**
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>mybatistest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatistest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--继承springboot框架-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 引入切面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--阿里巴巴Druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 阿里巴巴fastjson工具 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
<!--MySQL数据库JDBC驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- MyBatis持久层依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
2,填写Springboot项目启动入口类
package com.mybatistest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3,编写Controller组件,Service组件
package com.mybatistest.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.mybatistest.sys.annotation.LoggerAnnotation;
import com.mybatistest.test.service.TestService;
@RestController
public class TestController {
@Autowired
TestService testService;
@RequestMapping("test0")
@LoggerAnnotation("测试欢迎页面")//此注解是自定义的,具体见详细代码
public String test0() {
return "Hello World";
}
@RequestMapping("test1")
@LoggerAnnotation("查询chapter01全部书本")//此注解是自定义的,具体见详细代码
public String test1() {
return JSON.toJSONString(testService.queryAllBooks());
}
@RequestMapping("test2")
@LoggerAnnotation("查询chapter02全部书本")//此注解是自定义的,具体见详细代码
public String test2() {
return JSON.toJSONString(testService.queryAllBooks2());
}
}
package com.mybatistest.test.service;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.mybatistest.test.dao.TestDao;
import com.mybatistest.test.mapper.TestMapper;
@Service
public class TestService {
@Autowired
TestMapper testMapper;
@Autowired
TestDao testDao;
public Object queryAllBooks() {
return testMapper.queryAllBooks();
}
public Object queryAllBooks2() {
return testDao.queryAllBooks();
}
}
二,编写MyBatis配置项
1,新建一个Class,编写第一个数据源配置
package com.mybatistest.sys.config;
import javax.sql.DataSource;
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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages="com.mybatistest.test.mapper",sqlSessionFactoryRef="ssfone")
//basePackages,该数据源扫描MapperBean
//sqlSessionFactoryRef,该数据源的SqlSessionFactory
public class DruidDatasourceConfigOne {
@Bean(name="dsone")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/chapter01");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean(name="ssfone")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dsone")DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/one/*.xml"));
return bean.getObject();
}
@Bean(name="sstone")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("ssfone")SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
}
2,新建一个Class,配置第二个数据源
package com.mybatistest.sys.config;
import javax.sql.DataSource;
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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages="com.mybatistest.test.dao",sqlSessionFactoryRef="ssftwo")
public class DruidDatasourceConfigTwo {
@Bean("dstwo")//不可以添加Primary注解
public DataSource dataSource() {
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/chapter02");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean("ssftwo")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dstwo")DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/two/*.xml"));
return bean.getObject();
}
@Bean("ssttwo")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("ssftwo")SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
}
3,在对应地址下新建mapper接口
package com.mybatistest.test.mapper;
//对应第一个数据源
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
public interface TestMapper {
List<Map<String, Object>> queryAllBooks();
}
package com.mybatistest.test.dao;
//对应第二个数据源
import java.util.List;
import java.util.Map;
public interface TestDao {
List<Map<String, Object>> queryAllBooks();
}