mybatis搭建demo
# SQL准备
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80029
Source Host : localhost:3306
Source Schema : ssm
Target Server Type : MySQL
Target Server Version : 80029
File Encoding : 65001
Date: 24/03/2024 10:01:31
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint NOT NULL COMMENT '主键ID',
`name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '姓名',
`age` int NULL DEFAULT NULL COMMENT '年龄',
`email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'Jone', 18, 'test1@baomidou.com');
INSERT INTO `user` VALUES (2, 'Jack', 20, 'test2@baomidou.com');
INSERT INTO `user` VALUES (3, 'Tom', 28, 'test3@baomidou.com');
INSERT INTO `user` VALUES (4, 'Sandy', 21, 'test4@baomidou.com');
INSERT INTO `user` VALUES (5, 'Billie', 24, 'test5@baomidou.com');
SET FOREIGN_KEY_CHECKS = 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 主类
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/haha")
public String getUser() {
List<Userdao> userdaos = userMapper.queryUserList();
for (Userdao userdao : userdaos) {
System.out.println(userdao);
}
return "successs";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Data
public class Userdao {
private Long id;
private String name;
private Integer age;
private String email;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
@Mapper
@Repository
public interface UserMapper {
//只是整合测试,为了可读性,只写了一个方法
List<Userdao> queryUserList();
}
1
2
3
4
5
6
2
3
4
5
6
@SpringBootApplication
@MapperScan("com.ziki.mybatis_delete.mapper")
public class MybatisDeleteApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisDeleteApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 配置
spring:
datasource:
username: root
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.ziki.mybatis_delete.dao
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 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 namespace="!!!!!!!!!!!!!">
<select id="queryUserList" resultType="!!!!!!!!!!!">
select * from user
</select>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11