sample-spring-memecached+mybatis+spring mvc例子

项目中用到过的sample-spring-memecached,我这里整理了个小demo,以便以后继续学习,如下:

1.导包(maven管理)

<?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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.zqgame</groupId>

<artifactId>simp-spring-memacha</artifactId>

<name>simp-spring-memacha</name>

<packaging>war</packaging>

<version>1.0.0-SNAPSHOT</version>

<properties>

<java-version>1.6</java-version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<org.springframework.version>3.1.2.RELEASE</org.springframework.version>

<org.slf4j.version>1.6.1</org.slf4j.version>

<org.mybatis.version>3.1.1</org.mybatis.version>

<org.mybatis.spring.version>1.0.2</org.mybatis.spring.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${org.springframework.version}</version>

<exclusions>

<!-- Exclude Commons Logging in favor of SLF4j -->

<exclusion>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.ws</groupId>

<artifactId>spring-oxm</artifactId>

<version>1.5.10</version>

</dependency>

                <dependency>

<groupId>cglib</groupId>

<artifactId>cglib-nodep</artifactId>

<version>2.1_3</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${org.slf4j.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>jcl-over-slf4j</artifactId>

<version>${org.slf4j.version}</version>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${org.slf4j.version}</version>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.16</version>

<scope>runtime</scope>

</dependency>

 

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.7</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${org.springframework.version}</version>

<scope>test</scope>

</dependency>

       <dependency>

<groupId>c3p0</groupId>

<artifactId>c3p0</artifactId>

<version>0.9.1.2</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.1.1</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.21</version>

</dependency>

 

<dependency>

<groupId>org.noo</groupId>

<artifactId>mybatis-paging</artifactId>

<version>0.1</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>3.0</version>

</dependency>

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.4</version>

</dependency>

<dependency>

<groupId>net.sf.json-lib</groupId>

<artifactId>json-lib</artifactId>

<version>2.3</version>

<classifier>jdk15</classifier>

</dependency>

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.4</version>

</dependency>

<!--sample spring memecached begin-->

<dependency>

  <groupId>com.google.code.simple-spring-memcached</groupId>

  <artifactId>spymemcached-provider</artifactId>

  <version>3.1.0</version>

</dependency>

<dependency>

  <groupId>com.google.code.simple-spring-memcached</groupId>

  <artifactId>xmemcached-provider</artifactId>

  <version>3.1.0</version>

</dependency> 

<dependency>

  <groupId>com.google.code.simple-spring-memcached</groupId>

  <artifactId>spring-cache</artifactId>

  <version>3.1.0</version>

</dependency>

<dependency>

  <groupId>com.google.code.simple-spring-memcached</groupId>

  <artifactId>spymemcached-provider</artifactId>

  <version>3.1.0</version>

</dependency>

 <!--sample spring memecached end-->       

</dependencies>

<repositories>

<repository>

<id>jboss-public-repository-group</id>

<url>https://repository.jboss.org/nexus/content/groups/public-jboss</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

</repositories>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.5</source>

<target>1.5</target>

<showWarnings>true</showWarnings>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<executions>

<execution>

<id>install</id>

<phase>install</phase>

<goals>

<goal>sources</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>

</project>

(配置文件)

mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration

    PUBLIC "-//www.mybatis.org//DTD Config 3.0//EN"

    "http://www.mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<settings>

<setting name="cacheEnabled" value="false" />

<setting name="useGeneratedKeys" value="true" />

<setting name="defaultExecutorType" value="REUSE" />

</settings>

</configuration>

spring-root.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:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<import resource="spring/spring-db-context.xml" />

<import resource="spring/spring-memcached.xml" />

</beans>

spring-db-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!--配置和创建jdbc数据源 -->

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${driverClass}" />

<property name="jdbcUrl" value="${url}" />

<property name="user" value="${username}" />

<property name="password" value="${password}" />

</bean>

<bean id="txManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- myibatis配置,使其支持注解和配置文件形式 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="configLocation" value="classpath:mybatis.xml" />

<property name="typeAliasesPackage" value="com.zqgame.models" />

<property name="mapperLocations" value="classpath:mybatis-mapping/*mapper.xml" />

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.zqgame.mappers" />

</bean>

<!-- 使用spring全注解扫描services层 -->

<context:component-scan base-package="com.zqgame.services" />

</beans>

spring-memecached.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:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
     <bean name="defaultSerializationTypeAsString" class="java.lang.String">
<constructor-arg value="#{systemProperties['ssm.defaultSerializationType']?:'JSON'}" />
</bean>
  <import resource="simplesm-context.xml" />
  <aop:aspectj-autoproxy />
  <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
    <property name="cacheClientFactory">
      <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
    </property>
    <property name="addressProvider">
      <bean class="com.google.code.ssm.config.DefaultAddressProvider">
        <property name="address" value="127.0.0.1:11211" />
      </bean>
    </property>
    <property name="configuration">
      <bean class="com.google.code.ssm.providers.CacheConfiguration">
        <property name="consistentHashing" value="true" />
      </bean>
    </property>
    <property name="defaultSerializationType"
value="#{T(com.google.code.ssm.api.format.SerializationType).valueOf(@defaultSerializationTypeAsString)}" />
  </bean>
  <bean class="com.google.code.ssm.Settings">
  <property name="order" value="500" />
  </bean>
</beans>

那个simplesm-context.xml是从sample-spring-memecache的bao里解压出来的(不再多说了)

model,mybatis的model-mapper配置文件啊,dao啊,都不说了,按常规来就行了,下面说一下service下的注解缓存

package com.zqgame.services;

 

import java.util.List;

import java.util.Map;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import com.google.code.ssm.api.InvalidateSingleCache;

import com.google.code.ssm.api.ParameterDataUpdateContent;

import com.google.code.ssm.api.ParameterValueKeyProvider;

import com.google.code.ssm.api.ReadThroughSingleCache;

import com.google.code.ssm.api.UpdateSingleCache;

import com.zqgame.mappers.ArticleDao;

import com.zqgame.mappers.ArticleTypeDao;

import com.zqgame.models.Article;

import com.zqgame.models.ArticleType;

 

/**

 * 新闻服务层

 * 

 * @author panguixiang

 * 

 */

@Service("articleService")

public class ArticleService {

 

@Autowired

private ArticleDao articleDao;

/**

* 添加新闻

* @param article

*/

@UpdateSingleCache(namespace = "article")

public Article saveArticle(

@ParameterDataUpdateContent @ParameterValueKeyProvider Article article) {

articleDao.save(article);

return article;

}

 

/**

* 修改新闻

* @param article

*/

@UpdateSingleCache(namespace = "article")

public void updateArticle(

@ParameterDataUpdateContent @ParameterValueKeyProvider Article article) {

articleDao.update(article);

}

 

/**

* 主键查询article对象

* @param id

* @return

*/

@ReadThroughSingleCache(namespace = "article", expiration = 3000)

public Article getArticleById(@ParameterValueKeyProvider Long id) {

return articleDao.getArticleById(id);

}

 

/**

* 根据Id删除新闻对象

* @param id

*/

@InvalidateSingleCache(namespace = "article")

public void deleteArticleById(@ParameterValueKeyProvider(order = 0) Long id) {

articleDao.deleteArticleById(id);

}

/**

* 依据article与articleType关联字段artypeId查询新闻列表(传入artypeId)

* @param artypeId

* @return

*/

/* @ReadThroughSingleCache(namespace = "article", expiration = 300) */

public List<Article> articlePageByArtypeId(Long artypeId) {

List<Article> list = articleDao.articlePageByArtypeId(artypeId);

return list;

}

}

 

到这里就好了,然后开启memecache服务,用junit测试看看

telnet 127.0.0.1 11211

get article:173  获得namespace为article的key为173的缓存数据

 

 

 

你可能感兴趣的:(mvc,memecah)