# EhcacheUtil **Repository Path**: weijiang_admin/EhcacheUtil ## Basic Information - **Project Name**: EhcacheUtil - **Description**: 基于org.hibernate-ehcache的缓存查询工具,使用类SQL语言的语句进行查询缓存。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2018-06-29 - **Last Updated**: 2022-09-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 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 types = new ArrayList(); types.add(AttributeType.STRING); List> 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 types = new ArrayList(); 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> datas = EhcacheManager.search(cql, types); 示例三(占位符方式) String cql = "select * from com.ywj.cache.document where depname != ? " + "and title like ? " + "and pubdoctime between ? and ? " + "and istimeout between ? and ? " + "and istimeout = ? " + "order by pubdoctime desc,istimeout asc"; Object[][] paras = { {AttributeType.STRING,"福州市仓山区"}, {AttributeType.STRING,"*琅岐海峡*"}, {AttributeType.DATE,"2018-04-01,2018-04-30"}, {AttributeType.INT,"10,20"}, {AttributeType.INT,15} }; List> datas = EhcacheManager.search(cql, paras); ```