平时写作的习惯很少,这次之所以写也是因为springboot整合mybatis在网上真的众说纷纭五花八门,在实践的过程中真的碰到了很多坑,这次做个总结,方便自己,方便他人,话不多说直接上项目:
这个demo的框架是springboot+mybatis+thymleaf,项目地址:https://gitee.com/liyiming007/springboot_mybatis
如果是从0到1的过程,请参考以下内容:
第一部分.创建项目
本次创建完全依赖于idea的spring assistant插件进行自动化创建,规避了手写pom导致各个依赖版本不兼容的弊病,也是网上大多数文章的弊病。
以下是添加依赖:一共四个
1.springMVC即Web里的Spring Web
2.Thymleaf模板引擎
3.SQL里的mySql连接器以及mybatis框架
完成以上就可以下一步直至生成项目。
第二部分, mysql建库建表
mysql.zip 下载版本5以上的即可,用cmd命令完成安装,具体安装请参照
https://www.cnblogs.com/liuwei00125/p/11609862.html
可视化工具用workbench,安装这里不赘述。
完成准备工作,请新建my_sql的库并建立student的表,id字段是Integer,name字段是varchar

第三部分,补充代码完成前后交互
a.建立bean
在basePackage里新建bean包,并新建Student.java这样的一个bean,属性有两个,一个是id;用idea自动生成有参和无参两个构造方法,getter和setter以及toString方法
package com.example.hello.beans;
import org.springframework.stereotype.Component;
@Component
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
b.写完bean就该写dao了
在basePackage再建立一个dao包,里面新建xxxMapper的接口,接口只定义方法,这个方法是用来取得数据的,@Mapper注解是用于被spring扫描生成bean用的,相当于@Repository,在myBatis框架中取代了@Repository,@Mapper比它更方便。
package com.example.hello.dao;
import com.example.hello.beans.Student;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentMapper {
Student getStudentById(int id);
}
c.写完dao就该写dao的xml了,这里我们命名xml必须与对应的接口(以上)同名,且在resources中的package路径也完全相同。mapper中的namespace是接口的引用路径
<?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.example.hello.dao.StudentMapper">
<select id="getStudentById" parameterType="INTEGER" resultType="Student">
select *
from student
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
此时我们也不能少了另外一个极其重要的配置文件application.yml
#mysql连接四参数
spring:
datasource:
username: root
password: root
#连接mysql的host+库+timezone的UTC设置
url: jdbc:mysql://localhost:3306/my_sql?serverTimezone=UTC
#连接mysql的驱动
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
#mybatis扫描bean的包名,有了这个在mapper.xml里resultType就可以直接写类名
type-aliases-package: com.example.hello.beans
#mybatis扫描xml的路径
mapper-locations: classpath:com/example/hello/dao/*Mapper.xml
D.建service包再编写service层,先写接口
package com.example.hello.services;
import com.example.hello.beans.Student;
public interface StudentService {
Student getOneStudent(int id);
}
建实现类包再写实现类,注意service调用了Mapper接口的方法,所以Mapper接口需要在该类中完成自动装配成该类的一个bean,需要加@Autowired
package com.example.hello.serviceImpl;
import com.example.hello.beans.Student;
import com.example.hello.dao.StudentMapper;
import com.example.hello.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student getOneStudent(int id){
return studentMapper.getStudentById(id);
}
}
E. 建controller包并编写controller层调用service
package com.example.hello.controller;
import com.example.hello.beans.Student;
import com.example.hello.serviceImpl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/getStudent")
public class StudentController {
@Autowired
private StudentServiceImpl studentServiceImpl;
@GetMapping
public String getStudent(int id, Model model){
Student std = studentServiceImpl.getOneStudent(id);
model.addAttribute("studentModel", std);
return "student";
}
}
F.Controll返回视图后,现在写thymleaf前端
在resources/templates里新建student.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>student</title>
</head>
<body>
<h1>Welcome <span th:text="${studentModel.name}"></span></h1>
</body>
</html>
启动项目,在浏览器访问localhost:8080/getStudent?id=1,可以看到以下界面
内容结束!!!