在当前的企业中springboot工程应用越来越多,我们需要使用新的框架重构以前的技术。
案例说明:通过springboot整合mybatis实现对Student表的crud操作。
1、项目结构
2、通过idea创建工程,导入依赖包
springboot2.7.9+mybati3.5.11+mysql5.1.49
3、在数据库中创建项目测试依赖的表
案例中使用的是mysql数据库
CREATE TABLE `student` (
`stu_id` varchar(50) NOT NULL,
`stu_name` varchar(20) default NULL,
`stu_sex` varchar(20) default NULL,
`stu_age` varchar(20) default NULL,
`stu_addr` varchar(20) default NULL,
`stu_pwd` varchar(20) default NULL,
PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4、在工程中创建Student对象
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String stu_id;
private String stu_name;
private String stu_sex;
private String stu_age;
private String stu_addr;
private String stu_pwd;
}
5、application.yml系统配置文件配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
6、配置连接数据库及mybatis的配置信息
说明1:如何全部使用mybatis默认配置,application配置文件中可以啥也不配置。
说明2:下面是自定义mybatis的配置信息
#mybatis配置
mybatis:
#自定义映射文件的路径
mapper-locations: classpath:com/it/txc/config/*.xml
#自定义mybatis的全局配置文件
#config-location: mybatis.xml
#定义mybatis的路径别名方式
#type-aliases-package: com.it.txc.bean
configuration:
cache-enabled: true #开启二级缓存
7、工程的pom.xml信息
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.txc</groupId>
<artifactId>springbootmybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootmybatis</name>
<description>springbootmybatis</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
8、在pom.xml中配置读取xml文件配置
mybatis默认无法去识别src下的xml文件,需要配置项目允许范文src根目录下的xml文件
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</ build >
9、创建MyBatis的映射文件StudentMapper.xml
这里主要写了对表的常见操作:删除/修改/添加/查询全部/按照ID查询。
<?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.txc.mybatis.mapper.StudentMapper">
<!--
功能:查询所有学生信息
id:是Statement的名称
resultType:传入参数类型
-->
<select id="findAll" resultType="com.txc.mybatis.bean.Student">
select * from student
</select>
<!--
功能:根据id查询学生信息
parameterType:传入参数类型
-->
<select id="findStudentById" parameterType="string" resultType="com.txc.mybatis.bean.Student">
select * from student where stu_id=#{id}
</select>
<!--添加学生信息-->
<insert id="addStudent" parameterType="com.txc.mybatis.bean.Student">
insert into student(stu_id,stu_name,stu_sex,stu_age,stu_addr,stu_pwd)
value(#{stu_id},#{stu_name},#{stu_sex},#{stu_age},#{stu_addr},#{stu_pwd})
</insert>
<!--通过id修改学生信息-->
<update id="updStudentById" parameterType="com.txc.mybatis.bean.Student">
update student stu stu_name=#{stu_name},stu_sex=#{stu_sex}
where stu_id=#{stu_id}
</update>
<!--通过id删除学生信息-->
<delete id="deleteStuentById" parameterType="string">
delete from student where stu_id=#{stu_id}
</delete>
</mapper>
10、创建Mybatis的接口
mybatis的mapper代理开发,接口的创建需要遵循四大规范:
规范1:接口的方法名需要与StudentMapper.xml中的id的名称相同。
规范2:接口的输入参数类型与StudentMapper.xml中的parameterType的类型相同
规范3:接口的输出参数类型与StudentMapper.xml中年的resultType的类型相同
规范4:StudentMapper.xml中mapper标记的namespace的值与接口的路径名称相同
public interface StudentMapper {
//查询全部的学生信息
public List<Student> findAll();
//查询全部的学生信息
public List<Student> findStudentById(String stu_id);
//添加学生信息
public void addStudent(Student stu);
//通过id修改学生信息
public void updStudentById(Student stu);
//通过id删除学生信息
public void deleteStuentById(String stu_id);
}
11、创建StudentService业务层类
业务层类主要做业务操作,实现事务回滚功能,此处案例中的service功能不大。
@Service
public class StudentService {
@Autowired(required = false)
StudentMapper studentMapper;
//查询全部的学生信息
public List<Student> findAll() {
return studentMapper.findAll();
}
//查询全部的学生信息
public List<Student> findStudentById(String stu_id){
return studentMapper.findStudentById(stu_id);
}
//添加学生信息
public void addStudent(Student stu){
studentMapper.addStudent(stu);
}
//通过id修改学生信息
public void updStudentById(Student stu){
studentMapper.updStudentById(stu);
}
//通过id删除学生信息
public void deleteStuentById(String stu_id) {
studentMapper.deleteStuentById(stu_id);
}
}
12、创建springboot工程启动类
重点说明:启动类中需要通过@MapperScan(basePackages = "com.txc.mybatis.mapper")扫描接口和映射文件所在的包
@SpringBootApplication
@MapperScan(basePackages = "com.txc.mybatis.mapper")
public class SpringbootmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisApplication.class, args);
}
}
13、程序测试
测试地址:http://localhost:8080/findAll
输出结果:
14、源码下载地址
https://download.youkuaiyun.com/download/tangshiyilang/87597117