Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite支持 #373

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions campus-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.oddfar.campus.common.core;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* spring redis 工具类
Expand All @@ -21,13 +27,25 @@ public class RedisCache {
@Autowired
public RedisTemplate redisTemplate;

@Value("${spring.redis.disabled:false}")
private boolean redisDisabled;


//创建字典本地缓存,过期时间10分钟
TimedCache<String, Object> dictLocalCache = CacheUtil.newTimedCache(DateUnit.HOUR.getMillis() * 10);


/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setCacheObject(final String key, final T value) {
if (redisDisabled) {
dictLocalCache.put(key, value);
return;
}
redisTemplate.opsForValue().set(key, value);
}

Expand All @@ -40,6 +58,10 @@ public <T> void setCacheObject(final String key, final T value) {
* @param timeUnit 时间颗粒度
*/
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
if (redisDisabled) {
dictLocalCache.put(key, value, timeUnit.toMillis(timeout));
return;
}
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}

Expand All @@ -63,6 +85,14 @@ public boolean expire(final String key, final long timeout) {
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
if (redisDisabled) {
Object value = dictLocalCache.get(key);
if (value == null) {
return true;
}
dictLocalCache.put(key, value, unit.toMillis(timeout));
return true;
}
return redisTemplate.expire(key, timeout, unit);
}

Expand All @@ -83,6 +113,9 @@ public long getExpire(final String key) {
* @return true 存在 false不存在
*/
public Boolean hasKey(String key) {
if (redisDisabled) {
return dictLocalCache.containsKey(key);
}
return redisTemplate.hasKey(key);
}

Expand All @@ -93,8 +126,19 @@ public Boolean hasKey(String key) {
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
Object cachedObject = null;
if (redisDisabled) {
cachedObject = dictLocalCache.get(key);
if (cachedObject != null) {
return JSONObject.parseObject(JSON.toJSONString(cachedObject), (Class<T>) cachedObject.getClass());
}
return null;
}
cachedObject = redisTemplate.opsForValue().get(key);
if (cachedObject != null) {
return JSONObject.parseObject(JSON.toJSONString(cachedObject), (Class<T>) cachedObject.getClass());
}
return null;
}

/**
Expand All @@ -103,6 +147,10 @@ public <T> T getCacheObject(final String key) {
* @param key
*/
public boolean deleteObject(final String key) {
if (redisDisabled) {
dictLocalCache.remove(key);
return true;
}
return redisTemplate.delete(key);
}

Expand All @@ -113,6 +161,12 @@ public boolean deleteObject(final String key) {
* @return
*/
public boolean deleteObject(final Collection collection) {
if (redisDisabled) {
for (Object key : collection) {
dictLocalCache.remove(String.valueOf(key));
}
return true;
}
return redisTemplate.delete(collection) > 0;
}

Expand All @@ -124,6 +178,10 @@ public boolean deleteObject(final Collection collection) {
* @return 缓存的对象
*/
public <T> long setCacheList(final String key, final List<T> dataList) {
if (redisDisabled) {
dictLocalCache.put(key, JSON.toJSONString(dataList));
return dataList.size();
}
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
Expand All @@ -137,6 +195,10 @@ public <T> long setCacheList(final String key, final List<T> dataList) {
*/
public <T> long reSetCacheList(final String key, final List<T> dataList) {
this.deleteObject(key);
if (redisDisabled) {
dictLocalCache.put(key, dataList);
return dataList.size();
}
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
Expand All @@ -148,6 +210,10 @@ public <T> long reSetCacheList(final String key, final List<T> dataList) {
* @return 缓存键值对应的数据
*/
public <T> List<T> getCacheList(final String key) {
if (redisDisabled) {
Object obj = dictLocalCache.get(key);
return (List<T>) obj;
}
return redisTemplate.opsForList().range(key, 0, -1);
}

Expand Down Expand Up @@ -251,6 +317,17 @@ public boolean deleteCacheMapValue(final String key, final String hKey) {
* @return 对象列表
*/
public Collection<String> keys(final String pattern) {
if (redisDisabled) {
Set<String> stringSet = dictLocalCache.keySet();
// 将pattern转换为正则表达式,其中*表示任意数量的字符
String regex = String.format("^%s.*", pattern.replace("*", ".*"));
// 使用Java 8 Stream API和正则表达式过滤出匹配的键
Set<String> filteredKeys = stringSet.stream()
.filter(key -> key.matches(regex))
.collect(Collectors.toSet());

return filteredKeys;
}
return redisTemplate.keys(pattern);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.oddfar.campus.common.utils;

import com.alibaba.fastjson2.JSONArray;
import com.oddfar.campus.common.constant.CacheConstants;
import com.oddfar.campus.common.core.RedisCache;
import com.oddfar.campus.common.domain.entity.SysDictDataEntity;
Expand All @@ -10,7 +9,6 @@

/**
* 字典工具类
*
*/
public class DictUtils {
/**
Expand All @@ -35,11 +33,9 @@ public static void setDictCache(String key, List<SysDictDataEntity> dictDatas) {
* @return dictDatas 字典数据列表
*/
public static List<SysDictDataEntity> getDictCache(String key) {
JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(arrayCache)) {
return arrayCache.toList(SysDictDataEntity.class);
}
return null;
List<SysDictDataEntity> arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));

return arrayCache;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.oddfar.campus.framework.handler.DateTypeHandler;
import com.oddfar.campus.framework.handler.MyDBFieldHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("${mybatis-plus.mapperPackage}")
public class MybatisPlusConfig {

// driverClassName如果是SQLite,需要对时间转换
@Value("${spring.datasource.dynamic.datasource.master.driverClassName}")
private String driverClassName;

/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
Expand All @@ -34,5 +40,13 @@ public MetaObjectHandler defaultMetaObjectHandler() {
return new MyDBFieldHandler();
}

@Bean
public DateTypeHandler defaultDateTypeHandler() {
if (driverClassName.contains("sqlite")) {
return new DateTypeHandler();
}
return null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.oddfar.campus.framework.handler;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTypeHandler extends BaseTypeHandler<Date> {

private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, FORMATTER.format(parameter));
}

@Override
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
String dateTimeString = rs.getString(columnName);
return parseDate(dateTimeString);
}

@Override
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String dateTimeString = rs.getString(columnIndex);
return parseDate(dateTimeString);
}

@Override
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String dateTimeString = cs.getString(columnIndex);
return parseDate(dateTimeString);
}

private Date parseDate(String dateTimeString) {
if (dateTimeString == null) {
return null;
}
if (NumberUtil.isNumber(dateTimeString)) {
return DateUtil.date(Long.valueOf(dateTimeString));
}
try {
return FORMATTER.parse(dateTimeString);
} catch (ParseException e) {
throw new RuntimeException("Error parsing date string: " + dateTimeString, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default PageResult<SysResourceEntity> selectPage(SysResourceEntity resource) {
/**
* 清空 sys_resource 数据库
*/
@Update("truncate table sys_resource")
@Update("delete from sys_resource")
void truncateResource();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


<update id="cleanLogininfor">
truncate table sys_log_login
delete from sys_log_login
</update>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</resultMap>

<update id="cleanOperLog">
truncate table sys_log_oper
delete from sys_log_oper
</update>

</mapper>
4 changes: 4 additions & 0 deletions campus-modular/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>com.oddfar.campus</groupId>
<artifactId>campus-admin</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
*/
public interface IItemMapper extends BaseMapperX<IItem> {
//清空指定表
@Update("truncate table i_item")
@Update("delete from i_item")
void truncateItem();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ default PageResult<ILog> selectPage(ILog iLog, Long userId) {

}

@Select("truncate table i_log")
@Select("delete from i_log")
void cleanLog();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public interface IShopMapper extends BaseMapperX<IShop> {
//清空指定表
@Update("truncate table i_shop")
@Update("delete from i_shop")
void truncateShop();

default PageResult<IShop> selectPage(IShop iShop) {
Expand Down
Loading