springboot与mybatis 整合完整教程
项目总览结构

pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
准备数据源
create table student
(
id bigint auto_increment comment '主键ID'
primary key,
name varchar(32) null comment '姓名',
sex int null,
age int null
);
添加数据库配置信息
# 配置Spring Boot应用的数据库源
spring:
datasource:
# 数据库连接URL,包含数据库的地址,端口,数据库名以及连接参数
url: jdbc:mysql://192.168.1.53:3306/student_info?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
# 数据库用户名
username: root
# 数据库密码
password: 1
# 数据库驱动类名
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# MyBatis的Mapper XML文件位置
mapper-locations: classpath:mapper/*.xml
# MyBatis的类型别名包,用于简化配置
type-aliases-package: com.js.handlemybatis.dao
修改启动类

@SpringBootApplication
@MapperScan("com.jd.handlemybatisdemo.mapper")
public class HandleMybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HandleMybatisDemoApplication.class, args);
}
}
dao包 创建Student.java
package com.js.handlemybatis.dao;
public class Student {
private Long id;
private String name;
private Integer age;
private Integer sex;
public Student() {
}
public Student(String name, Integer age, Integer sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Student(Long id, String name, Integer age, Integer sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
mapper包 添加 StudentMapper.java

package com.js.handlemybatis.mapper;
import com.js.handlemybatis.dao.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAll();
}
resource/mapper文件夹下 添加StudentMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!-- 遵循MyBatis 3.0的映射器(DTD Mapper)公共类型定义 -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 定义一个映射器,namespace为映射器的唯一标识符,通常对应一个具体的DAO接口 -->
<mapper namespace="com.js.handlemybatis.mapper.StudentMapper">
<!-- 定义一个查询操作,id为操作的唯一标识符,resultMap指定结果集映射 -->
<select id="selectAll" resultMap="BaseResultMap">
<!-- SQL查询语句:选择学生表中的所有列 -->
select *
from student
</select>
<!-- 定义结果集映射,id为映射的唯一标识符,type指定映射到的Java类型 -->
<resultMap id="BaseResultMap" type="com.js.handlemybatis.dao.Student">
<!-- 映射学生表的id列到Student类的id属性,jdbcType指定对应的数据库类型 -->
<id column="id" property="id" jdbcType="BIGINT"/>
<!-- 映射学生表的name列到Student类的name属性,jdbcType指定对应的数据库类型 -->
<result column="name" property="name" jdbcType="VARCHAR"/>
<!-- 映射学生表的age列到Student类的age属性,javaType指定对应的Java类型 -->
<result column="age" property="age" javaType="Integer"/>
<!-- 映射学生表的sex列到Student类的sex属性,javaType指定对应的Java类型 -->
<result column="sex" property="sex" javaType="Integer"/>
</resultMap>
</mapper>
单元测试

@Autowired
private StudentMapper studentMapper;
@Test
void contextLoadsHandleSelectAll() {
List<Student> students = studentMapper.selectAll();
for (Student student : students) {
System.out.println(student);
}
}
实战:实现学生信息管理模块
修改mapper接口

import com.js.handlemybatis.dao.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAll();
Student selectOneById(Long id);
int insert(Student student);
int update(Student student);
int delete(Long id);
}
修改mapper映射文件

<?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,命名空间为com.js.handlemybatis.mapper.StudentMapper -->
<mapper namespace="com.js.handlemybatis.mapper.StudentMapper">
<!-- 定义一个查询所有学生的SQL语句 -->
<select id="selectAll" resultMap="BaseResultMap">
select *
from student
</select>
<!-- 定义一个根据ID查询单个学生的SQL语句 -->
<select id="selectOneById" resultMap="BaseResultMap">
select *
from student
where id = #{id}
</select>
<!-- 定义一个插入学生的SQL语句 -->
<insert id="insert">
insert into student(name, age, sex)
values (#{name}, #{age}, #{sex})
</insert>
<!-- 定义一个更新学生的SQL语句 -->
<update id="update">
update student
set name = #{name},
age = #{age},
sex = #{sex}
where id = #{id}
</update>
<!-- 定义一个删除学生的SQL语句 -->
<delete id="delete">
delete from student
where id = #{id}
</delete>
<!-- 定义一个结果映射,将查询结果映射到Student对象 -->
<resultMap id="BaseResultMap" type="com.js.handlemybatis.dao.Student">
<!-- 配置主键字段的映射 -->
<id column="id" property="id" jdbcType="BIGINT"/>
<!-- 配置其他字段的映射 -->
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" javaType="Integer"/>
<result column="sex" property="sex" javaType="Integer"/>
</resultMap>
</mapper>
单元测试

package com.js.handlemybatis;
import com.js.handlemybatis.dao.Student;
import com.js.handlemybatis.mapper.StudentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* HandleMyBatisApplicationTests类用于测试MyBatis的基本操作
* 包括查询、插入、更新和删除操作
*/
@SpringBootTest
class HandleMyBatisApplicationTests {
/**
* contextLoads方法用于测试应用程序上下文是否正确加载
* 这里没有执行任何业务逻辑,主要用于检查依赖注入和应用程序启动是否成功
*/
@Test
void contextLoads() {
}
/**
* 注入StudentMapper接口的实例,用于执行数据库操作
*/
@Autowired
private StudentMapper studentMapper;
/**
* contextLoadsHandleSelectAll方法用于测试查询所有学生信息的功能
* 它从数据库中选择所有学生,并打印出每个学生的信息
*/
@Test
void contextLoadsHandleSelectAll() {
List<Student> students = studentMapper.selectAll();
for (Student student : students) {
System.out.println(student);
}
}
/**
* contextLoadsHandleInsert方法用于测试插入学生信息的功能
* 它创建一个新的学生对象,插入到数据库中,并打印出所有学生的信息
*/
@Test
void contextLoadsHandleInsert() {
Student student = new Student("lijun", 18, 1);
studentMapper.insert(student);
List<Student> students = studentMapper.selectAll();
for (Student st : students) {
System.out.println(st);
}
}
/**
* contextLoadsHandleUpdate方法用于测试更新学生信息的功能
* 它根据ID选择一个学生,更新其信息,并打印出所有学生的信息
*/
@Test
void contextLoadsHandleUpdate() {
Student student = studentMapper.selectOneById(4L);
student.setName("lijunqiang");
student.setAge(28);
studentMapper.update(student);
// 打印出所有数据
List<Student> students = studentMapper.selectAll();
for (Student st : students) {
System.out.println(st);
}
}
/**
* contextLoadsHandleSelectOneById方法用于测试按ID查询学生信息的功能
* 它根据指定的ID选择一个学生,并打印出该学生的信息
*/
@Test
void contextLoadsHandleSelectOneById() {
Student student = studentMapper.selectOneById(2L);
System.out.println(student);
}
/**
* contextLoadsHandleDelete方法用于测试删除学生信息的功能
* 它首先打印出所有学生的信息,然后根据指定ID删除一个学生,最后再次打印出所有学生的信息
*/
@Test
void contextLoadsHandleDelete() {
List<Student> students = studentMapper.selectAll();
for (Student st : students) {
System.out.println(st);
}
studentMapper.delete(4L);
for (Student st : students) {
System.out.println(st);
}
}
}
posted on 2025-05-09 12:46 Indian_Mysore 阅读(85) 评论(0) 收藏 举报
浙公网安备 33010602011771号