Mybatis-Plus总结与开发实战思考
2020-11-23 10:31:20 0 举报
AI智能生成
后台ORM框架Mybatis-Plus框架介绍,JAVA开发经验总结与思考
作者其他创作
大纲/内容
简介
官网
https://baomidou.com/
特点
MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官方样例代码
https://github.com/baomidou/mybatis-plus-samples.git
分支主题
开发与实践
CRUD接口
Service CRUD接口
.save(T t);
.saveOrUpdate(T t);
先查再更新
Mapper CRUD接口
extends BaseMapper<T>
***Dao.insert(T t);
常用注解
@TableName
@TableId
Id生成器
IdType枚举类
AUTO
NONE
INPUT
ASSIGNED_ID
ASSIGNED_UUID
@TableField
exist
表中没有实体类字段对应的列的时候
value
默认数据库下划线---实体类驼峰
insertStrategy
updateStrategy
允许更新为null?
@TableLogic
逻辑删除
@Version
乐观锁
高并发
条件构造器
使用场景
简单的查询
多表关联
XML实现
AbstractWrapper
QueryWrapper
UpdateWrapper
常用
相等/不等
.eq
.ne
.isNull
.isNotNull
包含
.like
.notLike
.likeLeft
.likeRight
比较
.ne
.ge
.between
关联条件
.or
.and
默认
分页插件
PaginationInterceptor
结果样例
示例
数据库信息加密
Since 3.3.2
import com.baomidou.mybatisplus.core.toolkit.AES;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class EncryptUtil {
private static final String RANDOM_KEY = AES.generateRandomKey();
public static String encryptData(String data) {
if (log.isInfoEnabled()) {
log.info(RANDOM_KEY);
}
return AES.encrypt(data, RANDOM_KEY);
}
}
// Jar 启动参数( idea 设置 Program arguments , 服务器可以设置为启动环境变量 )
--mpw.key=${RANDOM_KEY}
开发经验与思考
接口接参
Data Binding
Jackson Json
Java 8 Date & Time API
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.12.0-rc1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.12.0-rc1</version>
</dependency>
FastJson
JSON.toJSONStringWithDateFormat(this, JSON.DEFFAULT_DATE_FORMAT);
下划线
@JsonProperty
多个key映射到一个字段
@JsonAlias
JSON与实体类转换
JSONObject转实体类
实体类转JSON字符串
样例
实体类Apple
package src;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
/**
* @author Jjn
* @date 2020-09-12 10:54
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class Apple {
private Integer weight;
@JsonProperty(value = "category_name")
private String categoryName;
@JsonProperty(value = "fruit_category")
private Integer fruitCategory;
@JsonProperty(value = "sale_price")
private Double salePrice;
@Override
public String toString() {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
}
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
/**
* @author Jjn
* @date 2020-09-12 10:54
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class Apple {
private Integer weight;
@JsonProperty(value = "category_name")
private String categoryName;
@JsonProperty(value = "fruit_category")
private Integer fruitCategory;
@JsonProperty(value = "sale_price")
private Double salePrice;
@Override
public String toString() {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
}
样例转换
package src;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* @author Jjn
* @date 2020-10-28 16:56
*/
public class JsonTest {
public static void main(String[] args) {
Apple apple = Apple.builder().fruitCategory(1).salePrice(6.8).categoryName("红富士").weight(100).build();
String s = apple.toString();
System.out.println(s);
JSONObject jsonObject = JSON.parseObject(s);
Apple javaObject = jsonObject.toJavaObject(Apple.class);
System.out.println(javaObject.getCategoryName() + ":" + javaObject.getFruitCategory() + ":" + javaObject.getWeight());
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* @author Jjn
* @date 2020-10-28 16:56
*/
public class JsonTest {
public static void main(String[] args) {
Apple apple = Apple.builder().fruitCategory(1).salePrice(6.8).categoryName("红富士").weight(100).build();
String s = apple.toString();
System.out.println(s);
JSONObject jsonObject = JSON.parseObject(s);
Apple javaObject = jsonObject.toJavaObject(Apple.class);
System.out.println(javaObject.getCategoryName() + ":" + javaObject.getFruitCategory() + ":" + javaObject.getWeight());
}
}
插件
MybatisX
Mapper与Dao对应关系
POJO to Json
User
{
"userName": "",
"orgId": "",
"orgName": "",
"age": 0,
"email": "",
"createTime": "2020-11-07 11:52:07",
"updateTime": "2020-11-07 11:52:07",
"registered": false,
"registerTime": "2020-11-07 11:52:07"
}
Maven Helper
Jar包冲突
RainBow Brackets
Code Glance
IntelliJ IDEA快捷键
.var
.null
.nn
.if
.for
.fori
.sout
参考文档
Mybatis-PLUS课程
https://www.bilibili.com/video/BV17E411N7KN
收藏
0 条评论
下一页