1、添加必要依赖
<dependencies>
...
<!-- mybatis 与 spring boot 整合依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql 驱动,注意确保所用驱动满足你使用的数据库版本 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- druid 驱动 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
2、配置数据库链接属性
在application.yml文件中进行如下配置
...
mybatis:
# 注册映射文件
mapper-locations: classpath:com/blairscott/dao/*.xml
# 注册实体类别名
type-aliases-package: com.blairscott.bean
# 注册数据源,注意:不同的MySQL版本,该设置可能不同
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_name?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
3、创建测试表与测试Bean
- 创建测试表
DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(16) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 创建测试实体
package com.blairscott.bean; import lombok.Data; @Data public class Teacher { private int id; private String name; private int age; }
4、编写DAO并注册资源目录
-
创建TeacherDao接口与映射文件
package com.blairscott.dao; import com.blairscott.bean.Teacher; import org.apache.ibatis.annotations.Mapper; @Mapper public interface TeacherDao { void insertTeacher(Teacher teacher); }
<!-- TeacherDao.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.blairscott.dao.TeacherDao"> <insert id="insertTeacher"> INSERT INTO teacher (name, age) VALUES (#{name}, #{age}) </insert> </mapper>
-
注册dao包下mybatis映射文件为资源目录
<!-- pom.xml --> <build> ... <resources> ... <!-- 注册dao包下mybatis映射文件为资源目录 --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
5、编写测试代码
-
服务接口与其实现类
public interface TeacherService { void addTeacher(Teacher teacher); }
@Service public class TeacherServiceImpl implements TeacherService { @Autowired private TeacherDao dao; @Override public void addTeacher(Teacher teacher) { dao.insertTeacher(teacher); } }
-
Controller类
@Controller @RequestMapping("/registry") public class MybatisController { @Autowired private TeacherService service; @PostMapping("/register") public String register(Teacher teacher, Model model) { service.addTeacher(teacher); return "/jsp/welcome.jsp"; } }
-
jsp页面
<!-- index.jsp --> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <form action="registry/register" method="post"> 姓名:<input name="name" /> 年龄:<input name="age" /> <input type="submit" value="注册" /> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 欢迎新教师:teacher = ${teacher} </body> </html>
6、测试