po.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.why springbootmybatisplus010.0.1-SNAPSHOT springbootmybatisplus01 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter2.2.0 org.springframework.boot spring-boot-devtoolsruntime true mysql mysql-connector-javaruntime org.springframework.boot spring-boot-configuration-processortrue org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest com.baomidou mybatis-plus-boot-starter3.3.1.tmp org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
SQL脚本
DROP TABLE IF EXISTS user; CREATE TABLE user ( id int(20) NOT NULL COMMENT '主键ID', name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名', age int(11) NULL DEFAULT NULL COMMENT '年龄', email varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (id) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT INTO user VALUES (1, '张三', 18, '123qq.com'); INSERT INTO user VALUES (2, '李四', 19, '345qq.com'); INSERT INTO user VALUES (3, '王五', 18, '678qq.com');
User实体类
package com.why.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User {
private static final long serialVersionUID=1L;
@TableId(value = "id", type = IdType.AUTO)//id 自增
private Integer id;
private String name;
private Integer age;
private String email;
public User(String name, Integer age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
}
UserMapper
package com.why.mapper; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.why.pojo.User; import org.springframework.stereotype.Component; @Component public interface UserMapper extends baseMapper{ }
package com.why;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.why.mapper")
public class Springbootmybatisplus01Application {
public static void main(String[] args) {
SpringApplication.run(Springbootmybatisplus01Application.class, args);
}
}
application.properties
#com.mysql.cj.jdbc.Driver:springBoot2.1以上要加上cj spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #serverTimezone=GMT%2B8:添加时区 spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=GMT%2B8 spring.datasource.username=mybatisplus spring.datasource.password=mybatisplus #mybatis日志:添加后可以查看执行的sql语句 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
测试方法
package com.why;
import com.why.mapper.UserMapper;
import com.why.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springbootmybatisplus01ApplicationTests {
@Autowired private UserMapper userMapper;
@Test
void contextLoads() {
}
@Test
void insert(){
userMapper.insert(new User("张三",20,"529044029@qq.com"));
}
}
项目结构



