web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- ==========================spring lo4j相关配置 开始========================================= -->
<!--log4j配置文件加载-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--启动一个watchdog线程每1800秒扫描一下log4j配置文件的变化-->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>1800000</param-value>
</context-param>
<!--spring log4j监听器-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- ==========================spring lo4j相关配置 结束========================================= -->
<!-- ==========================spring mvc context相关配置 开始========================================= -->
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-main.xml</param-value>
</context-param>
<!-- Spring MVC配置 -->
<servlet>
<description>可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,
名称为如spring-servlet.xml</description>
<servlet-name>spring mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Spring配置 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- ==========================spring mvc context相关配置 结束========================================= -->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-main.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<description>定义业务层和集成层对象,包括Action、Service、BO、DAO、SAO、EJB、JNDI资源
</description>
<!--===================================================================== -->
<!-- 配置外部变量文件 -->
<!--===================================================================== -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<description>可以引用CLASSPATH中common-spring.properties中定义的变量
</description>
<value>classpath:common-spring.properties</value>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<!--修改为本项目的根包名,这点很重要 -->
<context:component-scan base-package="com.fast.base"></context:component-scan>
<context:component-scan base-package="com.fast.app"></context:component-scan>
<!-- 引入其它配置文件 -->
<!--
<import resource="spring-mvc.xml"/>
-->
<import resource="spring-orm.xml"/>
</beans>
spring-mvc.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<description>此配置对应spring mvc相关拦截配置</description>
<!--开启扫描包和子包 -->
<context:component-scan base-package="com.fast.app.web"/>
<!-- 使用默认的Servlet来响应静态文件 -->
<mvc:default-servlet-handler />
<!-- 注解支持 -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/pages" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 拦截器设置 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*.do"/>
<bean class="com.fast.app.web.interceptor.CommonInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
spring-orm.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<description>此配置文件主要针对数据库连接,事务,session控制等等</description>
<!--===================================================================== -->
<!-- 数据源定义 -->
<!--===================================================================== -->
<!-- TODO: 如果使用到数据源,请将dsFactory的定义打开,并正确配置数据源JNDI -->
<!-- C3P0连接池配置 oracle-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="${jdbc.miniPoolSize}" />
<!--连接池中保留的最大连接数。-->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<!--最大空闲时间,设置值内未使用则连接被丢弃。若为0则永不丢弃。 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。-->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"/>
<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"/>
<!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->
<property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}"/>
<!--<property name="automaticTestTable" value="${jdbc.automaticTestTable}"/>-->
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>
<!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出
SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
<property name="checkoutTimeout" value="${jdbc.checkoutTimeout}"/>
<!--定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:测试的表必须在初始数据源的时候就存在。Default: null-->
<property name="preferredTestQuery" value="${jdbc.preferredTestQuery}" />
</bean>
<!--===================================================================== -->
<!-- MYBATIS 配置文件定义 -->
<!--===================================================================== -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fast.base.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 定义事务处理类,不同的数据访问方式,事务处理类不同
比如:Hibernate操作的HibernateTransactionManager,JDBC操作的使用DataSourceTransactionManager
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明使用注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
common-spring.properties 综合配置文件
#C3P0 连接池配置
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@10.20.128.115:1526:d0rsapp
jdbc.user=elisdata
jdbc.password=ilovetong
#连接池中保留的最小连接数。
jdbc.miniPoolSize= 20
#连接池中保留的最大连接数。
jdbc.maxPoolSize= 50
#初始化时获取N个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3
jdbc.initialPoolSize= 20
#最大空闲时间,设置值内未使用则连接被丢弃。若为0则永不丢弃。
jdbc.maxIdleTime= 25000
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。
jdbc.acquireIncrement= 3
#定义在从数据库获取新连接失败后重复尝试的次数。Default: 30
jdbc.acquireRetryAttempts= 30
#两次连接中间隔时间,单位毫秒。Default: 1000
jdbc.acquireRetryDelay= 1000
#如果设为true那么在取得连接的同时将校验连接的有效性。Default: false
jdbc.testConnectionOnCheckin= true
#c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。
jdbc.automaticTestTable= c3p0TestTable
#当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0
jdbc.idleConnectionTestPeriod= 18000
#定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。
#注意:测试的表必须在初始数据源的时候就存在。Default: null,该配置项不可以和automaticTestTable同时使用
jdbc.preferredTestQuery=select id from test_c3p0 where id=1
#超时时间
jdbc.checkoutTimeout=3000
log4j.properties 日志配置相关
#ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句,方便调试:
### 设置Logger输出级别和输出目的地 ###
log4j.rootLogger=info,stdout,logfile
### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### 把日志信息输出到文件:default ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=${catalina.home}/logs/default.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
###显示SQL语句部分
log4j.logger.com.mybatis=DEBUG
log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
#job
log4j.logger.com.jinguanjia.task.service.impl=INFO,task
log4j.appender.task=org.apache.log4j.DailyRollingFileAppender
log4j.appender.task.File=${catalina.home}/logs/task.log
log4j.appender.task.Append=true
log4j.appender.task.layout=org.apache.log4j.PatternLayout
log4j.appender.task.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}|%t|%-5p|%c,%L,%M|->%m%n
mybatis-config.xml mybatis综合配置文件
<?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>
<!--
通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean,
并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名。
也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(user)
<package name="com.dy.entity"/>
-->
<typeAlias alias="PRODUCTINFO" type="com.fast.app.model.ProductInfo" />
</typeAliases>
<mappers>
<mapper resource="com/fast/app/mapper/ProductInfoDaoMapper.xml"/>
</mappers>
</configuration>
基础Dao
package com.fast.base.dao;
import java.util.List;
import java.util.Map;
/**
* mybatis基础Dao
* @author LUSHUIFA242
*
*/
public interface BaseDao{
/**
* 对对象进行持久化操作,如果成功则返回1
* 失败返回-1
* @param obj
* @return
*/
<T> int save(T bean);
/**
* 批量保存
* @param objList
* @return 成功返回操作的成功的数量 失败则回滚并返回-1
*/
<T> int save(List<T> beanList);
/**
* 对对象进行持久化操作,如果成功则返回1
* 失败返回-1
* @param obj
* @return
*/
<T> int saveOrUpdate(T bean);
/**
* 批量保存与更新
* @param objList
* @return 成功返回操作的成功的数量 失败则回滚并返回-1
*/
<T> int saveOrUpdate(List<T> beanList);
/**
* 修改指定的持久化对象
* @param id
* @param obj
*/
<T> int update(T bean);
/**
* 修改指定的持久化对象,批量
* @param objList
* @return
*/
<T> int update(List<T> beanList);
/**
* 删除指定id的持久化对象
* @param id
* @return
*/
<T> int delete(Object primaryKey);
/**
* 返回持久化对象
* @param obj
* @return 找到则返回,否则返回空
*/
<T> T queryObject(T bean);
/**
* 返回持久化对象
* @param primaryKey
* @return 找到则返回,否则返回空
*/
<T> T queryObjectByPrimaryKey(Object primaryKey);
/**
* 返回持久化对象
* @param queryParam
* @return 找到则返回,否则返回空
*/
<T> T queryObjectByParam(Map<String,Object> queryParam);
/**
* 返回列表
* @param queryParam
* @return 找到则返回,否则返回空
*/
<T> List<T> queryList(T bean);
/**
* 返回列表
* @param bean
* @param limitSize
* @return 找到则返回,否则返回空
*/
<T> List<T> queryList(T bean,int limitSize);
/**
* 返回列表
* @param bean
* @param startIndex
* @param endIndex
* @return 找到则返回,否则返回空
*/
<T> List<T> queryList(T bean,int startIndex,int endIndex);
/**
* 返回列表
* @param queryParam
* @return 找到则返回,否则返回空
*/
<T> List<T> queryListByParam(Map<String,Object> queryParam);
/**
* 返回列表
* @param queryParam
* @param limitSize
* @return 找到则返回,否则返回空
*/
<T> List<T> queryListByParam(Map<String,Object> queryParam,int limitSize);
/**
* 返回列表
* @param queryParam
* @param startIndex
* @param endIndex
* @return 找到则返回,否则返回空
*/
<T> List<T> queryListByParam(Map<String,Object> queryParam,int startIndex,int endIndex);
/**
* 统计所有记录数
* @return
*/
<T> long count();
/**
* 根据条件统计
* @param bean
* @return
*/
<T> long count(T bean);
void setXmlName(String xmlName);
}
基础dao实现类
package com.fast.base.dao.impl;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.fast.base.dao.BaseDao;
@Component
public class BaseDaoImpl implements BaseDao {
@Resource(name = "sqlSession")
private SqlSession session;
private String path = "com.fast.app.mapper.";
private String xmlName;
protected BaseDaoImpl(){
path = path + this.getXmlName();
}
protected BaseDaoImpl(String xmlName) {
this.setXmlName(xmlName);
path = path + xmlName;
}
protected String getMethodPath(String methodName) {
return path +"."+ methodName;
}
/**
* 对对象进行持久化操作,如果成功则返回1
* 失败返回-1
* @param obj
* @return
*/
public <T> int save(T bean){
return this.session.insert(getMethodPath("save"), bean);
}
/**
* 批量保存
* @param objList
* @return 成功返回操作的成功的数量 失败则回滚并返回-1
*/
public <T> int save(List<T> beanList) {
return this.session.insert(getMethodPath("batchSave"), beanList);
}
/**
* 对对象进行持久化操作,如果成功则返回1
* 失败返回-1
* @param obj
* @return
*/
public <T> int saveOrUpdate(T bean) {
return this.session.insert(getMethodPath("saveOrUpdate"), bean);
}
/**
* 批量保存与更新
* @param objList
* @return 成功返回操作的成功的数量 失败则回滚并返回-1
*/
public <T> int saveOrUpdate(List<T> beanList) {
int actionCount = -1;
for (T t : beanList) {
if(saveOrUpdate(t)>0){
actionCount++;
}
}
return actionCount;
}
/**
* 修改指定的持久化对象
* @param id
* @param obj
*/
public <T> int update(T bean) {
return this.session.update(getMethodPath("update"), bean);
}
/**
* 修改指定的持久化对象,批量
* @param objList
* @return
*/
public <T> int update(List<T> beanList) {
return this.session.update(getMethodPath("batchUpdate"), beanList);
}
/**
* 删除指定id的持久化对象
* @param id
*/
public <T> int delete(Object primaryKey) {
return this.session.delete(getMethodPath("deleteByPrimaryKey"), primaryKey);
}
/**
* 返回持久化对象
* @param obj
* @return 找到则返回,否则返回空
*/
public <T> T queryObject(T bean) {
return this.session.selectOne(getMethodPath("queryObject"), bean);
}
/**
* 返回持久化对象
* @param primaryKey
* @return 找到则返回,否则返回空
*/
public <T> T queryObjectByPrimaryKey(Object primaryKey) {
return this.session.selectOne(getMethodPath("queryObjectByPrimaryKey"), primaryKey);
}
/**
* 返回持久化对象
* @param queryParam
* @return 找到则返回,否则返回空
*/
public <T> T queryObjectByParam(Map<String,Object> queryParam) {
return this.session.selectOne(getMethodPath("queryObjectByParam"), queryParam);
}
/**
* 返回列表
* @param queryParam
* @return 找到则返回,否则返回空
*/
public <T> List<T> queryList(T bean) {
return queryList(bean,-1);
}
/**
* 返回列表
* @param bean
* @param limitSize -1
* @return 找到则返回,否则返回空
*/
public <T> List<T> queryList(T bean,int limitSize) {
try {
Map<String,Object> param = new HashMap<String,Object>();
param.putAll((Map<? extends String, ? extends Object>) JSONObject.toJSON(bean));
param.put("limitSize", limitSize);
return this.session.selectList(getMethodPath("queryList"), param);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 返回列表 此方法适用分页
* @param bean
* @param startIndex
* @param endIndex
* @return 找到则返回,否则返回空
*/
public <T> List<T> queryList(T bean,int startIndex,int endIndex){
Map<String,Object> param = new HashMap<String,Object>();
param.putAll((Map<? extends String, ? extends Object>) JSONObject.toJSON(bean));
param.put("startIndex", startIndex);
param.put("endIndex", endIndex);
return this.session.selectList(getMethodPath("queryListInRange"), param);
}
/**
* 返回列表
* @param queryParam
* @return 找到则返回,否则返回空
*/
public <T> List<T> queryListByParam(Map<String,Object> queryParam){
return this.session.selectList(getMethodPath("queryListByParam"), queryParam);
}
/**
* 返回列表
* @param queryParam
* @param limitSize
* @return 找到则返回,否则返回空
*/
public <T> List<T> queryListByParam(Map<String,Object> queryParam,int limitSize){
queryParam.put("limitSize", limitSize);
return this.session.selectList(getMethodPath("queryListByParam"), queryParam);
}
/**
* 返回列表
* @param queryParam
* @param startIndex
* @param endIndex
* @return 找到则返回,否则返回空
*/
public <T> List<T> queryListByParam(Map<String,Object> queryParam,int startIndex,int endIndex){
queryParam.put("startIndex", startIndex);
queryParam.put("endIndex", endIndex);
return this.session.selectList(getMethodPath("queryListByParam"), queryParam);
}
/**
* 统计所有记录数
* @return
*/
public <T> long count(){
return this.session.selectOne(getMethodPath("count"));
}
/**
* 根据条件统计
* @param bean
* @return
*/
public <T> long count(T bean){
return this.session.selectOne(getMethodPath("count"),bean);
}
/**
* 得到数据库连接
* @return
*/
public Connection getConn(){
return this.getSession().getConnection();
}
/**
* 得到mybatis session操作类
* @return
*/
public SqlSession getSession() {
return session;
}
public void setSession(SqlSession session) {
this.session = session;
}
public String getXmlName() {
return xmlName;
}
@Override
public void setXmlName(String xmlName) {
this.xmlName = xmlName;
}
}
本文介绍了一个基于Spring框架的整合配置方案,包括web.xml、spring-main.xml等关键配置文件的详细内容,涵盖了Spring与MyBatis集成、日志配置、数据源配置等方面。
20万+

被折叠的 条评论
为什么被折叠?



