目录
一、MyBatis的应用配置
- 依赖 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>03_myBatis_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
<version>2.7.12</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
</project>
- workbench数据准备
-
创建数据库:course
-
建表user;
CREATE TABLE `user` (
`id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(20) NOT NULL,
`age` VARCHAR(50) NOT NULL,
`sex` VARCHAR(50) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- 插数据:
insert into `user` (`name`,`age`,`sex`) values
('Tom','20','男'),
('Jack','22','男'),
('Lily','24','女');
- 查询:
- 创建包:com/course/model
- resource下创建文件:mapper/mysql.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.course">
<select id="getUserCount" resultType="Integer">
select count(*) from user;
</select>
<insert id="addUser" parameterType="com.course.model.User">
insert into user(id,name,age,sex)
values(#{id},#{name},#{age},#{sex})
</insert>
<update id="updateUser" parameterType="com.course.model.User">
update user set name=#{name},age=#{age}
where id=#{id}
</update>
<delete id="deleteUser" parameterType="Integer">
delete from user where id = #{id}
</delete>
</mapper>
- resource下创建文件:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.course.model"/>
</typeAliases>
<mappers>
<mapper resource="mapper/mysql.xml"/>
</mappers>
</configuration>
- 配置 resource文件夹下:application.yml
server:
port: 8188
logging:
path: logs
file: mylog.log
spring:
application:
name: myTest
datasource:
driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/course
username: root
password: root
mybatis:
type-aliases-package: com.course.model
mapper-locations:
- mapper/*
二、使用mybatis+SpringBoot完成增删改查
2.1 代码实现
- com.course.Application.java
package com.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- com.course.controller.Demo.java
package com.course.controller;
import com.course.model.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("v1")
public class Demo {
// 获取一个执行sql的对象
@Autowired
private SqlSessionTemplate template;
@RequestMapping(value = "/getUserCount",method = RequestMethod.GET)
public int getUserCount(){
return template.selectOne("getUserCount");
}
@RequestMapping(value = "/addUser",method = RequestMethod.POST)
public int addUser(@RequestBody User user){
return template.insert("addUser",user);
}
@RequestMapping(value = "/updateUser",method = RequestMethod.POST)
public int updateUser(@RequestBody User user){
return template.update("updateUser",user);
}
@RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
public int delUser(@RequestParam int id){
return template.delete("deleteUser",id);
}
}
- com.course.model.User.java
package com.course.model;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String sex;
private String age;
}
2.2 增删改查接口调用
-
/v1/getUserCount:查询数据接口
-
/v1/addUser:添加数据接口
使用jmeter测试:
1)添加Thread Group
2)添加HTTP Header Manager:Content-Type=application/json
3)添加HTTP Request:
4)添加查看结果树
5)查询数据库结果
-
/v1/updateUser:更新数据接口
使用jmeter测试:
1)添加Thread Group
2)添加HTTP Header Manager:Content-Type=application/json
3)添加HTTP Request:
4)添加查看结果树
5)查询数据库结果
-
/v1/deleteUser:删除数据接口
使用jmeter测试:
1)添加Thread Group
2)添加HTTP Header Manager:Content-Type=application/json
3)添加HTTP Request:
4)添加查看结果树
5)查询数据库结果