大厂大牛教你如何用SpringBoot 整合jdbc和mybatis

通用配置

下面介绍的整合JDBC和整合MyBatis都需要添加的实体类和配置

数据库表

CREATE TABLE `user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
 `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

实体类

添加简单的User实体类,用于下面jdbc和mybatis的使用和测试。再添加一个toString方法为了测试时看结果比较简单。

public class User {
   
 private Integer id;
 private String username;
 private String address;
 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 getAddress() {
    return address; }
 public void setAddress(String address) {
    this.address = address; }
 @Override
 public String toString() {
   
  return "User{" +
    "id=" + id +
    ", username='" + username + '\'' +
    ", address='" + address + '\'' +
    '}';
 }
}

maven配置

mysql版本根据自己数据库版本设置

druid为阿里云提供的数据源(可理解为连接池)

<

你可能感兴趣的:(JAVA技术,数据库,mybatis,mysql,spring,java)