SpringBoot整合MyBatisPlus
现在有这样一个需求:在SpringBoot中整合MyBatisPlus,并且使用MyBatis的方式编写一个完整链路的接口,即不使用MyBatisPlus中默认的增删改查
添加依赖
这里要注意我们的mapper.xml文件需要在Pom中通过Resources标签引入,否则会找不到对应的Mapper.xml文件
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
</parent>
<groupId>com.example</groupId>
<artifactId>mongo-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>mongo-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--spring webmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!--velocity模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!--swagger3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--page helper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<!-- 此配置不可缺,否则mybatis的Mapper.xml将会丢失 -->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
YML
#spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/mongo?serverTimezone=UTC
# username: root
# password: root
#Spring整合Druid连接池
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mongo?characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 30000
validation-query: select 'x'
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
filters: stat,wall,slf4j
max-pool-prepared-statement-per-connection-size: 20
use-global-data-source-stat: true
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
server:
port: 80
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
#Spring整合MP
mybatis-plus:
#定义别名包
type-aliases-package: com.example.mongoadmin.entity
#导入映射文件
mapper-locations: classpath:/mapper/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#为com.example.mongoadmin.mapper包下的SQL执行打印日志
logging:
level:
com.example.mongoadmin.mapper: info
项目结构
编写代码
Mapper
一般来说我们需要查什么内容就在Mapper中定义什么接口就行
package com.example.mongoadmin.mapper;
import com.example.mongoadmin.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* <p>
* 用户管理 Mapper 接口
* </p>
*
* @author ZJ
* @since 2023-06-24
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> findAll();
}
Mapper.xml
Mapper中定义的接口具体实现在Mapper.xml文件中,也就是SQL语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mongoadmin.mapper.UserMapper">
<select id="findAll" resultType="com.example.mongoadmin.entity.User">
select * from sys_user;
</select>
</mapper>
Service
同理,和Mapper接口一样,Service接口定义要实现的规范
package com.example.mongoadmin.service;
import com.example.mongoadmin.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 用户管理 服务类
* </p>
*
* @author ZJ
* @since 2023-06-24
*/
public interface IUserService extends IService<User> {
List<User> findAll();
}
ServiceImpl
Service接口定义的规范,由实现它的ServiceImpl来具体实现
package com.example.mongoadmin.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.example.mongoadmin.entity.User;
import com.example.mongoadmin.mapper.UserMapper;
import com.example.mongoadmin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 用户管理 服务实现类
* </p>
*
* @author ZJ
* @since 2023-06-24
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
Controller
Controller是系统暴露对外的接口,这里通过依赖注入调用Service的具体实现来与数据库交互内容
package com.example.mongoadmin.controller;
import com.example.mongoadmin.entity.User;
import com.example.mongoadmin.service.IUserService;
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 javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 用户管理 前端控制器
* </p>
*
* @author ZJ
* @since 2023-06-24
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/findAll")
public List<User> findAll() {
return userService.findAll();
}
}
测试验证
启动项目,访问接口地址:http://localhost/user/findAll