一、创建项目
二、添加依赖包(pom.xml)
<?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 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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>boot_mp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot_mp</name>
<description>springboot整合mabitsPlus</description>
<!-- 版本 -->
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<mybatisplus.version>3.2.0</mybatisplus.version>
</properties>
<dependencies>
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mybatisplus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、添加application.yml
#配置数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
username: root
password: root
type: org.springframework.jdbc.datasource.DriverManagerDataSource
#配置mybatisplus
mybatis-plus:
mapper-locations:
- classpath:mapper/*Mapper.xml
global-config:
db-config:
id-type: auto
banner: true
四、添加实体类、Mapper接口、映射文件mapper.xml
1、实体类
package com.sxt.domain;
import java.io.Serializable;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName(value="sys_user") //建立User.class和数据的sys_user表的关系
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@TableId(value="id") //主键
private Integer id;
@TableField(value="name")
private String name;
private String address;
private Date birth;
}
2、Mapper接口
package com.sxt.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sxt.domain.User;
/**
* BaseMapper提供增删改查的sql语句
* @author LPP
*
*/
public interface UserMapper extends BaseMapper<User>{
}
3、映射文件mapper.xml
<?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.sxt.mapper.UserMapper" >
</mapper>
拓展知识:
BaseMapper
五、测试类
package com.sxt;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.domain.User;
import com.sxt.mapper.UserMapper;
/**
* 测试类
* @author LPP
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
class BootMpApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
System.out.println(userMapper);
}
/**
* 增加功能
*/
@Test
public void addUser() {
userMapper.insert(new User(null,"AA","BB",new Date()));
}
/**
* 分页功能
*/
@Test
public void queryForPage() {
IPage<User> page = new Page<>(1,5);
userMapper.selectPage(page, null);
List<User> list = page.getRecords();
for (User user : list) {
System.out.println(user);
}
System.out.println("总条数:"+page.getTotal());
}
}
MybatisPlus分页配置
package com.sxt.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
/**
* MybatisPlus分页配置
* @author LPP
*
*/
@Configuration
@ConditionalOnClass(value = PaginationInterceptor.class)
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}