RT:(划水篇,就是一个 RedisPlugin 启动的例子,会用的直接略过)
按照文档来个例子项目:
https://jfinal.com/doc/8-4
编辑 pom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yunfinal.demo</groupId> <artifactId>jfinal-demo-redis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- JF 项目框架 --> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>5.0.0</version> </dependency> <!-- redis 客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <!-- 注意:更高版本不支持 jdk 8 --> <version>3.6.3</version> </dependency> </dependencies> </project>
创建一个运行例子(JdkSerializer序列化的,比较简单依赖少):
package jfinal.demo; import com.jfinal.plugin.redis.Redis; import com.jfinal.plugin.redis.RedisPlugin; import com.jfinal.plugin.redis.serializer.JdkSerializer; public class RedisTest { public static void main(String[] args) { RedisPlugin rp = new RedisPlugin("myRedis", "连接", "密码"); //使用JdkSerializer rp.setSerializer(JdkSerializer.me); // 与web下唯一区别是需要这里调用一次start()方法 rp.start(); try{ String set = Redis.use().set("key", "value"); System.out.println(set); Object key = Redis.use().get("key"); System.out.println(key); }finally { rp.stop(); } } }
运行结果:
再来一个 FstSerializer 序列化的例子:
再次编辑 pom.xml 文件,增加依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yunfinal.demo</groupId> <artifactId>jfinal-demo-redis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- JF 项目框架 --> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>5.0.0</version> </dependency> <!-- redis 客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <!-- 注意:更高版本不支持 jdk 8 --> <version>3.6.3</version> </dependency> <!-- FstSerializer 序列化工具 --> <dependency> <groupId>de.ruedigermoeller</groupId> <artifactId>fst</artifactId> <version>2.57</version> </dependency> </dependencies> </project>
java例子去掉JDK序列化:
package jfinal.demo; import com.jfinal.plugin.redis.Redis; import com.jfinal.plugin.redis.RedisPlugin; public class RedisTest { public static void main(String[] args) { RedisPlugin rp = new RedisPlugin("myRedis", "连接", "密码"); //使用JdkSerializer // rp.setSerializer(JdkSerializer.me); // 与web下唯一区别是需要这里调用一次start()方法 rp.start(); try{ String set = Redis.use().set("key", "value"); System.out.println(set); Object key = Redis.use().get("key"); System.out.println(key); }finally { rp.stop(); } } }
运行结果:
好了,分享完毕。