MyBatisPlus 基础实现(一)

说明

创建一个最基本的MyBatisPlus项目,参考官网。

依赖

MyBatisPlus 依赖,最新版是:3.5.3.2 (截止2023-9-4)。

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.3.2version>
        dependency>

因为连接的是mysql数据库,还要加入mysql驱动的依赖

        <dependency>
            <groupId>com.mysqlgroupId>
            <artifactId>mysql-connector-jartifactId>
            <scope>runtimescope>
        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=[email protected])
User(id=2, name=Jack, age=20, email=[email protected])
User(id=3, name=Tom, age=28, email=[email protected])
User(id=4, name=Sandy, age=21, email=[email protected])
User(id=5, name=Billie, age=24, email=[email protected])

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 基础实现(一)_第1张图片

打印SQL日志

在开发和测试环境,想要看到每次SQL语句和执行的结果。
MyBatisPlus支持SQL日志打印功能。

开启方法如下:

yml 配置

# mybatis-plus打印SQL日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

SQL日志示例

MyBatisPlus 基础实现(一)_第2张图片

参考

MyBatisPlus官网

你可能感兴趣的:(spring,boot,mybatis)