spring boot2系列文章
- spring boot(一) 入门篇
- spring boot(二) 配置DB
- spring boot(三) Mybatis配置
- spring boot版本为2.0.3.RELEASE
- db使用mysql
- 本篇源码下载地址点我
简介
本篇主要介绍spring boot2关于mybatis的基本配置方式,阅读此章需要先了解dataSource的配置方式,可以观看我的前一篇文章 spring boot系列第二篇
目录摘要
- pom依赖
- 属性配置
- Mapper接口
- 测试类
mybatis基本配置
spring boot中配置mybatis非常方便,不需要传统的xml配置,mybatis.xml文件也可以不需要,首先引入mybatis的starter包
pom依赖包
<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>
<scope>runtime</scope>
</dependency>
只需要引入mybatis-spring-boot-starter和mysql的驱动包就可以,关于mybatis的其他包都由starter自动引入
属性配置
# 数据库datasource相关配置 属性来源于 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis配置 属性来源于org.mybatis.spring.boot.autoconfigure.MybatisProperties
mybatis.configuration.cache-enabled=false
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
mybatis.configuration.default-executor-type=reuse
mybatis.configuration.default-statement-timeout=600
MybatisProperties类,命名空间为mybatis. configuration
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis";
……
/**
* Externalized properties for MyBatis configuration.
*/
private Properties configurationProperties;
}
Mapper接口
package org.zhgs.demo.springboot.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import org.zhgs.demo.springboot.domain.User; import java.util.List;
@Mapper // mybatis注解,mybatis会自动扫描此类,也可以放在SpringBootApplication启动类中加入注解@MapperScan("org.zhgs.demo.springboot.mapper")
@Component // 定义bean注解
public interface UserMapper {
@Select("select * from user")
List<User> getAllUsers();
}
@Mapper // mybatis注解,mybatis会自动扫描此类,也可以放在SpringBootApplication启动类中加入注解
@MapperScan(“org.zhgs.demo.springboot.mapper”)
@Component // 定义bean注解
测试
新建UserController类代码如下
package org.zhgs.demo.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zhgs.demo.springboot.domain.User;
import org.zhgs.demo.springboot.mapper.UserMapper;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/get-user-list")
public List<User> queryUsers(){
return userMapper.getAllUsers();
}
}
新建SpringBootMybatisApplication启动类,注意MapperScan和Mapper的注解,只用其一就可以。
package org.zhgs.demo.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.zhgs.demo.springboot.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}