1.数据准备
-- 创建数据库
create database shop;
-- 使用数据库
use shop;
-- 创建测试表
create table tb_user(
id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
user_name varchar(20) NOT NULL COMMENT '用户名',
password varchar(20) NOT NULL COMMENT '密码',
name varchar(30) DEFAULT NULL COMMENT '姓名',
age int(11) DEFAULT NULL COMMENT '年龄',
email varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY(id)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=UTF8;
-- 插入测试数据
INSERT INTO tb_user(id, user_name, password, name, age, email) values (1, 'zhangsan', '123456', '张三', 18, 'zhangsan@163.com');
INSERT INTO tb_user(id, user_name, password, name, age, email) values (2, 'lisi', '123456', '李四', 20, 'lisi@163.com');
INSERT INTO tb_user(id, user_name, password, name, age, email) values (3, 'wangwu', '123456', '王五', 28, 'wangwu@163.com');
INSERT INTO tb_user(id, user_name, password, name, age, email) values (4, 'zhaoliu', '123456', '赵六', 21, 'zhaoliu@163.com');
INSERT INTO tb_user(id, user_name, password, name, age, email) values (5, 'sunqi', '123456', '孙七', 24, 'sunqi@163.com');
2.创建springboot工程
(1).pom文件
<?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>com.xiaotian</groupId>
<artifactId>boot-MyBatis-Plus</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 整合PageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
(2).工程目录结构
(3).启动类
package com.xiaotian;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.xiaotian.mapper")
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}
(4).实体类
package com.xiaotian.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
//描述:表名注解,标识实体类对应的表
@TableName(value = "tb_user")
public class User {
@TableId(type= IdType.AUTO)
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email; //查询时不显示
public Long getId() {
return id;
}
public void setId(Long 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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}
(5).mapper接口
package com.xiaotian.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaotian.bean.User;
public interface UserMapper extends BaseMapper<User> {
}