1:common下面的工做:java
a:在common下面的resources下面新建spring和mybatis文件夾,spring是給spring的配置文件使用,mybatis下面給mybatis配置文件使用,mybatis下面新建mapper文件夾。業務的mapper.xml都放在下面。redis
b:spring文件夾下面新建spring-db.xml文件,內容以下:spring
<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <aop:aspectj-autoproxy/> <bean id="multipleDataSourceAspect" class="me.explain.caption.dao.datasource.DataSourceAspect" /> <aop:config> <aop:aspect id="c" ref="multipleDataSourceAspect"> <aop:pointcut id="tx" expression="execution(* me.explain.caption.service.impl.*.*(..))"/> <aop:before pointcut-ref="tx" method="before"/> </aop:aspect> </aop:config> <bean id="masterdataSource" 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="maxActive" value="${jdbc.max_active}" /> <property name="initialSize" value="${jdbc.initial_size}" /> <property name="maxWait" value="${jdbc.max_wait}" /> <property name="minIdle" value="${jdbc.min_idle}" /> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="1800" /> <property name="logAbandoned" value="true" /> </bean> <bean id="dataSource" class="me.explain.caption.dao.datasource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="master" value-ref="masterdataSource"/> </map> </property> <property name="defaultTargetDataSource" ref="masterdataSource"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:/mybatis/mybatis-configuration.xml" /> <property name="mapperLocations" value="classpath*:/mybatis/mapper/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="me.explain.caption.dao.mapper" /> <property name="markerInterface" value="me.explain.caption.common.IBaseMapper" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
spring文件夾下面新建spring-redis.xml文件,內容以下:sql
<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- <property name="maxTotal" value="${redis.max_total}" /> --> <property name="minIdle" value="${redis.min_idle}" /> <property name="maxIdle" value="${redis.max_idle}" /> <!-- <property name="maxWaitMillis" value="${redis.max_wait}" /> --> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> <property name="testOnReturn" value="${redis.testOnReturn}" /> <property name="testWhileIdle" value="${redis.testWhileIdle}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="hostName" value="${redis.ip}"/> <property name="port" value="${redis.port}"/> <property name="database" value="${redis.db.index}"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> </beans>
spring下面新建文件spring-explain.xml 內容以下:express
<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!-- 加載配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> <value>classpath*:redis.properties</value> </list> </property> </bean> <context:annotation-config /> <context:component-scan base-package="me.explain.caption" > <!-- 解決Bean的可見性問題, Controller由子Context生成 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:component-scan base-package="me.explain.caption.cache" /> <import resource="spring-db.xml" /> <import resource="spring-redis.xml" /> </beans>
mybatis文件夾下面新建文件mybatis-configuration.xml,內容以下:apache
<?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> <settings> <setting name="cacheEnabled" value="true" /> <setting name="lazyLoadingEnabled" value="false" /> <setting name="aggressiveLazyLoading" value="false" /> <setting name="multipleResultSetsEnabled" value="true" /> <setting name="useColumnLabel" value="true" /> <setting name="useGeneratedKeys" value="true" /> <setting name="autoMappingBehavior" value="FULL" /> <setting name="defaultExecutorType" value="SIMPLE" /> <setting name="defaultStatementTimeout" value="25000" /> </settings> </configuration>
c:在common包下面新建文件:緩存
common包下面新建IBaseEntity.java。內容以下:mybatis
package me.explain.caption.common; import java.io.Serializable; /** * 繼承一下這個類 全部的實體類 不須要寫id字段 * * 編輯時間:2016年4月28日20:23:24 * * @author 楊中仁 * */ public class IBaseEntity implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private long id; // 編號主鍵 public long getId() { return id; } public void setId(long id) { this.id = id; } }
common包下面新建IBaseMapper.java 內容以下:mvc
package me.explain.caption.common; import org.apache.ibatis.annotations.Options; /** * 全部的mapper 繼承這個類 若是有get之類的方法 能夠直接在xml下面寫 不須要重新定義 * * 編輯時間 2016年4月28日20:59:28 * * * @author 楊中仁 * * @param <T> */ public abstract interface IBaseMapper<T extends IBaseEntity>{ public abstract <K extends IBaseEntity> T get(final Long id); @Options(useGeneratedKeys = true, keyProperty = "id") public abstract void add(final T target); public abstract void update(final T target); public abstract void delete(final Long id); }
common包下面新建IBaseRedisCache.java文件,內容以下:app
文章的字數的限制,不得不放下面的文章,地址:redis緩存的封裝類
common包下面新建文件IBaseService.java ,內容以下:
package me.explain.caption.common; /** * 使用redis 緩存 * * 2016年5月2日11:16:56 * * @author 楊中仁 * */ public abstract class IBaseService extends IBaseRedisCache {}
common包下面新建文件RootNamespace.java 內容以下:
package me.explain.caption.common; /** * 使用redis主要的key的最上層 * * 編輯時間:2016年4月28日20:20:13 * * @author 楊中仁 * */ public abstract interface RootNamespace { public static final String CAPTION_ROOT = "caption"; public static final String CAPTION_SEPARATOR = ":"; }