This commit is contained in:
lu7213 committed 2026-03-05 22:42:55 +08:00
1 parent 7cd697187c
commit 6c73388fdd
25 files changed
+454 -33

No files matched your search

@@ -0,0 +1,38 @@
package com.luqj.ec.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.luqj.ec.domain.dto.EcDeviceHeartbeatDto;
import com.luqj.ec.domain.dto.EcDeviceReportDto;
import com.luqj.ec.service.IEcDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ec/device-api")
public class EcDeviceApiController extends BaseController {
@Autowired
private IEcDeviceService ecDeviceService;
@PostMapping("/report")
public AjaxResult report(@RequestBody EcDeviceReportDto reportDto) {
if (reportDto.getDeviceCode() == null || reportDto.getDeviceCode().isEmpty()) {
return AjaxResult.error("deviceCode不能为空");
}
ecDeviceService.reportDevice(reportDto);
return AjaxResult.success("设备上报成功");
}
@PostMapping("/heartbeat")
public AjaxResult heartbeat(@RequestBody EcDeviceHeartbeatDto heartbeatDto) {
if (heartbeatDto.getDeviceCode() == null || heartbeatDto.getDeviceCode().isEmpty()) {
return AjaxResult.error("deviceCode不能为空");
}
ecDeviceService.heartbeat(heartbeatDto);
return AjaxResult.success("心跳成功");
}
}
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -18,6 +21,7 @@ public class EcCommandResult extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 命令ID */
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -21,6 +24,7 @@ public class EcCommandResultPlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 命令ID */
@Excel(name = "命令ID")
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -18,6 +21,7 @@ public class EcControlTask extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 任务名称 */
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -21,6 +24,7 @@ public class EcControlTaskPlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 任务名称 */
@Excel(name = "任务名称")
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -18,6 +21,7 @@ public class EcDevice extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 设备唯一标识 */
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -18,6 +21,7 @@ public class EcDeviceCommand extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 设备ID */
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -21,6 +24,7 @@ public class EcDeviceCommandPlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 设备ID */
@Excel(name = "设备ID")
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -21,6 +24,7 @@ public class EcDevicePlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 设备唯一标识 */
@Excel(name = "设备唯一标识")
@@ -2,6 +2,9 @@ package com.luqj.ec.domain;
import java.math.BigDecimal;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -19,6 +22,7 @@ public class EcDeviceStatus extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 设备ID */
@@ -2,6 +2,9 @@ package com.luqj.ec.domain;
import java.math.BigDecimal;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -22,6 +25,7 @@ public class EcDeviceStatusPlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 设备ID */
@Excel(name = "设备ID")
@@ -1,5 +1,7 @@
package com.luqj.ec.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
@@ -16,6 +18,7 @@ public class EcTaskCommand extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 任务ID */
@@ -1,5 +1,7 @@
package com.luqj.ec.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
@@ -19,6 +21,7 @@ public class EcTaskCommandPlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 任务ID */
@Excel(name = "任务ID")
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -18,6 +21,7 @@ public class EcTaskDevice extends BaseEntity
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 任务ID */
@@ -1,6 +1,9 @@
package com.luqj.ec.domain;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -21,6 +24,7 @@ public class EcTaskDevicePlus implements Serializable
private static final long serialVersionUID = 1L;
/** 主键 */
@TableId(type = IdType.AUTO)
private Long id;
/** 任务ID */
@Excel(name = "任务ID")
@@ -0,0 +1,26 @@
package com.luqj.ec.domain.dto;
import java.io.Serializable;
public class EcDeviceHeartbeatDto implements Serializable {
private static final long serialVersionUID = 1L;
private String deviceCode;
private String ipAddress;
public String getDeviceCode() {
return deviceCode;
}
public void setDeviceCode(String deviceCode) {
this.deviceCode = deviceCode;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
@@ -0,0 +1,125 @@
package com.luqj.ec.domain.dto;
import java.io.Serializable;
public class EcDeviceReportDto implements Serializable {
private static final long serialVersionUID = 1L;
private String deviceCode;
private String brand;
private String model;
private String osType;
private String osVersion;
private String appVersion;
private String ipAddress;
private Integer widthNum;
private Integer heightNum;
private Integer controlType;
private Long appId;
private Long userId;
private String secretKey;
public String getDeviceCode() {
return deviceCode;
}
public void setDeviceCode(String deviceCode) {
this.deviceCode = deviceCode;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getOsType() {
return osType;
}
public void setOsType(String osType) {
this.osType = osType;
}
public String getOsVersion() {
return osVersion;
}
public void setOsVersion(String osVersion) {
this.osVersion = osVersion;
}
public String getAppVersion() {
return appVersion;
}
public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public Integer getWidthNum() {
return widthNum;
}
public void setWidthNum(Integer widthNum) {
this.widthNum = widthNum;
}
public Integer getHeightNum() {
return heightNum;
}
public void setHeightNum(Integer heightNum) {
this.heightNum = heightNum;
}
public Integer getControlType() {
return controlType;
}
public void setControlType(Integer controlType) {
this.controlType = controlType;
}
public Long getAppId() {
return appId;
}
public void setAppId(Long appId) {
this.appId = appId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}
@@ -1,21 +1,25 @@
package com.luqj.ec.service;
import java.util.Map;
import java.util.List;
import com.luqj.ec.domain.EcDevice;
import com.luqj.ec.domain.EcDevicePlus;
import com.luqj.ec.domain.dto.EcDeviceHeartbeatDto;
import com.luqj.ec.domain.dto.EcDeviceReportDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ruoyi.common.select.SelectItem;
/**
* 设备(手机设备)Service接口
*
* @author luqj
* @date 2026-02-27
*/
public interface IEcDeviceService extends IService<EcDevicePlus>
{
public interface IEcDeviceService extends IService<EcDevicePlus> {
List<SelectItem> select(String keyword);
/**
* 查询设备(手机设备)
*
@@ -86,4 +90,10 @@ public interface IEcDeviceService extends IService<EcDevicePlus>
Long count(String where);
<T> List<T> countByGroupBy(String select, String where, String groupby, Class<T> tClass);
EcDevicePlus selectEcDeviceByDeviceCode(String deviceCode);
void reportDevice(EcDeviceReportDto reportDto);
void heartbeat(EcDeviceHeartbeatDto heartbeatDto);
}
@@ -1,4 +1,5 @@
package com.luqj.ec.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -6,6 +7,7 @@ import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import com.ruoyi.common.utils.StringUtils;
import com.alibaba.fastjson.JSONArray;
@@ -16,10 +18,13 @@ import org.springframework.stereotype.Service;
import com.luqj.ec.mapper.EcDeviceMapper;
import com.luqj.ec.domain.EcDevice;
import com.luqj.ec.domain.EcDevicePlus;
import com.luqj.ec.domain.dto.EcDeviceHeartbeatDto;
import com.luqj.ec.domain.dto.EcDeviceReportDto;
import com.luqj.ec.service.IEcDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import com.ruoyi.common.select.SelectItem;
/**
* 设备(手机设备)Service业务层处理
*
@@ -27,8 +32,7 @@ import com.ruoyi.common.select.SelectItem;
* @date 2026-02-27
*/
@Service
public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlus> implements IEcDeviceService
{
public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlus> implements IEcDeviceService {
@Autowired
private EcDeviceMapper ecDeviceMapper;
@Autowired
@@ -40,14 +44,15 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
.select(columns).in(EcDevicePlus::getId, ids);
return this.list(lambdaQueryWrapper);
}
@Override
public List<SelectItem> select(String keyword) {
LambdaQueryWrapper<EcDevicePlus> lambdaQueryWrapper = new LambdaQueryWrapper<EcDevicePlus>()
.select(EcDevicePlus::getId, EcDevicePlus::getDeviceCode)
.like(EcDevicePlus::getDeviceCode, keyword);
List<EcDevicePlus> list = this.list(lambdaQueryWrapper);
List<SelectItem> result =new ArrayList<>();
for (EcDevicePlus item : list){
List<SelectItem> result = new ArrayList<>();
for (EcDevicePlus item : list) {
SelectItem selectItem = new SelectItem();
selectItem.setName(item.getDeviceCode());
selectItem.setId(item.getId());
@@ -55,6 +60,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
}
return result;
}
@Override
public EcDevicePlus getById(Long id, SFunction<EcDevicePlus, ?>... columns) {
LambdaQueryWrapper<EcDevicePlus> lambdaQueryWrapper = new LambdaQueryWrapper<EcDevicePlus>()
@@ -84,7 +90,8 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
@Override
public List<EcDevicePlus> getByIds(List<Long> ids) {
LambdaQueryWrapper<EcDevicePlus> lambdaQueryWrapper = new LambdaQueryWrapper<EcDevicePlus>().in(EcDevicePlus::getId, ids);
LambdaQueryWrapper<EcDevicePlus> lambdaQueryWrapper = new LambdaQueryWrapper<EcDevicePlus>()
.in(EcDevicePlus::getId, ids);
return this.list(lambdaQueryWrapper);
}
@@ -131,6 +138,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
List<T> tList = JSONArray.parseArray(JSONArray.toJSONString(result), tClass);
return tList;
}
/**
* 查询设备(手机设备)
*
@@ -138,8 +146,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
* @return 设备(手机设备)
*/
@Override
public EcDevice selectEcDeviceById(Long id)
{
public EcDevice selectEcDeviceById(Long id) {
return ecDeviceMapper.selectEcDeviceById(id);
}
@@ -150,8 +157,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
* @return 设备(手机设备)
*/
@Override
public List<EcDevice> selectEcDeviceList(EcDevice ecDevice)
{
public List<EcDevice> selectEcDeviceList(EcDevice ecDevice) {
return ecDeviceMapper.selectEcDeviceList(ecDevice);
}
@@ -162,8 +168,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
* @return 结果
*/
@Override
public int insertEcDevice(EcDevice ecDevice)
{
public int insertEcDevice(EcDevice ecDevice) {
return ecDeviceMapper.insertEcDevice(ecDevice);
}
@@ -174,8 +179,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
* @return 结果
*/
@Override
public int updateEcDevice(EcDevice ecDevice)
{
public int updateEcDevice(EcDevice ecDevice) {
return ecDeviceMapper.updateEcDevice(ecDevice);
}
@@ -186,8 +190,7 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
* @return 结果
*/
@Override
public int deleteEcDeviceByIds(Long[] ids)
{
public int deleteEcDeviceByIds(Long[] ids) {
return ecDeviceMapper.deleteEcDeviceByIds(ids);
}
@@ -198,8 +201,74 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
* @return 结果
*/
@Override
public int deleteEcDeviceById(Long id)
{
public int deleteEcDeviceById(Long id) {
return ecDeviceMapper.deleteEcDeviceById(id);
}
@Override
public EcDevicePlus selectEcDeviceByDeviceCode(String deviceCode) {
LambdaQueryWrapper<EcDevicePlus> queryWrapper = new LambdaQueryWrapper<EcDevicePlus>()
.eq(EcDevicePlus::getDeviceCode, deviceCode);
return this.getOne(queryWrapper);
}
@Override
public void reportDevice(EcDeviceReportDto reportDto) {
EcDevicePlus existDevice = selectEcDeviceByDeviceCode(reportDto.getDeviceCode());
Date now = new Date();
if (existDevice != null) {
existDevice.setBrand(reportDto.getBrand());
existDevice.setModel(reportDto.getModel());
existDevice.setOsType(reportDto.getOsType());
existDevice.setOsVersion(reportDto.getOsVersion());
existDevice.setAppVersion(reportDto.getAppVersion());
existDevice.setIpAddress(reportDto.getIpAddress());
existDevice.setWidthNum(reportDto.getWidthNum());
existDevice.setHeightNum(reportDto.getHeightNum());
existDevice.setControlType(reportDto.getControlType());
existDevice.setAppId(reportDto.getAppId());
existDevice.setUserId(reportDto.getUserId());
existDevice.setSecretKey(reportDto.getSecretKey());
existDevice.setStatus(1);
existDevice.setLastOnlineTime(now);
existDevice.setUpdatedAt(now);
this.updateById(existDevice);
} else {
EcDevicePlus newDevice = new EcDevicePlus();
newDevice.setDeviceCode(reportDto.getDeviceCode());
newDevice.setBrand(reportDto.getBrand());
newDevice.setModel(reportDto.getModel());
newDevice.setOsType(reportDto.getOsType());
newDevice.setOsVersion(reportDto.getOsVersion());
newDevice.setAppVersion(reportDto.getAppVersion());
newDevice.setIpAddress(reportDto.getIpAddress());
newDevice.setWidthNum(reportDto.getWidthNum());
newDevice.setHeightNum(reportDto.getHeightNum());
newDevice.setControlType(reportDto.getControlType());
newDevice.setAppId(reportDto.getAppId());
newDevice.setUserId(reportDto.getUserId());
newDevice.setSecretKey(reportDto.getSecretKey());
newDevice.setStatus(1);
newDevice.setLastOnlineTime(now);
newDevice.setCreatedAt(now);
newDevice.setUpdatedAt(now);
this.save(newDevice);
}
}
@Override
public void heartbeat(EcDeviceHeartbeatDto heartbeatDto) {
EcDevicePlus existDevice = selectEcDeviceByDeviceCode(heartbeatDto.getDeviceCode());
if (existDevice != null) {
Date now = new Date();
existDevice.setStatus(1);
existDevice.setLastOnlineTime(now);
existDevice.setUpdatedAt(now);
if (StringUtils.isNotBlank(heartbeatDto.getIpAddress())) {
existDevice.setIpAddress(heartbeatDto.getIpAddress());
}
this.updateById(existDevice);
}
}
}
+1
View File
@@ -0,0 +1 @@
luqj-ec这个模块是群控系统的模块,现在需要为群控系统设备EcDevice添加设备上报接口,和心跳接口,通过deviceCode来识别设备。
@@ -134,11 +134,10 @@ mybatis-plus:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
# 逻辑删除配置
identifier-generator: com.ruoyi.framework.config.CustomKeyGenerator
db-config:
# 删除前
id-type: auto
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
# PageHelper分页插件
pagehelper:
+5
View File
@@ -78,6 +78,11 @@
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
@@ -0,0 +1,80 @@
package com.ruoyi.framework.config;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicLong;
@Slf4j
@Component
public class CustomKeyGenerator implements IdentifierGenerator {
private final ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
private final AtomicLong atomicLong = new AtomicLong(System.currentTimeMillis());
@Override
public Number nextId(Object entity) {
IdType idType = getIdTypeFromEntity(entity);
if (idType == null || idType == IdType.AUTO) {
return null;
}
switch (idType) {
case ASSIGN_ID:
return generateSnowflakeId();
case ASSIGN_UUID:
return null;
case INPUT:
return null;
case ID_WORKER:
return generateSnowflakeId();
case ID_WORKER_STR:
return generateSnowflakeId();
case UUID:
return null;
default:
return null;
}
}
@Override
public String nextUUID(Object entity) {
IdType idType = getIdTypeFromEntity(entity);
if (idType == IdType.ASSIGN_UUID || idType == IdType.UUID) {
return java.util.UUID.randomUUID().toString().replace("-", "");
}
return null;
}
private IdType getIdTypeFromEntity(Object entity) {
if (entity == null) {
return null;
}
Class<?> clazz = entity.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
TableId tableId = field.getAnnotation(TableId.class);
if (tableId != null) {
return tableId.type();
}
}
return null;
}
private Long generateSnowflakeId() {
return atomicLong.incrementAndGet();
}
}
@@ -1,6 +1,9 @@
package com.ruoyi.framework.config;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.ruoyi.common.utils.StringUtils;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
@@ -25,16 +28,14 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* Mybatis支持*匹配扫描包
*
* @author ruoyi
*/
@Configuration
public class MyBatisConfig {
@Autowired
private Environment env;
@Autowired
private MybatisPlusProperties mybatisPlusProperties;
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
public static String setTypeAliasesPackage(String typeAliasesPackage) {
@@ -45,7 +46,8 @@ public class MyBatisConfig {
for (String aliasesPackage : typeAliasesPackage.split(",")) {
List<String> result = new ArrayList<String>();
aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/"
+ DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resolver.getResources(aliasesPackage);
if (resources != null && resources.length > 0) {
MetadataReader metadataReader = null;
@@ -53,7 +55,8 @@ public class MyBatisConfig {
if (resource.isReadable()) {
metadataReader = metadataReaderFactory.getMetadataReader(resource);
try {
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage()
.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
@@ -68,7 +71,8 @@ public class MyBatisConfig {
if (allResult.size() > 0) {
typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
} else {
throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
throw new RuntimeException(
"mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
}
} catch (IOException e) {
e.printStackTrace();
@@ -93,7 +97,8 @@ public class MyBatisConfig {
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor paginationInterceptor) throws Exception {
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor paginationInterceptor,
CustomKeyGenerator customKeyGenerator) throws Exception {
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
String mapperLocations = env.getProperty("mybatis.mapperLocations");
String configLocation = env.getProperty("mybatis.configLocation");
@@ -106,6 +111,15 @@ public class MyBatisConfig {
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
sessionFactory.setPlugins(paginationInterceptor);
return sessionFactory.getObject();
GlobalConfig globalConfig = mybatisPlusProperties.getGlobalConfig();
if (globalConfig == null) {
globalConfig = new GlobalConfig();
}
globalConfig.setIdentifierGenerator(customKeyGenerator);
sessionFactory.setGlobalConfig(globalConfig);
SqlSessionFactory sqlSessionFactory = sessionFactory.getObject();
return sqlSessionFactory;
}
}
@@ -128,7 +128,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/life/housingEstate/all-by-letter").permitAll()
.antMatchers("/system/config/getVerifyStatus").permitAll()
.antMatchers("/system/config/setVerifyStatus").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.antMatchers("/ec/device-api/**").anonymous()
.anyRequest().authenticated()
.and()
.headers().frameOptions().disable();