spring4+mybatis框架构建

applicationContext.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"
	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/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">
           
	<context:annotation-config />
	<context:component-scan base-package="com.myland.*" >
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
	</context:component-scan>

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	
	<bean id="dataSource1"	class="org.apache.commons.dbcp.BasicDataSource"	destroy-method="close">				
		<property name="driverClassName"  value="${mysql.driverClassName}"></property>   
		<property name="url" value="${mysql.url}"></property>    
		<property name="username" value="${mysql.username}"></property>   
		<property name="password" value="${mysql.password}"></property>   
		<property name="initialSize" value="${mysql.initialSize}"></property>   
		<property name="maxActive" value="${mysql.maxActive}"></property>     
		<property name="maxIdle" value="${mysql.maxIdle}"></property>  
		<property name="timeBetweenEvictionRunsMillis" value="${mysql.timeBetweenEvictionRunsMillis}"></property>  
		<property name="minEvictableIdleTimeMillis" value="${mysql.minEvictableIdleTimeMillis}"></property>  
	</bean>
	
	<!-- 阿里连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  init-method="init" destroy-method="close">
   		<!-- 驱动名称   -->
        <property name="DriverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${mysql.url}" /> 
        <!-- 数据库用户名称   -->
        <property name="username" value="root" />  
        <!-- 数据库密码 -->  
        <property name="password" value="root" />  
        <!-- 连接池最大使用连接数量 -->  
        <property name="maxActive" value="20" />  
        <!-- 初始化大小 -->  
        <property name="initialSize" value="5" />  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="60000" />  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="2" />  
        <!-- 逐出连接的检测时间间隔 -->  
        <property name="timeBetweenEvictionRunsMillis" value="3000" />  
        <!-- 最小逐出时间  --> 
        <property name="minEvictableIdleTimeMillis" value="300000" />  
        <!-- 测试有效用的SQL Query -->  
        <property name="validationQuery" value="SELECT 'x'" />  
        <!-- 连接空闲时测试是否有效 -->  
        <property name="testWhileIdle" value="true" />  
        <!-- 获取连接时测试是否有效 -->  
        <property name="testOnBorrow" value="false" />  
        <!-- 归还连接时是否测试有效  --> 
        <property name="testOnReturn" value="false" />
        <!-- 统计监控信息 -->
        <property name="filters" value="stat,wall,log4j" />
   	</bean>
   	

    <!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />          
         <property name="configLocation" value="classpath:mybatis-config.xml"/>    
         <property name="mapperLocations">
			<list>
				<value>classpath*:com/myland/myapp/**/*Mapper.xml</value>
			</list>
		</property>                               
    </bean>
    

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" > 
        <constructor-arg ref="sqlSessionFactory" />
    </bean>
    
    
	<!-- 事务处理  begin -->
	<bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="baseServiceMethods" expression="execution(* com.myland.demo.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />
    </aop:config>
	
	<aop:aspectj-autoproxy proxy-target-class="true"/> 


    
	<!-- 事务处理  end -->
	
	<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    </bean>
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>com.myland..Service.*</value>
            </list>
        </property>
    </bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
    </aop:config>
	

	
</beans>

 

 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>

	<properties>  		
   		<property name="dialect" value="MYSQL"/>
    </properties>
    
	<settings>
		<!-- 全局映射器启用缓存 -->  
        <setting name="cacheEnabled" value="false" /> 
        <!-- 查询时,关闭关联对象即时加载以提高性能 -->  
        <setting name="lazyLoadingEnabled" value="true" /> 
        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指定),不会加载关联表的所有字段,以提高性能 -->  
        <setting name="aggressiveLazyLoading" value="false" />  
        <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->  
        <setting name="multipleResultSetsEnabled" value="true" />  
        <!-- 允许使用列标签代替列名 -->  
        <setting name="useColumnLabel" value="true" />  
         <!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->  
        <setting name="useGeneratedKeys" value="true" />
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->  
        <setting name="autoMappingBehavior" value="FULL" />  
        <!-- 对于批量更新操作缓存SQL以提高性能  -->  
        <setting name="defaultExecutorType" value="BATCH" /> 
        <!-- 数据库超过25000秒仍未响应则超时 -->  
        <setting name="defaultStatementTimeout" value="25000" />        
	</settings>
	
	<plugins>  
    	<plugin interceptor="com.myland.framework.Interceptor.PaginationInterceptor"></plugin>  
    </plugins>
	
</configuration>

 BaseService,参考网上一位同仁代码

public abstract class BaseService<T extends Serializable>{
	
	@Resource
	private SqlSessionTemplate sqlSessionTemplate ;
	
	public static final String SQLNAME_SEPARATOR = "Mapper.";
	
	private String sqlNamespace = getDefaultSqlNamespace(); 

	public String getSqlNamespace() {  
        return sqlNamespace;  
    }
	
	public void setSqlNamespace(String sqlNamespace) {  
        this.sqlNamespace = sqlNamespace;  
    }
	
	/** 
     * 将SqlMapping命名空间与给定的SqlMapping名组合在一起。 
     * @param sqlName SqlMapping名  
     * @return 组合了SqlMapping命名空间后的完整SqlMapping名  
     */  
	protected String getSqlName(String sqlName) {  
        return sqlNamespace + SQLNAME_SEPARATOR + sqlName;  
    }

	
	
	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	/**
	 * Mapper命名空间
	 * @Title: getDefaultSqlNamespace
	 * @Description: TODO
	 * @return
	 */
	protected String getDefaultSqlNamespace() {  
        Class<?> genericClass = MyBeanUtils.getGenericClass(this.getClass());  
        return genericClass == null ? null : genericClass.getName();  
    }
	/**
	 * 根据条件查询单条记录
	 * @Title: selectOne
	 * @Description: TODO
	 * @param query
	 * @return
	 */
	public <V extends T> V selectOne(T query) {
		Assert.notNull(query);  
        try {  
            Map<String, Object> params = MyBeanUtils.toMap(query);  
            return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT), params);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询一条记录出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }
	}
	/**
	 * 根据主键查询记录
	 * @Title: selectByKey
	 * @Description: TODO
	 * @param id
	 * @return
	 */
	public <V extends T> V selectByKey(Long id) {
		Assert.notNull(id);  
        try {  
            return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_ONE), id);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询一条记录出错!语句:%s", getSqlName(SqlId.SQL_SELECT_ONE)), e);  
        }
	}
	/**
	 * 根据条件查询多条记录
	 * @Title: selectList
	 * @Description: TODO
	 * @param query
	 * @return
	 */
	public <V extends T> List<V> selectList(T query) {  
        try {  
            Map<String, Object> params = MyBeanUtils.toMap(query);  
            return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), params);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }  
    }
	/**
	 * 查询所有记录
	 * @Title: selectAll
	 * @Description: TODO
	 * @return
	 */
	public <V extends T> List<V> selectAll() {  
        try {  
            return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT));  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询所有对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }  
    }
	/**
	 * 根据条件查询记录返回Map
	 * @Title: selectMap
	 * @Description: TODO
	 * @param query
	 * @param mapKey
	 * @return
	 */
	public <K, V extends T> Map<K, V> selectMap(T query, String mapKey) {  
        Assert.notNull(mapKey, "[mapKey] - must not be null!");  
        try {  
            Map<String, Object> params = MyBeanUtils.toMap(query);  
            return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), params, mapKey);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询对象Map时出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }  
    }
	
	/** 
     * 设置分页 
     * @param pageInfo 分页信息 
     * @return SQL分页参数对象 
     */
	protected RowBounds getRowBounds(Pageable pageable) {  
        RowBounds bounds = RowBounds.DEFAULT;  
        if (null != pageable) {  
            bounds = new RowBounds(pageable.getOffset(), pageable.getPageSize());  
        }  
        return bounds;  
    }
	/** 
     * 获取分页查询参数 
     * @param query 查询对象 
     * @param pageable 分页对象 
     * @return Map 查询参数 
     */
	protected Map<String, Object> getParams(T query, Pageable pageable) {  
        Map<String, Object> params = MyBeanUtils.toMap(query, getRowBounds(pageable));  
        if (pageable != null && pageable.getSort() != null) {  
            String sorting = pageable.getSort().toString();  
            params.put("sorting", sorting.replace(":", ""));  
        }  
        return params;  
    }
	/**
	 * 分页查询
	 * @Title: selectList
	 * @Description: TODO
	 * @param query
	 * @param pageable
	 * @return
	 */
	public <V extends T> List<V> selectList(T query, Pageable pageable) {  
        try {  
        	List<V> contentList= sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable));  
        	return contentList;
        } catch (Exception e) {  
            throw new ServiceException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }  
    }
	/**
	 * 分页查询
	 * @Title: selectPageList
	 * @Description: TODO
	 * @param query
	 * @param pageable
	 * @return
	 */
	public <V extends T> Page<V> selectPageList(T query, Pageable pageable) {  
        try {  
            List<V> contentList = sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT),  
                    getParams(query, pageable));  
            return new PageImpl<V>(contentList, pageable, this.selectCount(query));  
        } catch (Exception e) {  
            throw new ServiceException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }  
    }
	
	public <K, V extends T> Map<K, V> selectMap(T query, String mapKey, Pageable pageable) {  
        try {  
            return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable), mapKey);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e);  
        }  
    }
	
	public Long selectCount() {  
        try {  
            return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT));  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e);  
        }  
    }
	
	public Long selectCount(T query) {  
        try {  
            Map<String, Object> params = MyBeanUtils.toMap(query);  
            return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT), params);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e);  
        }  
    }
	
	public int insert(T entity) {  
        Assert.notNull(entity);  
        try {  
//            if (StringUtils.isBlank(entity.getId()))  
//                entity.setId(generateId());  
            return sqlSessionTemplate.insert(getSqlName(SqlId.SQL_INSERT), entity);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("添加对象出错!语句:%s", getSqlName(SqlId.SQL_INSERT)), e);  
        }  
    }
	
	public int delete(T query) {  
        Assert.notNull(query);  
        try {  
            Map<String, Object> params = MyBeanUtils.toMap(query);  
            return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE), params);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e);  
        }  
    }
	
	public int deleteByKey(Long id) {  
        Assert.notNull(id);  
        try {  
            return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE_BY_ID), id);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("根据ID删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE_BY_ID)), e);  
        }  
    } 
	
	public int deleteAll() {  
        try {  
            return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE));  
        } catch (Exception e) {  
            throw new ServiceException(String.format("删除所有对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e);  
        }  
    }
	
	public int updateById(T entity) {  
        Assert.notNull(entity);  
        try {  
            return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID), entity);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("根据ID更新对象出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID)), e);  
        }  
    }
	
	public int updateByIdSelective(T entity) {  
        Assert.notNull(entity);  
        try {  
            return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE), entity);  
        } catch (Exception e) {  
            throw new ServiceException(String.format("根据ID更新对象某些属性出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE)),  
                    e);  
        }  
    } 
	
	public void deleteByIdInBatch(List<Long> idList) {  
        if (idList == null || idList.isEmpty())  
            return;  
        for (Long id : idList) {  
            this.deleteByKey(id);  
        }  
    }
	
	public void updateInBatch(List<T> entityList) {  
        if (entityList == null || entityList.isEmpty())  
            return;  
        for (T entity : entityList) {  
            this.updateByIdSelective(entity);  
        }  
    }
	
	public void insertInBatch(List<T> entityList) {  
        if (entityList == null || entityList.isEmpty())  
            return;  
        for (T entity : entityList) {  
            this.insert(entity);  
        }  
    } 
}

 jars,jar直接在工程里面取得

activation.jar
ant.jar
aopalliance-1.0.jar
asm-3.3.1.jar
aspectjweaver.jar
atomikos-util.jar
axis.jar
bcprov-jdk16-1.45.jar
ccuploader.jar
cglib-nodep-2.2.jar
commons-beanutils-1.8.3.jar
commons-chain-1.2.jar
commons-codec-1.8.jar
commons-collections-3.2.1.jar
commons-configuration-1.10.jar
commons-dbcp-1.2.1.jar
commons-digester3-3.2.jar
commons-discovery-0.5.jar
commons-email-1.3.2.jar
commons-fileupload-1.3.1.jar
commons-httpclient-3.1.jar
commons-io-2.2.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
commons-math3-3.2.jar
commons-net-3.3.jar
commons-pool-1.6.jar
commons-validator-1.3.1.jar
cxf-2.7.10.jar
dom4j-1.6.1.jar
druid-1.0.13.jar
ehcache-2.9.0.jar
ezmorph-1.0.3.jar
fastjson-1.2.5.jar
freemarker-2.3.19.jar
google-collections-1.0.jar
gson-2.2.4.jar
guava-18.0.jar
httpasyncclient-4.0-beta3.jar
httpclient-4.2.5.jar
httpcore-4.2.4.jar
httpcore-nio-4.2.4.jar
httpmime-4.1.2.jar
jackson-all-1.7.6.jar
jackson-annotations-2.2.0.jar
jackson-core-2.2.0.jar
jackson-databind-2.2.0.jar
jamm-0.2.5.jar
jamon-2.81.jar
jamontomcat-2.81.jar
jasperreports-5.2.0.jar
javassist-3.17.1-GA.jar
javax.mail.jar
jaxen-1.1.1.jar
jaxrpc.jar
jcommon-1.0.21.jar
jdom-2.0.5.jar
jedis-2.1.0.jar
jettison-1.2.jar
jfreechart-1.0.17.jar
joda-time-2.3.jar
json_simple-1.1.jar
json-20131018.jar
json-lib-2.2-jdk15.jar
jsp-api.jar
jstl.jar
jta-1_1-classes.jar
junit-4.12.jar
jxl.jar
log4j-1.2.14.jar
log4j-api-2.0.2.jar
log4j-core-2.0.2.jar
mybatis-3.2.8.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.28-bin.jar
neethi-3.0.3.jar
ognl-3.0.6.jar
ojdbc6.jar
poi-3.10-FINAL-20140208.jar
poi-3.8-20120326.jar
poi-ooxml-3.10-FINAL-20140208.jar
poi-ooxml-schemas-3.10-FINAL-20140208.jar
quartz-all-1.8.6.jar
sax.jar
servlet-api.jar
slf4j-api-1.7.7.jar
spring-aop-4.0.5.RELEASE.jar
spring-aspects-4.0.5.RELEASE.jar
spring-beans-4.0.5.RELEASE.jar
spring-context-4.0.5.RELEASE.jar
spring-context-support-4.0.5.RELEASE.jar
spring-core-4.0.5.RELEASE.jar
spring-data-commons-core-1.4.0.RELEASE.jar
spring-data-redis-1.0.2.RELEASE.jar
spring-expression-4.0.5.RELEASE.jar
spring-framework-bom-4.0.5.RELEASE.jar
spring-instrument-4.0.5.RELEASE.jar
spring-instrument-tomcat-4.0.5.RELEASE.jar
spring-jdbc-4.0.5.RELEASE.jar
spring-messaging-4.0.5.RELEASE.jar
spring-orm-4.0.5.RELEASE.jar
spring-oxm-4.0.5.RELEASE.jar
spring-test-4.0.5.RELEASE.jar
spring-tx-4.0.5.RELEASE.jar
spring-web-4.0.5.RELEASE.jar
spring-webmvc-4.0.5.RELEASE.jar
standard.jar
stax2-api-3.1.1.jar
tk-filters.jar
tomcat-jdbc.jar
tomcat-juli.jar
tomcat-redis-session-manager-1.2-tomcat-7.jar
tomcat-util.jar
transactions-api.jar
transactions-jdbc.jar
transactions-jta.jar
transactions.jar
upacp_sdk-1.0.0.jar
woodstox-core-asl-4.2.0.jar
wsdl4j-1.6.3.jar
xercesImpl.jar
xmemcached-1.4.3.jar
xml-apis.jar
xmlbeans-2.3.0.jar
xmlschema-core-2.1.0.jar
xstream-1.4.3.jar
xwork-core-2.3.16.1.jar
zj_webservice_client.jar
zxing-core-2.2.jar
zxing-javase-2.2.jar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值