1. Spring boot工程应用mybatis

1.1. 引入依赖,完成配置

1.1.1. 依赖包

mybatis开发团队为Spring Boot 提供了 mybatis-spring-boot-starter。你需要引入如下依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

​ 使用了该starter之后,只需要定义一个DataSource即可,它会自动利用该DataSource创建需要使用到的SqlSessionFactoryBeanSqlSessionTemplate、以及ClassPathMapperScanner来自动扫描你的映射器接口,并针对每个接口都创建一个MapperFactoryBean,注册到Spring上下文中。

​ 关于mybatis-spring-boot-starter如何实现自动配置的相关源码,参见:

org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration类。

​ 默认情况下,扫描的basePackagespring boot的根目录(这里指的是应用启动类Application.java类所在的目录),且只会对添加了@Mapper注解的映射器接口进行注册。

1.1.2. 连接信息配置

application.yml中进行数据源的相关配置即可,以下配置依赖于spring-boot-starter-jdbc

server:
  port: 9000
  
spring:
    datasource:
       url: jdbc:mysql://localhost:3306/demo?allowMultiQueries=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;rewriteBatchedStatements=true
       username: root
       password: root
       driver-class-name: com.mysql.jdbc.Driver

1.1.3. MybatisConfiguration

package springboot.tutorials.mybatis.annotation.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;

/**
 * @author Ricky Fung
 */
@Configuration
@MapperScan(basePackages = "springboot.tutorials.mybatis.annotation.mapper",
        sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfiguration {

    static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String user;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MAPPER_LOCATION));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

1.2. 数据库

CREATE TABLE `user` (
  `id` int(13) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(33) DEFAULT NULL COMMENT '姓名',
  `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

1.3. IDEA 自动生成相应文件

  • 1、安装插件better-mybatis-generator

  • 2、功能database tool 配置连接数据库,选择table,右键,选择mybatis generate,打开预览界面,填好相应配置信息,即可。

    • model folderxxx.java - java 表的对象、xxxExample.java - Criteria 过滤条件。

    • dao folderxxxDao.java - java 对象方法与 xml sql 语句之间的关系,增删改查的方法。

    • xml folderxxxDao.xml - 数据库语句与对象之间的映射关系,可修改SQL语句。

1.4. 业务实现,增删改查

// 实例化操作对象
@Autowired
private xxxDao xxxDao;

//  select
xxxExample xxxExample = new xxxExample();
xxxExample.Criteria criteria = xxxExample.createCriteria();
criteria.andNameEqualTo(name); // 设置筛选条件
List<xxx> xxxs = this.xxxDao.selectByExample(xxxExample);

// insert
xxx xxxInsert2DB = new xxx();
xxxInsert2DB.setName(name);
this.xxxDao.insert(xxxInsert2DB);

// update 多种选择
xxx xxxUpdate2DB = new xxx();
xxxUpdate2DB.setName(name);
this.xxxDao.updateByPrimaryKeySelective(xxxUpdate2DB);


// delete
xxxExample xxxExample = new xxxExample();
xxxExample.Criteria criteria = xxxExample.createCriteria();
criteria.andNameEqualTo(name); // 设置筛选条件
this.xxxDao.deleteByExample(xxxExample);