-
Notifications
You must be signed in to change notification settings - Fork 183
如何在spring中配置使用sqltoy
zhongxuchen edited this page Sep 4, 2019
·
3 revisions
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 配置辅助sql处理工具用于sql查询条件的处理 -->
<bean id="sqlToyContext" name="sqlToyContext" class="org.sagacity.sqltoy.SqlToyContext"
init-method="initialize">
<!-- 指定sql.xml 文件的路径实现目录的递归查找,非必须属性 -->
<property name="sqlResourcesDir" value="classpath:com/sagframe/portal/modules" />
<!-- 针对不同数据库函数进行转换,非必须属性 -->
<property name="functionConverts" value="default"/>
<!-- pojo 包路径,非必须属性 -->
<property name="packagesToScan">
<list>
<value>com.sagframe.portal.modules.business.vo</value>
<value>com.sagframe.portal.modules.common.vo</value>
<value>com.sagframe.portal.modules.system.vo</value>
<value>com.sagframe.portal.modules.sagacity.vo</value>
</list>
</property>
<!-- 统一公共字段赋值,原理:只做补漏,开发者已经设置了值不会被覆盖,当没有被赋值时才会起作用 -->
<property name="unifyFieldsHandler">
<bean
class="com.sagframe.portal.plugins.sqltoy.SqltoyUnifyFieldsHandler" />
</property>
<!-- elasticsearch 访问配置,非必须属性 -->
<property name="elasticEndpoints">
<list>
<bean class="org.sagacity.sqltoy.config.model.ElasticEndpoint">
<constructor-arg value="${es.default.url}"/>
<property name="id" value="default"/>
<property name="enableSql" value="true"/>
</bean>
</list>
</property>
<!-- 缓存翻译管理器,非必须属性 -->
<property name="translateConfig" value="classpath:sqltoy-translate.xml" />
<!-- 默认值为:false -->
<property name="debug" value="${sqltoy.debug}" />
<!-- 默认值为:50,提供sqltoy批量更新的batch量 -->
<property name="batchSize" value="${sqltoy.batchSize}" />
<!-- 如果是单一类型的数据库,建议dialect一定要设置,可避免不必要的数据库类型判断 -->
<property name="dialect" value="${sqltoy.dialect}" />
<!-- 默认值为:100000,设置分页查询最大的提取数据记录量,防止恶意提取数据造成系统内存压力以及保障数据安全 -->
<property name="pageFetchSizeLimit" value="50000" />
<!-- 默认dataSource -->
<property name="defaultDataSource" ref="dataSource" />
<!-- 打印sql的策略 -->
<property name="printSqlStrategy" value="debug" />
<!-- 超过多少毫秒打印sql -->
<property name="printSqlTimeoutMillis" value="20000" />
</bean>
<!-- 配置通用的dao,减少代码不必要的dao编写 -->
<bean id="sqlToyLazyDao" name="sqlToyLazyDao"
class="org.sagacity.sqltoy.dao.impl.SqlToyLazyDaoImpl" />
<!-- 配置通用的service,减少没有逻辑的简单的crud编写 -->
<bean id="sqlToyCRUDService" name="sqlToyCRUDService"
class="org.sagacity.sqltoy.service.impl.SqlToyCRUDServiceImpl" />
</beans>
import org.sagacity.core.utils.DateUtil;
import org.sagacity.exoteric.security.SpringSecurityUtils;
import org.sagacity.sqltoy.plugin.IUnifyFieldsHandler;
/**
* @project sqltoy-showcase
* @description 在sqltoy进行新增和修改操作时,对VO指定属性进行统一赋值操作(匹配不上的属性会自动跳过)
* @author wyl
* @version Revision:v1.0,Date:2018年1月18日
* @Modification Date:2018年1月18日
*/
public class SqltoyUnifyFieldsHandler implements IUnifyFieldsHandler {
/**
* 如果获取不到当前用户,填入系统默认值
*/
private String defaultUserName = "system-auto";
/*
* @todo 对象创建时统一设置创建人、创建时间、启用状态等字段值,注意:内部逻辑为:当字段值为null时进行补充赋值
* @see org.sagacity.sqltoy.plugin.IUnifyFieldsHandler#createUnifyFields()
*/
@Override
public Map<String, Object> createUnifyFields() {
HashMap<String, Object> map = new HashMap<String, Object>();
Date nowDate = DateUtil.getNowTime();
Timestamp nowTime = DateUtil.getTimestamp(null);
// 获取用户信息
String userId = getUserId();
map.put("createBy", userId);
map.put("createDate", nowDate);
map.put("createTime", nowTime);
map.put("updateBy", userId);
map.put("updateDate", nowDate);
map.put("updateTime", nowTime);
map.put("systemTime", nowTime);
// enabled 是否启用状态
map.put("enabled", 1);
return map;
}
/*
* @todo 数据库修改操作时,统一对修改人、修改时间等进行赋值,注意:内部逻辑为:当字段值为null时进行补充赋值
* @see org.sagacity.sqltoy.plugin.IUnifyFieldsHandler#updateUnifyFields()
*/
@Override
public Map<String, Object> updateUnifyFields() {
Timestamp nowTime = DateUtil.getTimestamp(null);
Map<String, Object> map = new HashMap<String, Object>();
// 获取用户信息
map.put("updateBy", getUserId());
map.put("updateTime", nowTime);
map.put("updateDate", nowTime);
map.put("systemTime", nowTime);
return map;
}
/**
* @todo 获取当前用户Id信息
* @return
*/
private String getUserId() {
//基于spring-security SecurityContextHolder
return (SpringSecurityUtils.getCurrentUser() != null) ? SpringSecurityUtils.getCurrentUser().getId()
: defaultUserName;
}
/**
* @return the defaultUserName
*/
public String getDefaultUserName() {
return defaultUserName;
}
/**
* @param defaultUserName
* the defaultUserName to set
*/
public void setDefaultUserName(String defaultUserName) {
this.defaultUserName = defaultUserName;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<sagacity xmlns="http://www.sagframe.com/schema/sqltoy-translate"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sagframe.com/schema/sqltoy-translate http://www.sagframe.com/schema/sqltoy/sqltoy-translate.xsd">
<!-- 缓存有默认失效时间,默认为1小时,因此只有较为频繁的缓存才需要及时检测 -->
<cache-translates disk-store-path="logs/translatecaches/portal">
<!-- 员工信息翻译 -->
<sql-translate cache="staffIdName" datasource="dataSource">
<sql><![CDATA[
select c.staff_id,c.staff_name from sys_staff_info c
]]></sql>
<!-- 机构信息翻译 -->
<sql-translate cache="organIdName" datasource="dataSource">
<sql><![CDATA[
select c.organ_id,c.organ_name from sys_organ_info c
]]></sql>
<!-- 数据字典 -->
<sql-translate cache="dictKeyName" datasource="dataSource">
<sql><![CDATA[
select t.DICT_KEY,t.DICT_NAME,t.SEGMENT,t.SEGMENT1
from sag_dict_detail t
where t.DICT_TYPE_CODE=?
order by t.SHOW_INDEX
]]></sql>
</sql-translate>
</cache-translates>
<!-- 缓存刷新检测,根据最后修改时间进行检测 -->
<cache-update-checkers>
<!-- 基于sql的缓存更新检测,分时段不同频率进行检查 -->
<sql-checker check-frequency="0..8:30?180,8:30..20?30,20..24?120" datasource="dataSource">
<sql><![CDATA[
--#not_debug#--
-- 机构缓存更新检测
select distinct 'organIdName' cacheName,null cache_type
from sys_organ_info t
where t.UPDATE_TIME >=:lastUpdateTime
-- 员工工号姓名缓存检测
union all
select distinct 'staffIdName' cacheName,null cache_type
from sys_staff_info t1
where t1.UPDATE_TIME >=:lastUpdateTime
-- 数据字典key和name缓存检测
union all
select distinct 'dictKeyName' cacheName,t2.DICT_TYPE_CODE cache_type
from sag_dict_detail t2
where t2.UPDATE_TIME >=:lastUpdateTime
]]></sql>
</sql-checker>
</cache-update-checkers>
</sagacity>