• spring+springMVC+mybatis整合并使用redis做缓存


    1.用idea 构建一个maven 的web项目

    点击下一步

    继续下一步

    选着maven home directory 自己的maven 安装目录,全选下一步,等一分钟就OK了

    2.配置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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-redis</groupId>
    <artifactId>redis</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>redis Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
    <spring.version>4.2.5.RELEASE</spring.version>
    <mybatis.version>3.4.4</mybatis.version>
    <log4j.version>1.2.17</log4j.version>
    </properties>
    <dependencies>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</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-jdbc</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>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.10.5.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${spring.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc-portlet</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument-tomcat</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>

    <!--打印日志-->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
    </dependency>
    <dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>

    <!--redis需用的包-->
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.8.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    </dependency>

    <!--mybatis包-->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
    </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
    </dependency>

    <!--c3p0包-->
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
    </dependency>


    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.4</version>

    </dependency>

    </dependencies>




    <build>
    <!--将mybatis的数据库查询的xml打进包里-->
    <resources>
    <resource>
    <directory>src/main/java</directory>
    <includes>
    <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
    </resource>
    </resources>
    <finalName>spring-redis</finalName>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <encoding>utf-8</encoding>
    </configuration>
    </plugin>

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    </plugin>

    </plugins>
    </build>

    </project>

    2.配置jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/cas
    jdbc.username=root
    jdbc.password=root

    minPoolSize.count=1
    maxPoolSize.count=5
    initialPoolSize.count=3
    maxIdleTime=0
    acquireIncrement.count=2
    idleConnectionTestPeriod=120
    maxStatements=100
    acquireRetryAttempts=30
    breakAfterAcquireFailure=false
    testConnectionOnCheckout=false

    3.配置spring-core.xml

    <?xml version="1.0" encoding="UTF-8"?><!--suppress ALL -->
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    ">

    <description>Spring核心配置</description>

    <!-- <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:config/local.properties" />
    </bean>-->
    <!-- <context:property-placeholder location="classpath:config/redis.properties"/>-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <!-- 这里支持多种寻址方式:classpath和file -->
    <value>classpath:config/local.properties</value>
    <value>classpath:config/redis.properties</value>
    <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
    </list>
    </property>
    </bean>

    <!-- 扫描除了Controller之外的注解-->
    <context:component-scan base-package="com.spring.*">
    <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
    <context:exclude-filter type="annotation"
    expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--数据源的配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <!--连接池中保留的最小连接数。 -->
    <property name="minPoolSize" value="${minPoolSize.count}"/>
    <!--连接池中保留的最大连接数。Default: 15 -->
    <property name="maxPoolSize" value="${maxPoolSize.count}"/>
    <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
    <property name="initialPoolSize" value="${initialPoolSize.count}"/>
    <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
    <property name="maxIdleTime" value="${maxIdleTime}"/>
    <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
    <property name="acquireIncrement" value="${acquireIncrement.count}"/>
    <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
    如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
    <property name="maxStatements" value="${maxStatements}"/>
    <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
    <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
    <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
    <property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
    <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍 有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
    获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
    <property name="breakAfterAcquireFailure" value="${breakAfterAcquireFailure}"/>
    <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其 有效性。建议使用idleConnectionTestPeriod或automaticTestTable
    等方法来提升连接测试的性能。Default: false -->
    <property name="testConnectionOnCheckout" value="${testConnectionOnCheckout}"/>

    </bean>

    <!-- mybatis 配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath*:/com/spring/map/redis-mapper.xml"/>
    </bean>

    <!-- mybatis 扫描 mapper 对应类的配置 多个包的扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.spring.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- 事务配置 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>


    <!-- 配置JedisPoolConfig相关参数 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>
    <property name="minIdle" value="${spring.redis.pool.minIdle}"></property>
    </bean>

    <!-- 配置redis服务器信息 -->
    <bean id="connectionFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="poolConfig" ref="poolConfig"></property>
    <property name="hostName" value="${spring.redis.host}"></property>
    <property name="port" value="${spring.redis.port}"></property>
    <property name="database" value="${spring.redis.database}"></property>
    <property name="timeout" value="${spring.redis.timeout}"></property>
    </bean>

    <!-- 配置RedisTemplate -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="connectionFactory"></property>
    <property name="keySerializer">
    <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
    </property>
    <property name="valueSerializer">
    <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
    </property>
    <property name="hashKeySerializer">
    <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
    </property>
    <!-- 使用JacksonJsonRedisSerializer需要引入jar包:barchart-wrap-jackson-1.8.6-build001.jar -->
    <!-- JacksonJsonRedisSerializer 需要一个有参的构造函数,因此需要配置constructor-arg -->
    <property name="hashValueSerializer">
    <bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">
    <constructor-arg type="java.lang.Class" value="java.lang.Object"></constructor-arg>
    </bean>
    </property>
    </bean>

    <bean class="redis.clients.jedis.JedisPool">
    <constructor-arg index="0" ref="poolConfig"/>
    <constructor-arg index="1" type="java.lang.String" value="${spring.redis.host}"/>
    <constructor-arg index="2" type="int" value="${spring.redis.port}"/>
    </bean>

    <!-- Redis缓存管理对象 -->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
    <constructor-arg index="0" ref="redisTemplate" />
    <property name="defaultExpiration" value="300"/>
    </bean>

    <!-- 开启Spring缓存 -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    </beans>

    4.配置spring-mvc.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <description>Spring MVC 配置</description>
    <!--开启注解-->
    <mvc:annotation-driven/>

    <!-- 自动扫描且只扫描@Controller -->
    <context:component-scan
    base-package="com.spring.controller"
    use-default-filters="false">
    <context:include-filter type="annotation"
    expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 使用默认的servlet handler 对静态资源不进行访问 -->
    <mvc:default-servlet-handler/>

    <!--配置资源的位置-->
    <mvc:resources mapping="/images/**" location="/images/" />

    <!-- 视图解析器 -->
    <bean name="viewResolvers"

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    </beans>

    5.配置web.xml

    启动项目,就ok了,

    redis的配置在spring-core.xml 中,
    在你java 代码里写上缓存的注解

    完成

    源码:https://github.com/chenshijun007/spring-springMVC-mybatis-redis.git






  • 相关阅读:
    Java的jmap命令使用详解
    Linux常用文件管理命令详解
    Java的jps命令使用详解
    快速整明白Redis中的整数集合到底是个啥
    Java的jinfo命令使用详解
    Java的jstack命令使用详解
    快速整明白Redis中的字典到底是个啥
    Java的jstat命令使用详解
    Linux常用系统管理命令详解
    详解ElasticAPM实现微服务的链路追踪(NET)
  • 原文地址:https://www.cnblogs.com/chenshijun007/p/7383797.html
Copyright © 2020-2023  润新知