文章目录
前言
通过对Mybatis-Plus的安装和配置来完成对完成对Mybatis-Plus的使用。
一、准备
1. 建表语句
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
id BIGINT NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
2. 新增语句
DELETE FROM `user`;
INSERT INTO `user` (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
二、安装
1.Spring Boot2
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
2.Spring Boot3
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
三、配置
1. 配置
在 application.yml 配置文件中添加 H2 数据库的相关配置:
H2
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
username: root
password: test
sql:
init:
schema-locations: classpath:db/schema-h2.sql
data-locations: classpath:db/data-h2.sql
MYSQL
spring:
application:
name: spring-boot3
#druid
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: 123456a?
initial-size: 5
max-active: 20
max-wait: 60000
min-idle: 3
sql部分,手动执行即可
2. 扫描
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
四、编码
编写实体类 User.java:
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写 Mapper 接口类 UserMapper.java:
public interface UserMapper extends BaseMapper<User> {
}
五、开始使用
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.isTrue(5 == userList.size(), "");
userList.forEach(System.out::println);
}
}
UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper,所以不填写就是无任何条件
控制台输出:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
六、开启日志
启用Mybatis内部日志记录可以帮助我们更好地观察sql的执行情况
1. 方式一
在您的 application.yml
或 application.properties
文件中添加以下配置:
#这将使用 MyBatis 内置的 StdOutImpl 日志记录实现将日志输出到控制台
mybatis-plus:
configuration:
# 如果项目无日志框架,可以考虑指定为 org.apache.ibatis.logging.stdout.StdOutImpl (请勿在实际生产中使用).
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2. 方式二
在您的 application.yml
或 application.properties
文件中增加日志级别配置,以指定特定包的日志级别
#这将指定 com.baomidou.example.mapper 包下的日志级别为 debug。您可以根据需要调整级别
logging:
level:
com.baomidou.example.mapper: debug
总结
回到顶部
Spring Boot 快速启动示例
Spring MVC 快速启动示例
通过以上几个简单的步骤,我们就实现了 User 表的 CRUD 功能,甚至连 XML 文件都不用编写!
从以上步骤中,我们可以看到集成 MyBatis-Plus 非常的简单,只需要引入 starter 依赖,简单进行配置即可使用。
但 MyBatis-Plus 的强大远不止这些功能,想要详细了解 MyBatis-Plus 的强大功能?那就继续往下看吧!