release: mybatis-plus优化

release-2024-08-08
huangyw 4 months ago
parent 710ae46e99
commit 446c0a0771

@ -8,8 +8,13 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date; import java.util.Date;
import java.util.Objects;
/** /**
* Created by Administrator on 2018/4/11. * Created by Administrator on 2018/4/11.
@ -86,4 +91,47 @@ public class JwtUtil {
return null; return null;
} }
} }
public UserLoginDto getUser() {
return getUserLoginDto(getToken());
}
public static String getToken()
{
return getToken(Objects.requireNonNull(getRequest()));
}
/**
* requesttoken
*/
public static String getToken(HttpServletRequest request)
{
// 从header获取token标识
return request.getHeader("Authorization");
}
public static HttpServletRequest getRequest()
{
try
{
return getRequestAttributes().getRequest();
}
catch (Exception e)
{
return null;
}
}
public static ServletRequestAttributes getRequestAttributes()
{
try
{
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
}
catch (Exception e)
{
return null;
}
}
} }

@ -0,0 +1,28 @@
package com.luoo.user.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class MyBatisPlusConfig {
/**
*
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

@ -0,0 +1,52 @@
package com.luoo.user.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.JwtUtil;
import java.time.LocalDateTime;
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Autowired
private JwtUtil jwtUtil;
@Override
public void insertFill(MetaObject metaObject) {
String operator = null;
String operatorName = "";
try {
operator = jwtUtil.getUser().getUserId();
operatorName = jwtUtil.getUser().getNickName();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
this.setFieldValByName("createUser", operator, metaObject);
this.setFieldValByName("createUserName", operatorName, metaObject);
this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
this.setFieldValByName("updateUser", operator, metaObject);
this.setFieldValByName("updateUserName", operatorName, metaObject);
this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
this.setFieldValByName("delFlag", 0, metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
String operator = null;
String operatorName = "";
try {
operator = jwtUtil.getUser().getUserId();
operatorName = jwtUtil.getUser().getNickName();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
this.setFieldValByName("updateUser", operator, metaObject);
this.setFieldValByName("updateUserName", operatorName, metaObject);
this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
}
}

@ -1,13 +1,14 @@
package com.luoo.user.pojo; package com.luoo.user.pojo;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.annotation.JsonFormat;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
/** /**
@ -42,79 +43,31 @@ public class WithdrawMybatis implements Serializable {
/** /**
* *
*/ */
private Date createTime; @TableField(fill = FieldFill.INSERT, value = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
/** /**
* *
*/ */
private Date updateTime; @TableField(fill = FieldFill.INSERT_UPDATE, value = "update_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
/** /**
* *
*/ */
@TableField(fill = FieldFill.INSERT, value = "create_user")
private String createUser; private String createUser;
/** /**
* *
*/ */
@TableField(fill = FieldFill.INSERT_UPDATE, value = "update_user")
private String updateUser; private String updateUser;
@TableField(exist = false) @TableField(exist = false)
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Withdraw other = (Withdraw) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getState() == null ? other.getState() == null : this.getState().equals(other.getState()))
&& (this.getAmount() == null ? other.getAmount() == null : this.getAmount().equals(other.getAmount()))
&& (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
&& (this.getCreateUser() == null ? other.getCreateUser() == null : this.getCreateUser().equals(other.getCreateUser()))
&& (this.getUpdateUser() == null ? other.getUpdateUser() == null : this.getUpdateUser().equals(other.getUpdateUser()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getState() == null) ? 0 : getState().hashCode());
result = prime * result + ((getAmount() == null) ? 0 : getAmount().hashCode());
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
result = prime * result + ((getCreateUser() == null) ? 0 : getCreateUser().hashCode());
result = prime * result + ((getUpdateUser() == null) ? 0 : getUpdateUser().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", state=").append(state);
sb.append(", amount=").append(amount);
sb.append(", userId=").append(userId);
sb.append(", createTime=").append(createTime);
sb.append(", updateTime=").append(updateTime);
sb.append(", createUser=").append(createUser);
sb.append(", updateUser=").append(updateUser);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
} }

@ -195,7 +195,7 @@ public class UserPointLogService {
* @param token token * @param token token
*/ */
public void dailySign(String token) { public void dailySign(String token) {
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); UserLoginDto userLoginDto = jwtUtil.getUser();
UserPointLog todayByUserIdAndTaskPointId = userPointLogDao.findTodayByUserIdAndTaskPointId(userLoginDto.getUserId(), TaskPointIdConstants.DAILY_SIGN); UserPointLog todayByUserIdAndTaskPointId = userPointLogDao.findTodayByUserIdAndTaskPointId(userLoginDto.getUserId(), TaskPointIdConstants.DAILY_SIGN);
if (todayByUserIdAndTaskPointId == null) { if (todayByUserIdAndTaskPointId == null) {

@ -15,30 +15,24 @@ oauth2:
secret: 741727f12d1b262ac855b905bf2e60e2 secret: 741727f12d1b262ac855b905bf2e60e2
appidShare: wx667f580d1605650b appidShare: wx667f580d1605650b
secretShare: 4bb42de98cb422d733ac7d50e7cade3b secretShare: 4bb42de98cb422d733ac7d50e7cade3b
#mybatis-plus
mybatis-plus: mybatis-plus:
## 这个可以不用配置,因其默认就是这个路径 mapper-locations: classpath*:mapper/**/*Mapper.xml
mapper-locations: classpath:/mapper/*Mapper.xml type-aliases-package: com.luoo.user.pojo
#实体扫描多个package用逗号或者分号分隔 checkConfigLocation : true
typeAliasesPackage: com.luoo.user.pojo configuration:
map-underscore-to-camel-case: true
jdbc-type-for-null: null
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
global-config: global-config:
# 数据库相关配置 banner: true
db-config: db-config:
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: ASSIGN_ID id-type: ASSIGN_ID
#字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断" table-underline: true
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置
logic-delete-value: 2
logic-not-delete-value: 0 logic-not-delete-value: 0
db-type: h2 logic-delete-value: 2
#刷新mapper 调试神器
refresh: true pagehelper:
# 原生配置 helperDialect: mysql
configuration: supportMethodsArguments: true
map-underscore-to-camel-case: true params: count=countSql
cache-enabled: false
Loading…
Cancel
Save