# Spring boot工程应用mybatis ### 引入依赖,完成配置 #### 依赖包 mybatis开发团队为Spring Boot 提供了 `mybatis-spring-boot-starter`。你需要引入如下依赖: ```xml org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 ``` ​ 使用了该starter之后,只需要定义一个`DataSource`即可,它会自动利用该`DataSource`创建需要使用到的`SqlSessionFactoryBean`、`SqlSessionTemplate`、以及`ClassPathMapperScanner`来自动扫描你的映射器接口,并针对每个接口都创建一个`MapperFactoryBean`,注册到Spring上下文中。 ​ 关于`mybatis-spring-boot-starter`如何实现自动配置的相关源码,参见: `org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration`类。 ​ 默认情况下,扫描的`basePackage`是`spring boot`的根目录(这里指的是应用启动类Application.java类所在的目录),且只会对添加了@Mapper注解的映射器接口进行注册。 #### 连接信息配置 `application.yml`中进行数据源的相关配置即可,以下配置依赖于`spring-boot-starter-jdbc`: ```yaml server: port: 9000 spring: datasource: url: jdbc:mysql://localhost:3306/demo?allowMultiQueries=true&characterEncoding=UTF-8&autoReconnect=true&rewriteBatchedStatements=true username: root password: root driver-class-name: com.mysql.jdbc.Driver ``` #### MybatisConfiguration ```java 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()); } } ``` ### 数据库 ```sql 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 ``` ### IDEA 自动生成相应文件 - 1、安装插件`better-mybatis-generator` - 2、功能`database tool` 配置连接数据库,选择`table`,右键,选择`mybatis generate`,打开预览界面,填好相应配置信息,即可。 - `model folder`:`xxx.java` - `java` 表的对象、`xxxExample.java` - `Criteria` 过滤条件。 - `dao folder` :`xxxDao.java` - `java` 对象方法与 `xml sql` 语句之间的关系,增删改查的方法。 - `xml folder` : `xxxDao.xml` - 数据库语句与对象之间的映射关系,可修改`SQL`语句。 ### 业务实现,增删改查 ```java // 实例化操作对象 @Autowired private xxxDao xxxDao; // select xxxExample xxxExample = new xxxExample(); xxxExample.Criteria criteria = xxxExample.createCriteria(); criteria.andNameEqualTo(name); // 设置筛选条件 List 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); ``` ### 参考如下 [**MYBATIS中文教程**](http://www.tianshouzhi.com/api/tutorials/mybatis/392) [Spring Boot教程 - 整合Mybatis(注解方式)](https://www.jianshu.com/p/688f511acc1d) [idea插件better mybatis generator自动生成代码](https://blog.csdn.net/wangduduzuishuai/article/details/88419095)