更多文章请进入:我的开源网
1.所需要jar包:
2.web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>commServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 手动配置加载的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/*.xml</param-value>
</init-param>
<!-- 项目启动时加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet映射 -->
<servlet-mapping>
<!-- 所有.aspx结尾的请求都由commServlet管理 -->
<servlet-name>commServlet</servlet-name>
<url-pattern>*.aspx</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 初始化参数,编码是UTF-8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 过滤器映射 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 登录过滤 -->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.cjh.filter.LoginFilter</filter-class>
</filter>
<!-- 登录过滤映射 -->
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- 错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error_page/404.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.Spring MVC配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- ==================主键扫描,配置要扫描的Java类============================ -->
<context:component-scan base-package="com.cjh" />
<!-- ==================启动Spring MVC的注解功能,完成请求和注解POJO的映射,配置一个基于注解的定制的WebBindingInitializer,
解决日期转换问题,方法级别的处理器映射============================ -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<!-- 配置一下对json数据的转换 -->
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- ==================定义一个视图解析类,基于ResourceView的解析器 ,此解析器是在url解析器上,
加入了对jstl的支持============================ -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<!-- <property name="prefix" value="/jsp/" /> -->
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- ================== 处理文件上传 ============================ -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 (ISO-8859-1) -->
<property name="defaultEncoding" value="utf-8" />
<!-- 最大内存大小 (10240)-->
<property name="maxInMemorySize" value="10240" />
<!-- 上传后的临时目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
<property name="uploadTempDir" value="/upload_temp/" />
<!-- 最大文件大小,-1为无限止(-1),注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="-1" />
</bean>
<!-- ================== SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException
该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 ============================ -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到fileError.jsp页面 -->
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">
error_page/fileUploadError
</prop>
</props>
</property>
</bean>
</beans>
4.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"
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">
<!-- ===================使用 annotation============================== -->
<context:annotation-config />
<!-- ===================强制使用CGLIB代理============================== -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- ===================C3P0 数据源配置=============================== -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 数据库连接url -->
<property name="jdbcUrl"
value="jdbc:mysql://127.0.0.1:3306/my?autoReconnect=true&useUnicode=true&characterEncoding=utf8" />
<!-- 用户名 -->
<property name="user" value="root" />
<!-- 密码 -->
<property name="password" value="root" />
<!-- 设置数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="100" />
<!-- 设置数据库连接池的最小连接数 -->
<property name="minPoolSize" value="3" />
<!-- 设置数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="3" />
<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
<property name="maxIdleTime" value="100" />
</bean>
<!-- ===================ibatis配置=============================== -->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocations">
<value>classpath:ibatis/sqlmap-config.xml</value>
</property>
</bean>
<!-- ===================事务管理器=============================== -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--启动spring注解功能-->
<tx:annotation-driven transaction-manager="txManager" />
<aop:config>
<aop:pointcut id="baseServiceMethods" expression="execution(* com.cjh.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />
</aop:config>
<aop:aspectj-autoproxy />
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="del*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
</beans>
5.Ibatis配置文件:
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/cjh/model/Userinfo.xml" />
</sqlMapConfig>
6.操作语句配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Userinfo" type="com.cjh.model.Userinfo"/>
<insert id="saveUser" parameterClass="Userinfo">
<![CDATA[
INSERT INTO
user_info(
username,
password
) VALUES (
#username#,
#password#
)
]]>
</insert>
</sqlMap>
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;
public class BaseDao extends SqlMapClientDaoSupport{
@Resource
private SqlMapClient sqlMapClient;
@PostConstruct
public void initSqlMapClient() {
super.setSqlMapClient(sqlMapClient);
}
}