一、导入相关包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis.starter依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.starter.version}</version>
</dependency>
<!-- lombok 工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.starter.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
二、配置文件
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql///springboot?
# 数据库连接池
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 最大连接池数量
max-active: 20
# 初始化连接池的数量
initial-size: 5
# 最小连接池 数量
min-idle: 2
# 这里建议配置为TRUE,防止取到的连接不可用
test-on-borrow: true
test-on-return: false
# 验证连接有效与否的SQL,不同的数据配置不同
validation-query: select
#通过别名的方式配置扩展插件,常用的插件有:
#监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
filters: stat,slf4j,wall
# 配置获取连接等待超时的时间 单位毫秒
max-wait: 6000
# mybatis 配置
mybatis:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.vp.example.entity
# settings 下的配置
configuration:
# 开启驼峰命名功能 其它的一些属性参考 mybatis-config.xml中的settings属性
map-underscore-to-camel-case: true
三、主程序入口类开启扫描
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
@SpringBootApplication
// @MapperScan("com.xxx.xxx.mapper")
// 扫描多个包
// @MapperScans({"com.xxx.xxx.mapper",""})
@MapperScan("com.vp.example.mapper")
public class ShiroApplication {
public static void main(String[] args) {
SpringApplication.run(ShiroApplication.class, args);
}
}
四、核心代码
1、建表sql
CREATE TABLE test(
tid INT AUTO_INCREMENT PRIMARY KEY ,
title VARCHAR(100)
)
2、实体类
@Data
public class Test {
public int tid;
public String title;
}
3、UserMapper接口
public interface TestMapper {
int insert(Test record);
}
4、UserMapper.xml
<?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.vp.example.mapper.TestMapper">
<resultMap id="BaseResultMap" type="com.vp.example.entity.Test">
<id column="tid" jdbcType="INTEGER" property="tid" />
<result column="title" jdbcType="VARCHAR" property="title" />
</resultMap>
<insert id="insert" keyColumn="tid" keyProperty="tid" parameterType="com.vp.example.entity.Test" useGeneratedKeys="true">
insert into test (title)
values (#{title,jdbcType=VARCHAR})
</insert>
</mapper>
5、测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MapperTest {
@Resource
TestMapper testMapper;
@Test
public void insert() {
TestBean bean = new TestBean();
bean.setTitle("mybatis");
testMapper.insert(bean);
}
}