以下内容参考尚硅谷的https://www.bilibili.com/video/BV1y7411y7am视频
在实际开发中,经常使用发行已久的稳定的版本,而不是最新的版本
除了自带的springboot-starter和springboot-test外,只导入了mybatisplus、mysql和lombok三个依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.0.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
DROP TABLE IF EXISTS USER;
CREATE TABLE USER(
id BIGINT(20) NOT NULL COMMENT '主键',
NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
INSERT INTO USER (id,name,age,email) VALUES
('1','Jone',18,'[email protected]')
('2','Jack',20,'[email protected]'),
('3','Tom',28,'[email protected]'),
('4','Sandy',21,'[email protected]'),
('5','Billie',24,'[email protected]');
ALTER TABLE USER ADD COLUMN version INT;
ALTER TABLE USER ENGINE=INNODB CHARSET=UTF8;
ALTER TABLE USER ADD COLUMN deleted TINYINT(1) DEFAULT 0;
package com.nslt.mpdemo10101.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
注意使用@Repository注解
package com.nslt.mpdemo10101.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nslt.mpdemo10101.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
在启动类上添加@MapperScan注解
@MapperScan("com.nslt.mpdemo10101.mapper")
@SpringBootApplication
public class Mpdemo10101Application {
public static void main(String[] args) {
SpringApplication.run(Mpdemo10101Application.class, args);
}
}
查询所有用户
@Autowired
private UserMapper userMapper;
@Test
public void selectAllUsers(){
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
运行结果如下