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>3.1.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- Generated by https://start.springboot.io -->
<!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
<groupId>com.example</groupId>
<artifactId>SpringBootMyBatisDemo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootMyBatisDemo02</name>
<description>SpringBootMyBatisDemo02</description>
<properties>
<java.version>17</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>3.0.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.2</version>
<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>
更改application为yml
做数据源配置:
# 数据源配置:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
在resources下新建目录mapper
继续在yml内做mybatis配置
#mybatis配置:
mybatis:
mapper-locations: classpath:mapper/*.xml #加入mapper.xml的扫描
type-aliases-package: com.example.entity #设置实体类别名
#开启mybatis对应的sql的日志
logging:
level:
com.example.mapper: trace
数据库user表
创建实体类User
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
private Integer id;
private String name;
private String pwd;
}
创建UserMapper 简单增删改查一下
public interface UserMapper {
//查询 : 根据主键查询一条用户数据
User selectOneUser(Integer id);
//保存
int save(User user);
//更新
int update(User user);
//删除 : 根据主键id删除一条用户数据
int delete(Integer id);
}
在resource下的mapper目录中创建UserMapper.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.example.mapper.UserMapper">
<select id="selectOneUser" parameterType="int" resultType="User">
select * from user where id = #{arg0}
</select>
<insert id="save" parameterType="User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="update" parameterType="User">
update user set name = #{name},pwd = #{pwd} where id = #{id}
</update>
<delete id="delete" parameterType="int">
delete from user where id = #{arg0}
</delete>
</mapper>
创建接口UserService
public interface UserService {
User findOneUser(Integer id);
int save(User user);
int update(User user);
int deleteUserById(Integer id);
}
创建实现类UserServiceImpl 这里会爆红因为还没有写Controller
@Service
public class UserServiceImpl implements UserService {
// 连接mapper层 用Autowired注解注入进来
@Autowired
private UserMapper userMapper;
@Override
public User findOneUser(Integer id) {
return userMapper.selectOneUser(id);
}
@Override
public int save(User user) {
return userMapper.save(user);
}
@Override
public int update(User user) {
return userMapper.update(user);
}
@Override
public int deleteUserById(Integer id) {
return userMapper.delete(id);
}
}
创建UserController
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
@ResponseBody
public User select(@PathVariable Integer id){
return userService.findOneUser(id);
}
@PostMapping("/user")
@ResponseBody
public int save(@RequestBody User user){
return userService.save(user);
}
@PutMapping("/user")
@ResponseBody
public int update(@RequestBody User user){
return userService.update(user);
}
@DeleteMapping("/user/{id}")
@ResponseBody
public int delete(@PathVariable Integer id){
return userService.deleteUserById(id);
}
}
测试
启动后报错
说对应的UserMapper找不到,漏加了包扫描
补上@MapperScan
@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringBootMyBatisDemo02Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootMyBatisDemo02Application.class, args);
}
}
启动成功
使用apipost开始测试
刷新数据库user表
刷新user表
刷新user表
这样所有的功能都没问题了