2.通用Service
2.1分析
- 通用Service分析
2.2基本使用
- 标准service:接口 + 实现
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...
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:配置加密信息
- 步骤4:使用秘钥启动服务