<h1>使用Leopard Redis操作Redis</h1><h2>学习如何在旧项目中使用Leopard Redis。</h2><p>本指南将引导您完成使用Leopard Redis操作Redis。</p><h1>How to complete this guide</h1><p>你可以从头开始并完成每一个步骤,或者您可以绕过你已经熟悉的基本设置步骤。无论哪种方式,你最终都可以得到可工作的代码。</p><h1>1、配置maven依赖</h1><p>在dao模块的pom.xml加入</p><p><pre name="code" class="java"><dependencies> […] <dependency> <groupId>io.leopard.data4j</groupId> <artifactId>data4j-redis</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> […]</dependencies><repositories> <repository> <id>leopard-snapshots</id> <name>Leopard Snapshots</name> <url></url> </repository></repositories></pre>如果您是非maven用户,可以通过以下链接下载jar包.<br /><a href="" target="_blank">io.leopard.data4j:data4j-redis:0.0.1-SNAPSHOT</a><br /></p><h1>2、配置spring</h1><p><code>src/main/resources/applicationContext-dao.xml</code><pre name="code" class="java"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="" xmlns:xsi="" xsi:schemaLocation=" http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userDao" class="io.leopard.guides.dao.UserDao" /><bean id="redis" class="io.leopard.data4j.redis.RedisImpl"><property name="server" value="112.126.75.27:6311" /><property name="maxActive" value="128" /></bean></beans></pre></p><h1>3、使用Redis接口</h1><p>创建<code>src/main/java/io/leopard/guides/dao/UserDao.java</code><pre name="code" class="java">package io.leopard.guides.dao;import io.leopard.burrow.lang.Json;import io.leopard.data4j.redis.Redis;import io.leopard.guides.model.User;import javax.annotation.Resource;public class UserDao {@Resourceprivate Redis redis;protected String getKey(long uid) {return "user:" + uid;}/*** 添加用户.** @param user* @return 添加成功返回true,出错抛异常*/public boolean add(User user) {String key = this.getKey(user.getUid());String json = Json.toJson(user);this.redis.set(key, json);return true;}/*** 根据uid获取用户信息.** @param uid* @return 用户存在则返回用户对象,不存在则返回null.*/public User get(long uid) {String key = this.getKey(uid);String json = this.redis.get(key);return Json.toObject(json, User.class);}/*** 删除用户** @param uid* @return 成功删除记录就返回true,记录不存在则返回false,出错则抛异常.*/public boolean delete(long uid) {String key = this.getKey(uid);Long result = this.redis.del(key);return (result != null && result == 1);}}</pre></p><h1>Json解析模块引入</h1><p>例子代码中使用到Json类,如果你希望在项目中使用,需要配置maven依赖</p><p><pre name="code" class="java"><dependencies> […] <dependency> <groupId>io.leopard.burrow</groupId> <artifactId>burrow-lang</artifactId> <version>0.0.5-SNAPSHOT</version> </dependency> […]</dependencies></pre>如果您是非maven用户,可以通过以下链接下载jar包.<br /><a href="" target="_blank">io.leopard.burrow:burrow-lang:0.0.5-SNAPSHOT</a><br /></p><p>了解Leopard更多功能模块,请访问<a href=""></a></p><h1>总结</h1><p>恭喜你!您已经可以在旧项目配置使用Leopard Redis,虽然功能比较简单,你可以在这个基础上扩展出你的业务系统,祝您好运。</p>
,世界上那些最容易的事情中,拖延时间最不费力。