一、步骤预览
二、初始化工程
使用 Spring Initializr 快速初始化一个 Spring Boot 工程
Group:com.mybatisplus
Artifact:demo;
版本:2.2.1.RELEASE
三、添加依赖
1、引入依赖
spring-boot-starter
、spring-boot-starter-test
添加:mybatis-plus-boot-starter
、MySQL、
lombok
、
在项目中使用Lombok可以减少很多重复代码的书写。比如说getter/setter/toString等方法的编写
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2、idea中安装lombok插件
四、配置
在 application.properties
配置文件中添加 MySQL 数据库的相关配置:
mysql5
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
mysql8以上(spring boot 2.1)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
注意:
1、这里的 url 使用了 ?serverTimezone=GMT%2B8 后缀,因为Spring Boot 2.1 集成了 8.0版本的jdbc驱动,这个版本的 jdbc 驱动需要添加这个后缀,否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more
2、这里的 driver-class-name 使用了 com.mysql.cj.jdbc.Driver ,在 jdbc 8 中 建议使用这个驱动,之前的 com.mysql.jdbc.Driver 已经被废弃,否则运行测试用例的时候会有 WARN 信息
五、编写代码
1、主类
在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 包名
package com.mybatisplus.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.mybatisplus.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2、实体
package com.mybatisplus.demo.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
3、mapper
创建包 mapper 编写Mapper 接口: UserMapper.java
package com.mybatisplus.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybatisplus.demo.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
六、开始使用
使用测试类,进行功能测试:
package com.mybatisplus.demo;
import com.mybatisplus.demo.entity.User;
import com.mybatisplus.demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
//查询user表所有数据
@Test
public void findAll() {
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
}
直接点击运行findAll()方法可以测试
输出结果如下
[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)]
七、配置日志
在配置文件中application.properties添加
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
可以在控制台查看sql输出日志
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67110f71] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1713129148 wrapping com.mysql.cj.jdbc.ConnectionImpl@37864b77] will not be managed by Spring
==> Preparing: SELECT id,name,age,email FROM user
==> Parameters:
<== Columns: id, name, age, email
<== Row: 1, Jone, 18, [email protected]
<== Row: 2, Jack, 20, [email protected]
<== Row: 3, Tom, 28, [email protected]
<== Row: 4, Sandy, 21, [email protected]
<== Row: 5, Billie, 24, [email protected]
<== Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67110f71]