【MyBatis-Plus】MyBatis-Plus插件机制以及通用Service、新功能(二)

简介: 【MyBatis-Plus】MyBatis-Plus插件机制以及通用Service、新功能(二)

2.通用Service

       2.1分析

  • 通用Service分析

image.png

2.2基本使用

  • 标准service:接口 + 实现

image.png

service接口

package com.czxy.service;
  import com.baomidou.mybatisplus.extension.service.IService;
  import com.czxy.domain.Customer;
  public interface CustomerService extends IService<Customer> {
  }

service实现类

package com.czxy.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.domain.Customer;
import com.czxy.mapper.CustomerMapper;
import com.czxy.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper,Customer> implements CustomerService {
}

     2.3常见方法

  • 查询所有
  • 添加
  • 修改
  • 删除
package com.czxy.test;
import com.czxy.mp.Day62MybatisPlusApplication;
import com.czxy.mp.domain.Customer;
import com.czxy.mp.service.CustomerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Day62MybatisPlusApplication.class)
public class TestDay62CustomerService {
    @Resource
    private CustomerService customerService;
    @Test
    public void testSelectList() {
        List<Customer> list = customerService.list();
        list.forEach(System.out::println);
    }
    @Test
    public void testInsert() {
        Customer customer = new Customer();
        customer.setCname("张三");
        customer.setPassword("9999");
        // 添加
        customerService.save(customer);
    }
    @Test
    public void testUpdate() {
        Customer customer = new Customer();
        customer.setCid(14);
        customer.setCname("777");
        customer.setPassword("777");
        customerService.updateById(customer);
    }
    @Test
    public void testSaveOrUpdate() {
        Customer customer = new Customer();
        customer.setCid(15);
        customer.setCname("999");
        customer.setPassword("99");
        customerService.saveOrUpdate(customer);
    }
    @Test
    public void testDelete() {
        customerService.removeById(15);
    }
}

3.新功能

       3.1执行SQL分析打印

该功能依赖 p6spy 组件,完美的输出打印 SQL 及执行时长

  • p6spy 依赖引入
<dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.9.1</version>
</dependency>

核心yml配置

spring:
  datasource:
    # p6spy 提供的驱动代理类,
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    # url 固定前缀为 jdbc:p6spy,跟着冒号为对应数据库连接地址
    url: jdbc:p6spy:mysql://127.0.0.1:3306...

image.png

spy.properties 配置

#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

3.2数据库安全保护

为了保护数据库配置及数据安全,在一定的程度上控制开发人员流动导致敏感信息泄露

步骤:

  • 步骤1:使用 AES 工具类,生成秘钥
  • 步骤2:使用 AES工具类,根据步骤1生成的秘钥对敏感信息进行加密
  • 步骤3:设置加密后的配置信息
  • 步骤4:启动服务时,使用秘钥

步骤1-2:使用工具类生成秘钥以及对敏感信息进行加密

package com.czxy;
import com.baomidou.mybatisplus.core.toolkit.AES;
import org.junit.Test;
public class TestAES {
    @Test
    public void testAes() {
        String randomKey = AES.generateRandomKey();
        String url =  "jdbc:p6spy:mysql://127.0.0.1:3306/zx_edu_teacher?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "1234";
        String urlAES = AES.encrypt(url, randomKey);
        String usernameAES = AES.encrypt(username, randomKey);
        String passwordAES = AES.encrypt(password, randomKey);
        System.out.println("--mpw.key=" + randomKey);
        System.out.println("mpw:" + urlAES);
        System.out.println("mpw:" + usernameAES);
        System.out.println("mpw:" + passwordAES);
    }
}
// Jar 启动参数( idea 设置 Program arguments , 服务器可以设置为启动环境变量 )
//--mpw.key=fddd2b7a67460e16
//mpw:7kSEISvq3QWfnSh6vQZc2xgE+XF/sJ0WS/sgGkYpCOTQRjO1poLi3gfmGZNOwKzfqZUec0odiwAdmxcS7lfueENGIx8OmIe//d9imrGFpnkrf8jNSHdzfNPCUi3MbmUb
//mpw:qGbCMksqA90jjiGXXRr7lA==
//mpw:xKG9GABlywqar6CGPOSJKQ==
  • 步骤3:配置加密信息

image.png

  • 步骤4:使用秘钥启动服务  

image.png


相关文章
|
3天前
MyBatis-Plus分页插件基于3.3.2
MyBatis-Plus分页插件基于3.3.2
8 0
MyBatis-Plus分页插件基于3.3.2
|
21小时前
|
SQL Java 数据库连接
IDEA插件(MyBatis Log Free)
IDEA插件(MyBatis Log Free)
8 0
|
23小时前
|
Java 数据库连接 mybatis
idea无法下载Mybatis插件怎么办
idea无法下载Mybatis插件怎么办
|
1天前
|
SQL Java 数据库连接
MyBatis插件深度解析:功能、原理、使用、应用场景与最佳实践
MyBatis插件深度解析:功能、原理、使用、应用场景与最佳实践
4 0
|
3天前
|
Java 关系型数据库 MySQL
3.MyBatis和SpringBoot整合及MyBatis-plus与SpringBoot整合
3.MyBatis和SpringBoot整合及MyBatis-plus与SpringBoot整合
11 0
|
3天前
|
Java 数据库连接 mybatis
mybatis自定义插件实现日期自动注入
mybatis自定义插件实现日期自动注入
12 0
|
3天前
|
XML 关系型数据库 数据库
使用mybatis-generator插件生成postgresql数据库model、mapper、xml
使用mybatis-generator插件生成postgresql数据库model、mapper、xml
20 0
|
4天前
|
存储 Java 数据库连接
SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展
SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展
6 0
|
1月前
|
算法 Java 数据库连接
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
|
1月前
|
Java 数据库连接 Spring
Spring 整合mybatis
Spring 整合mybatis
29 2