现在有这样一个需求:在SpringBoot中整合MyBatisPlus,并且使用MyBatis的方式编写一个完整链路的接口,即不使用MyBatisPlus中默认的增删改查
这里要注意我们的mapper.xml文件需要在Pom中通过Resources标签引入,否则会找不到对应的Mapper.xml文件
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.1version>
parent>
<groupId>com.examplegroupId>
<artifactId>mongo-adminartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.examplegroupId>
<artifactId>mongo-coreartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.3version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-boot-starterartifactId>
<version>3.0.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.22version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.3.0version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.ymlinclude>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
resource>
resources>
build>
project>
#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中定义什么接口就行
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;
/**
*
* 用户管理 Mapper 接口
*
*
* @author ZJ
* @since 2023-06-24
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> findAll();
}
Mapper中定义的接口具体实现在Mapper.xml文件中,也就是SQL语句
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>
同理,和Mapper接口一样,Service接口定义要实现的规范
package com.example.mongoadmin.service;
import com.example.mongoadmin.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
*
* 用户管理 服务类
*
*
* @author ZJ
* @since 2023-06-24
*/
public interface IUserService extends IService<User> {
List<User> findAll();
}
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;
/**
*
* 用户管理 服务实现类
*
*
* @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是系统暴露对外的接口,这里通过依赖注入调用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;
/**
*
* 用户管理 前端控制器
*
*
* @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