maven + springmvc + mybatis + ehcache 搭建

来源:互联网 发布:simpleadapter源码 编辑:程序博客网 时间:2024/06/07 11:49

搭建的过程主要是配置文件的编写,下面是所有需要的配置文件。

maven配置文件pom.xml

<pre class="java" name="code"><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.cinyi</groupId><artifactId>spring_mybatis</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>Spring_mybatis Maven Webapp</name><url>http://maven.apache.org</url><properties><junit-version>4.11</junit-version><log4j-version>1.2.14</log4j-version><mysql-version>5.1.32</mysql-version><servlet-version>2.5</servlet-version><spring-version>4.0.6.RELEASE</spring-version><spring-mock-version>2.0.8</spring-mock-version><freemarker-version>2.3.20</freemarker-version><commons-logging-version>1.2</commons-logging-version><commons-lang-version>2.6</commons-lang-version><mybatis-version>3.2.7</mybatis-version><mybatis-spring-version>1.2.2</mybatis-spring-version><commons-collections4-version>4.0</commons-collections4-version><commons-dbcp-version>1.4</commons-dbcp-version><commons-pool-version>1.6</commons-pool-version><ehcache-version>2.6.11</ehcache-version><ehcache-spring-version>1.1.2</ehcache-spring-version><mybatis-ehcache-version>1.0.0</mybatis-ehcache-version><guava-version>19.0-rc2</guava-version><druid-version>1.0.13</druid-version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit-version}</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql-version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j-version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</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</groupId><artifactId>spring-core</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-jdbc</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring-version}</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-mock</artifactId><version>${spring-mock-version}</version><scope>test</scope></dependency><dependency><groupId>com.sun.jersey.contribs</groupId><artifactId>jersey-spring</artifactId><version>1.18.1</version><exclusions>           <exclusion>               <groupId>org.springframework</groupId>               <artifactId>*</artifactId>           </exclusion></exclusions></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>${freemarker-version}</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>${commons-logging-version}</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>${commons-lang-version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>${commons-collections4-version}</version></dependency><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>${commons-dbcp-version}</version></dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><version>${commons-pool-version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis-version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring-version}</version></dependency><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>${ehcache-version}</version></dependency><dependency><groupId>com.googlecode.ehcache-spring-annotations</groupId><artifactId>ehcache-spring-annotations</artifactId><version>${ehcache-spring-version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-ehcache</artifactId><version>${mybatis-ehcache-version}</version></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>${guava-version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid-version}</version></dependency></dependencies><build><finalName>spring_mybatis</finalName></build></project>


spring配置文件一共有3个,spring-mvc.xml、spring-mybatis.xml、spring-ehcache.xml

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"      xmlns:p="http://www.springframework.org/schema/p"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        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.xsd      http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">      <!-- 添加注解驱动,启用springmvc注解 -->   <mvc:annotation-driven />    <!-- 默认扫描包 -->   <context:component-scan base-package="com.cinyi.*.controller" />   <context:component-scan base-package="com.cinyi.*.service" />         <!-- 定义跳转文件的前后缀 -->   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="prefix" value="/WEB-INF/jsp/" />         <property name="suffix" value=".jsp" />     </bean>        <!-- 完成请求和注解POJO映射 -->   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>      <!--配置拦截器, 多个拦截器,顺序执行 -->  <mvc:interceptors>        <mvc:interceptor>            <!-- 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->          <mvc:mapping path="/user/**" />          <bean class="com.cinyi.common.interceptor.CommonInterceptor"></bean>        </mvc:interceptor>      <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->  </mvc:interceptors> </beans>


spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns="http://www.springframework.org/schema/beans"       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"><!-- 扫描类,表示会被当做{mybatis mapper}处理,配置了之后表示可以自动引入mapper类 --><mybatis:scan base-package="com.cinyi.*.dao"/><!-- 引入属性文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置数据源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- 初始化连接大小 --><property name="initialSize" value="0" /><!-- 连接池最大使用连接数量 --><property name="maxActive" value="20" /><!-- 连接池最小空闲 --><property name="minIdle" value="0" /><!-- 获取连接最大等待时间 --><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="25200000" /></bean><!-- mybatis配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="dataSource" ref="dataSource" /></bean></beans>


spring-ehcache.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:mvc="http://www.springframework.org/schema/mvc"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd"><!-- 开启spring缓存 --><cache:annotation-driven cache-manager="cacheManager" /><!-- Spring提供的基于的Ehcache实现的缓存管理器 --><bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:/ehcache.xml"/><!-- 由于mybatis也使用了Ehcache, 保证双方都使用同一个缓存管理器   import--><property name="shared" value="true"/></bean><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="ehcacheManager"/></bean></beans>


数据源配置文件 jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/myuser?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=123456

 

mybatis的配置文件一共有2个,mybatis-config.xml、user-mapper.xml

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases>       <typeAlias type="com.user.bean.User" alias="User" />    </typeAliases> <mappers>           <mapper resource="com/user/bean/user-mapper.xml" />   <!-- mapper对应的xml配置文件 -->    </mappers></configuration>


user-mapper.xml 在src目录下,与javabean在同一个目录

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.user.dao.UserDao"><cache type="org.mybatis.caches.ehcache.EhcacheCache"/><select id="selectUser" parameterType="string" resultType="User">SELECT * from myuser where  id = #{id}</select></mapper>

ehcache的配置文件ehcache.xml

ehcache.xml 在src目录下

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"monitoring="autodetect" dynamicConfig="true"><diskStore path="java.io.tmpdir" /><!-- name:Cache的唯一标识 maxElementsInMemory:内存中最大缓存对象数 maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大 eternal:Element是否永久有效,一但设置了,timeout将不起作用 overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中 timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大 timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大 diskPersistent:是否缓存虚拟机重启期数据 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用) --><defaultCache maxElementsInMemory="10000" eternal="false"timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"maxElementsOnDisk="10000000" diskPersistent="false"diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /><cache name="userCache" maxElementsInMemory="10000"maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"memoryStoreEvictionPolicy="LFU" /></ehcache>


至此,所有的配置文件已完成。

 

 

0 0