##1. 添加mybatis-plus、Mysql驱动以及数据库连接池(以druid为例)的jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
##2. 在启动类上添加MapperScan,并指定Mapper所在的包名
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true) //开启事务支持,然后在访问数据库的Service方法上添加注解@Transactional便可
@MapperScan(basePackages = { "com.xxx.mapper" }) //Mapper类包扫描
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
##3. 在application.yml文件里配置数据库信息以及Mapper的映射文件(*.xml)
spring:
datasource:
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${db_server:127.0.0.1:3306}/${db_name:xxx}?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&autoReconnect=true&allowMultiQueries=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL
username: ${db_uid:root}
password: ${db_pwd:123456}
druid:
initialSize: 1 # 配置初始化大小
minIdle: 200 # 配置连接池中最小闲置的可用连接的个数
maxActive: 1000 # 配置连接池中最大可用连接的个数
maxWait: 60000 # 配置获取连接等待超时的时间, 单位是毫秒
timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测, 检测需要关闭的空闲连接, 单位是毫秒
minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间, 单位是毫秒
validationQuery: SELECT 1
validationQueryTimeout: 60000
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true # 打开PSCache, 并且指定每个连接上PSCache的大小.分库分表较多的数据库,建议配置为false
maxPoolPreparedStatementPerConnectionSize: 20
maxOpenPreparedStatements: 20
connection-init-sqls: SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci
mybatis-plus:
mapper-locations: classpath:com.xxx.mapper/*Mapper.xml #指定映射文件
type-aliases-package: com.xxx.entity.dto #别名包
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
##4. 编写Mapper接口类,并继承BaseMapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Phone;
import org.springframework.stereotype.Repository;
@Repository
public interface PhoneMapper extends BaseMapper<User> {
}
##5. 编写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.example.demo.mapper.UserMapper">
</mapper>
##6. 编写Service类, 并继承ServiceImp类型。(第一个泛型是对应的Mapper类 ,第二个泛型是对应的数据库实体类型)
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Phone;
import com.example.demo.mapper.PhoneMapper;
import org.springframework.stereotype.Service;
@Service
public class PhoneService extends ServiceImpl<PhoneMapper, Phone> {
}
##7. 给实体类型加上注解
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
//指定实体类对应的表名
@TableName("tb_phone")
public class Phone {
//主键,且属于自增型
@TableId(type = IdType.AUTO)
@TableField
private Long id;
@TableField
private Long brandId;
@TableField
private String name;
}
##8. 添加分页插件, 在@Configuration配置类中添加如下代码
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
##9.添加逻辑删除
在逻辑删除字段上添加注解@TableLogic即可。
@TableLogic
private Boolean isDeleted;