您的位置:首页 > 数据库 > Redis

springmvc mybatis redis mysql maven搭建基本开发框架 (二)

2016-10-25 00:00 691 查看
1:common下面的工作:

a:在common下面的resources下面新建spring和mybatis文件夹,spring是给spring的配置文件使用,mybatis下面给mybatis配置文件使用,mybatis下面新建mapper文件夹。业务的mapper.xml都放在下面。

b:spring文件夹下面新建spring-db.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: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文件,内容如下:

<?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 内容如下:

<?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,内容如下:

<?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。内容如下:

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 内容如下:

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文件,内容如下:

文章的字数的限制,不得不放下面的文章,地址: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 = ":";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring MyBatis Redis MySQL
相关文章推荐