说明:核心配置就是数据库连接相关信息;映射配置:SQL映射。
说明:创建了一个springboot_05_mybatis项目。
说明:按需配置,也就是按需勾选。
说明:springboot自带的都是spring-boot这样格式;第三方技术是技术-spring-boot。
说明:配置数据连接配置
#配置数据库
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 123456
说明:在domain文件夹创建User实体类
package com.forever.domain;
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String addr_city;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", addr_city='" + addr_city + '\'' +
'}';
}
public User(Integer id, String username, String password, String gender, String addr_city) {
this.id = id;
this.username = username;
this.password = password;
this.gender = gender;
this.addr_city = addr_city;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddr_city() {
return addr_city;
}
public void setAddr_city(String addr_city) {
this.addr_city = addr_city;
}
}
说明: dao文件下创建UserDao接口
package com.forever.dao;
import com.forever.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
// 数据库SQL映射需要添加@Mapper被容器识别到
public interface UserDao {
@Select("select * from tb_user where id= #{id}")
public User getById(Integer id);
}
package com.forever;
import com.forever.dao.UserDao;
import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
private UserDao userDao;
@Test
void contextLoads() {
System.out.println(userDao.getById(1));
}
}
/*
Navicat Premium Data Transfer
Source Server : mysql
Source Server Type : MySQL
Source Server Version : 50741
Source Host : localhost:3306
Source Schema : mybatis
Target Server Type : MySQL
Target Server Version : 50741
File Encoding : 65001
Date: 04/11/2023 22:00:10
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`addr_city` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES (1, '张三', '123', '男', '成都');
INSERT INTO `tb_user` VALUES (2, '李四', '123', '女', '北京');
INSERT INTO `tb_user` VALUES (3, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (4, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (5, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (6, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (7, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (8, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (9, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (10, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (11, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (12, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (13, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (14, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (15, '李', '1320', '男', '成都');
INSERT INTO `tb_user` VALUES (16, '李', '1320', '男', '成都');
SET FOREIGN_KEY_CHECKS = 1;
10.注意
问题1:MySQL8.x驱动强制要求设置时区
解决:
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
问题2:This is deprecated(这个已经弃用)
解决:
driver-class-name: com.mysql.cj.jdbc.Driver