# EhcacheUtil
码云地址:https://gitee.com/weijiang_admin/EhcacheUtil
#### 项目介绍
基于org.hibernate-ehcache的缓存查询工具,使用类SQL语言的语句进行查询缓存。
MYSQL数据库:`select * from document where title like '%土地征收%' order by pubdoctime desc`
示例对比:`select * from com.ywj.cache.document where title like '*土地征收*' order by pubdoctime desc`
两者得到的结果是一模一样的数据。
#### 使用说明
1. 这边我准备了初始化一些缓存数据从Excel文件读取缓存。
2. 你们也可以自行把数据存储在数据库表里面,通过数据库的SQL语句进行查询比对数据,效果更直观。
3. 项目目前实现单一的缓存对象(即一张表)查询,实现几个较常用的“>”、“>=”、“<”、“<=”、“between and”、“in”、“like”、“and”、“or”的逻辑运算,以及“order”排序。
#### 代码示例
语法和SQL的语法基本类似:select [cacheKey] from [cacheName] [condition] [order]
示例一(简单条件):
```
String cql = "select * from com.ywj.cache.document where title like '*土地征收*' ";
List<AttributeType> types = new ArrayList<AttributeType>();
types.add(AttributeType.STRING);
List<Map<String, Object>> datas = EhcacheManager.search(cql, types);
```
其中cql语句的检索条件【condition】要求:
1、检索条件的字段名称必须与缓存的键值一致【cacheKey】。
2、检索条件的“值”必须加单引号''。
types的含义是告知检索条件的字段的基本类型是什么类型,多个条件按从左到又顺序添加,见示例二,属性类型具体详见net.sf.ehcache.search.attribute.AttributeType类。
有:BOOLEAN、BYTE、CHAR、DOUBLE、FLOAT、INT、LONG、SHORT、DATE、SQL_DATE、ENUM、STRING
示例二(复杂条件):
```
String cql = "select * from com.ywj.cache.document where depname = '福州市仓山区' "
+ "and pubdoctime between '2018-04-01' and '2018-04-30' "
+ "and istimeout in('14','15','16') "
+ "and istimeout < '20' "
+ "order by pubdoctime desc,istimeout asc";
List<AttributeType> types = new ArrayList<AttributeType>();
types.add(AttributeType.STRING);//第一个检索条件depname是属于String类型
types.add(AttributeType.DATE);//第二个检索条件pubdoctime是属于java.util.Date类型
types.add(AttributeType.INT);//第三个检索条件istimeout是Int类型。
types.add(AttributeType.INT);//第四个检索条件istimeout是Int类型。
List<Map<String, Object>> datas = EhcacheManager.search(cql, types);
```