说明
创建一个最基本的MyBatisPlus项目,参考官网。
依赖
MyBatisPlus
依赖,最新版是:3.5.3.2
(截止2023-9-4)。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
因为连接的是mysql数据库,还要加入mysql驱动的依赖
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
配置
数据源(MySQL)配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatisplus?serverTimeZone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
此处,相比较使用 MyBatis 的配置,url 增加了 allowPublicKeyRetrieval=true
;否则,会报错:
Public Key Retrieval is not allowed
Mapper
package com.example.web.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.web.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
Mapper扫描配置
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = {"com.example.web.mapper"})
public class MybatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemoApplication.class, args);
}
}
测试
代码
package com.example;
import com.example.web.entity.User;
import com.example.web.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 MybatisPlusDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
}
测试打印日志
----- selectAll method test ------
2023-09-04 23:50:47.920 INFO 12640 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-09-04 23:50:48.442 INFO 12640 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
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)
Controller直接调用Mapper
此处只是测试通过接口调用Mapper,就直接将Mapper写在Controller里面了;
实际的项目中,一般是通过Service再调用Mapper的。
代码
package com.example.web.controller;
import com.example.web.entity.User;
import com.example.web.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("users")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping
public List<User> selectAll() {
return userMapper.selectList(null);
}
}
接口调用结果
参考
MyBatisPlus官网