cached-query 将缓存和查询数据库快速连接起来的轻类库
时间:2022-03-14 00:00
介绍
我们常常有这样的需求:当我们把memcached加入到项目后我还还要写一个 cacheUtils 或者 cacheManager 之类的类来操作memcached。并且一般的操作不外乎是这种操作:
- <dependency>
<groupId>org.crazycake</groupId>
<artifactId>cached-query</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
- 要查询并被实例化的类一定要实现Serializable接口
- 实例化的类里面属性的名字要取跟数据库里面字段对应的驼峰式命名,比如数据库中的字段是 student_name 对应的属性就是 studentName
- 类的属性不能用基本类型,int要用Integer,float用Float,其他依次类推,其实这样开发者养成一个好习惯也是很好的
使用方法
建立一个配置文件 cached-query.properties 在classpath跟目录下,你可以放到 src/resources 下
#cache type. [memcached] cache.type=memcached cache.host=127.0.0.1 cache.port=11211 cache.expire=1800
建立你要查询的表对应的model
public class Student implements Serializable{ private Integer studentId; private String studentName; public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } }
在你的代码中使用它
CachedQuery q = CachedQuery.getInstance(); String sql="select student_id,student_name from student where student_id=?"; Object[] params = new Object[]{1}; ArrayList<Student> list = q.queryList(sql, params, conn, Student.class);
注意事项
最新版本情况参考 https://github.com/alexxiyang/cached-query