一、步驟預覽
二、初始化工程
使用 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]