加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring基于注解整合Redis完整实例

发布时间:2020-05-23 20:41:57 所属栏目:Java 来源:互联网
导读:在《Redis之――Spring整合Redis》一文中,向大家介绍了如何将spring与Redis整合起来,但不是基于注解的形式,很多同学都希望能够通过注解的形式来简单的将Spring与Redis整合起来,这样,在使用的时候,只需要在相应

在《Redis之――Spring整合Redis》一文中,向大家介绍了如何将spring与Redis整合起来,但不是基于注解的形式,很多同学都希望能够通过注解的形式来简单的将Spring与Redis整合起来,这样,在使用的时候,只需要在相应的方法上加上注解,便可以使方法轻松的调用Redis的缓存。那么今天就来向大家介绍如何用基于注解的形式来整合Spring与Redis。

一、项目搭建

今天,我们不使用hibernate来操作数据库了,我们今天选择的框架是: Spring4(包括mvc、context、orm) + MyBatis3,所以,我们今天的基础架构是:Spring4(包括mvc、context、orm) + MyBatis3+Redis

1、构建pom.xml

<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>lyz</groupId> 
  <artifactId>lyz</artifactId> 
  <version>1.0.0</version> 
  <packaging>war</packaging> 
  <name>lyz</name> 
  <description>Basic Data Platform</description> 
 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <spring.version>4.2.0.RELEASE</spring.version> 
  </properties> 
 
  <dependencies> 
 
    <!-- spring mvc related.....start --> <!-- TODO: replace jackson with fastjson --> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
    </dependency> 
    <dependency> 
      <groupId>commons-logging</groupId> 
      <artifactId>commons-logging</artifactId> 
      <version>1.1.3</version> 
    </dependency> 
    <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.13</version> 
    </dependency> 
    <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-annotations</artifactId> 
      <version>2.6.1</version> 
    </dependency> 
    <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.6.1</version> 
    </dependency> 
    <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.6.1</version> 
    </dependency> 
    <!-- spring mvc related.....end --> 
 
    <!-- mybatis orm related.....start --> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.mybatis</groupId> 
      <artifactId>mybatis-spring</artifactId> 
      <version>1.2.3</version> 
    </dependency> 
    <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.36</version> 
    </dependency> 
    <dependency> 
      <groupId>org.mybatis</groupId> 
      <artifactId>mybatis</artifactId> 
      <version>3.3.0</version> 
    </dependency> 
    <dependency> 
      <groupId>c3p0</groupId> 
      <artifactId>c3p0</artifactId> 
      <version>0.9.1.2</version> 
    </dependency> 
    <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.7.12</version> 
    </dependency> 
    <!-- mybatis orm related.....end --> 
 
    <!-- project log related.....start --> 
    <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.17</version> 
    </dependency> 
    <!-- project log related.....end --> 
 
    <!-- redis cache related.....start --> 
    <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-redis</artifactId> 
      <version>1.6.0.RELEASE</version> 
    </dependency> 
    <dependency> 
      <groupId>redis.clients</groupId> 
      <artifactId>jedis</artifactId> 
      <version>2.7.3</version> 
    </dependency> 
    <!-- redis cache related.....end --> 
  </dependencies> 
 
  <build> 
    <plugins> 
      <plugin> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.1</version> 
        <configuration> 
          <source>1.7</source> 
          <target>1.7</target> 
        </configuration> 
      </plugin> 
      <plugin> 
        <artifactId>maven-war-plugin</artifactId> 
        <version>2.4</version> 
        <configuration> 
          <warSourceDirectory>WebContent</warSourceDirectory> 
          <failOnMissingWebXml>false</failOnMissingWebXml> 
        </configuration> 
      </plugin> 
    </plugins> 
  </build> 
</project> 

2、创建测试数据库:lyz,测试数据表:user

DROP DATABASE IF EXISTS `lyz`; 
CREATE DATABASE `lyz`; 
DROP TABLE IF EXISTS `user`; 
CREATE TABLE `user` ( 
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`sex` varchar(255) DEFAULT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

3、创建实体类user

package com.lyz.user.bean; 
 
import java.io.Serializable; 
 
import org.codehaus.jackson.map.annotate.JsonSerialize; 
 
import com.fasterxml.jackson.databind.PropertyNamingStrategy; 
import com.fasterxml.jackson.databind.annotation.JsonNaming; 
 
/** 
 * 用户实体类 
 * @author liuyazhuang 
 * 
 */ 
@JsonSerialize 
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)  
public class User implements Serializable{ 
  private static final long serialVersionUID = 1332643889208978231L; 
   
  /** 
   * id 
   */ 
  private Integer id; 
   
  /** 
   * 姓名 
   */ 
  private String name; 
   
  /** 
   * 性别 
   */ 
  private String sex; 
   
  /** 
   * 年龄 
   */ 
  private Integer age; 
 
  public User() { 
    super(); 
  } 
   
  public User(Integer id,String name) { 
    super(); 
    this.id = id; 
    this.name = name; 
  } 
 
  public User(String name,String sex,Integer age) { 
    super(); 
    this.name = name; 
    this.sex = sex; 
    this.age = age; 
  } 
 
  public Integer getId() { 
    return id; 
  } 
 
  public void setId(Integer id) { 
    this.id = id; 
  } 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public String getSex() { 
    return sex; 
  } 
 
  public void setSex(String sex) { 
    this.sex = sex; 
  } 
 
  public Integer getAge() { 
    return age; 
  } 
 
  public void setAge(Integer age) { 
    this.age = age; 
  } 
 
  @Override 
  public String toString() { 
    return "User [id=" + id + ",name=" + name + ",sex=" + sex + ",age=" + age + "]"; 
  } 
   
} 

4、创建UserMapper接口

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读