一、集成
1、pom:
2、配置文件
3、启动类与业务逻辑:
无变化。引入mybatis-plus后,原mybatis逻辑可以正常使用。
二、demo
1、代码框架
(1)pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.security</groupId>
<artifactId>security-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.RELEASE</version>
</parent>
<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>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)配置文件:
server.port=1111
server.servlet.context-path=/plusDemo
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3308/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
#打印日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
(3)启动类
package com.pluscache.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.pluscache.demo.mapper")
@SpringBootApplication
public class PlusApplication {
public static void main(String[] args) {
SpringApplication.run(PlusApplication.class, args);
}
}
(4)controller
package com.pluscache.demo.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pluscache.demo.dto.UserDTO;
import com.pluscache.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/listUsers")
public List<UserDTO> listUsers(String userName){
return userService.listUsers(userName);
}
@RequestMapping("/listUsersByAccount")
public List<UserDTO> listUsersByAccount(String userAccount){
return userService.listUsersByAccount(userAccount);
}
@RequestMapping("/listPageUser")
public IPage<UserDTO> listPageUser(Page<UserDTO> page,String userName){
return userService.listPageUser(page,userName);
}
}
(5)service
package com.pluscache.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pluscache.demo.dto.UserDTO;
import java.util.List;
public interface UserService {
List<UserDTO> listUsers(String userName);
List<UserDTO> listUsersByAccount(String userAccount);
IPage<UserDTO> listPageUser(Page<UserDTO> page, String userName);
}
package com.pluscache.demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pluscache.demo.dto.UserDTO;
import com.pluscache.demo.repository.UserRepository;
import com.pluscache.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
public UserRepository userRepository;
@Override
public List<UserDTO> listUsers(String userName) {
return userRepository.listUsers(userName);
}
@Override
public List<UserDTO> listUsersByAccount(String userAccount) {
return userRepository.listUsersByAccount(userAccount);
}
@Override
public IPage<UserDTO> listPageUser(Page<UserDTO> page, String userName) {
Page<UserDTO> userIPage = new Page<>();
userIPage.setCurrent(page.getCurrent());
userIPage.setSize(page.getSize());
userIPage.setOrders(page.orders());
Long total = userRepository.countByUserName(userName);
if (total <= 0) {
return new Page<>();
}
userIPage.setTotal(total);
List<UserDTO> users = userRepository.pageUsers(page,userName);
userIPage.setRecords(users);
return userIPage;
}
}
(6)dao
package com.pluscache.demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pluscache.demo.dto.UserDTO;
import com.pluscache.demo.repository.UserRepository;
import com.pluscache.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
public UserRepository userRepository;
@Override
public List<UserDTO> listUsers(String userName) {
return userRepository.listUsers(userName);
}
@Override
public List<UserDTO> listUsersByAccount(String userAccount) {
return userRepository.listUsersByAccount(userAccount);
}
@Override
public IPage<UserDTO> listPageUser(Page<UserDTO> page, String userName) {
Page<UserDTO> userIPage = new Page<>();
userIPage.setCurrent(page.getCurrent());
userIPage.setSize(page.getSize());
userIPage.setOrders(page.orders());
Long total = userRepository.countByUserName(userName);
if (total <= 0) {
return new Page<>();
}
userIPage.setTotal(total);
List<UserDTO> users = userRepository.pageUsers(page,userName);
userIPage.setRecords(users);
return userIPage;
}
}
package com.pluscache.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pluscache.demo.dto.UserDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
//@Mapper
public interface UserMapper extends BaseMapper<UserDTO> {
List<UserDTO> listUsers(@Param("userName") String userName);
List<UserDTO> pageUsers(@Param("offset") long offset, @Param("size") long size, @Param("userName") String userName);
Long countByUserName(@Param("userName") String userName);
}
<?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.pluscache.demo.mapper.UserMapper">
<select id="listUsers" resultType="com.pluscache.demo.dto.UserDTO">
select user_name userName,user_account userAccount,age from t_user
where 1=1
<if test="userName != '' and userName != null">
and user_name = #{userName}
</if>
</select>
<select id="countByUserName" resultType="long">
select count(1) from t_user
where 1=1
<if test="userName != '' and userName != null">
and user_name = #{userName}
</if>
</select>
<select id="pageUsers" resultType="com.pluscache.demo.dto.UserDTO">
select user_name userName,user_account userAccount,age from t_user
where 1=1
<if test="userName != '' and userName != null">
and user_name = #{userName}
</if>
limit #{offset},#{size}
</select>
</mapper>
(7)dto
package com.pluscache.demo.dto;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("t_user")
public class UserDTO {
private String userName;
private String userAccount;
private Integer age;
}
(8)测试:
2、增删改查
3、分页查询
此时有三种方法
- 原始的 limit 进行分页
- pageHelper 第三方插件
- MyBatisPlus 内置分页插件