崇文企业网站建设公司,江苏省建设考试网站准考证打印,17做网店这个网站做起多少钱,企业邮箱多少钱在这篇文章中#xff0c;我们将构建一个简单的用户界面。 数据将存储在Redis中。 为了与Redis交互#xff0c;我们将使用Jedis库。 CDI用于Depedency Injection#xff0c;而Servlet 3.0用于视图。 让我们从Redis / Jedis部分开始。 您可以在这些 帖子中找到有关Redis和Jed… 在这篇文章中我们将构建一个简单的用户界面。 数据将存储在Redis中。 为了与Redis交互我们将使用Jedis库。 CDI用于Depedency Injection而Servlet 3.0用于视图。 让我们从Redis / Jedis部分开始。 您可以在这些 帖子中找到有关Redis和Jedis的概述。 让我们从User类开始我们可以在下面看到 public class User {private String firstName;private String lastName;private String email;private String gender;private long id;
} 现在让我们定义用于在Redis上存储用户信息的键。 在我们的示例中我们将使用三个键 userids –将通过使用INCR命令来生成用户ID。 userall –用于存储所有用户ID的Redis列表 useriddata –系统中的每个用户都有一个带有此模式的密钥。 这些密钥将是哈希。 当我们要向系统添加新用户时我们将处理三个键如下面的步骤所示 首先我们通过增加userids键获得一个新的用户ID INCR userids 然后我们将其添加到userall列表 lpush userall returnId 并将用户信息添加到其自己的哈希中 HMSET userreturnedIddata字段值.. 我们可以在方法UserDAO.addUser中看到以下代码 public User addUser(User user){long userId jedis.incr(Keys.USER_IDS.key());user.setId(userId);//Getting the PipelinePipeline pipeline jedis.pipelined();//add to users listpipeline.lpush(Keys.USER_ALL.key(), String.valueOf(userId));//add to the hashpipeline.hmset(Keys.USER_DATA.formated(String.valueOf(userId)), BeanUtil.toMap(user));pipeline.sync();return user;} 解释上面的代码首先我们得到新的用户ID。 jedis变量是UserDAO类的属性它是Jedis类的实例。 为了避免对Redis服务器的三个网络调用我们使用管道的概念因此在对Redis服务器的一次调用中我们将在userall列表中添加用户ID并在useriddata中添加用户信息。哈希。 管道实例执行的命令将在调用pipeline.sync之后在redis服务器中执行。 我们创建了一个util类用于将Map StringString中的用户对象转换为存储在redis哈希中的对象。 为了查看用户的详细信息我们在DAO中有一个方法来获取用户我们可以在下面看到 public User getUser(long userId){String userInfoKey Keys.USER_DATA.formated(String.valueOf(userId));MapString, String properties jedis.hgetAll(userInfoKey);return BeanUtil.populate(properties, new User());} 如我们所见这是一个简单的方法基本上我们调用命令HGETALL来从哈希中检索所有字段。 Jedis api将其作为Map返回因此我们可以简单地从Map中填充用户属性。 要删除用户我们创建了以下方法 public boolean remove(long userId){String userInfoKey Keys.USER_DATA.formated(String.valueOf(userId));Pipeline pipeline jedis.pipelined();ResponseLong responseDel pipeline.del(userInfoKey);ResponseLong responseLrem pipeline.lrem(Keys.USER_ALL.key(), 0, String.valueOf(userId));pipeline.sync();return responseDel.get() 0 responseLrem.get() 0;} 一旦需要从userall列表中删除Hash键和用户ID下面的方法也将使用流水线的概念。 LREM命令从列表中删除该值零表示删除该值在列表中的所有出现。 在此方法中我们还通过使用每个命令返回的Response对象来使用命令返回的值。 我们只有在调用sync方法之后才能使用那些对象。 更新方法非常简单我们可以在下面看到它 public User update(User user){String userInfoKey Keys.USER_DATA.formated(String.valueOf(user.getId()));jedis.hmset(userInfoKey ,BeanUtil.toMap(user));return user;} 这只是HMSET的调用它传递具有所有用户属性的Map该映射将在Redis哈希中进行更新。 要列出用户我们还需要使用管道。 Redis不提供HMGETALL命令因此要检索具有一个网络连接的所有用户我们将通过管道进行操作。 列表方法如下所示 public ListUser list(){ListUser users new ArrayListUser();//Get all user ids from the redis list using LRANGEListString allUserIds jedis.lrange(Keys.USER_ALL.key(), 0, -1);if(allUserIds ! null !allUserIds.isEmpty()){ListResponseMapString,String responseList new ArrayListResponseMapString,String();Pipeline pipeline jedis.pipelined();for(String userId : allUserIds){//call HGETALL for each user idresponseList.add(pipeline.hgetAll(Keys.USER_DATA.formated(userId)));}pipeline.sync();//iterate over the pipelined resultsfor(ResponseMapString, String properties : responseList){users.add(BeanUtil.populate(properties.get(), new User()));}}return users;} 在这种方法中我们首先使用命令LRANGE从列表userall获得所有用户ID。 之后我们通过管道执行“ HMGETALL”我们为每个用户调用HGETALL命令然后从返回的Map实例构建用户对象。 在第一篇文章中我们了解了如何使用Jedis api与Redis服务器进行交互以存储和检索用户的信息。 我们看到了Pipeline的概念和用法。 在下一篇文章中我们将展示如何使用CDI进行依赖注入和使用Servlet 3.0进行视图。 参考 使用Servlet 3.0Redis / Jedis和CDI的简单CRUD –第1部分来自XICO JUNIORS WEBLOG博客的JCG合作伙伴 Francisco Ribeiro Junior。 翻译自: https://www.javacodegeeks.com/2013/10/simple-crud-using-servlet-3-0-redisjedis-and-cdi-part-1.html