XML配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" xmlns:p="http://www.springframework.org/schema/p">
<!--指定要扫哪些包-->
<context:component-scan base-package="com.qd"/>
<!--主持注解的方式-->
<context:annotation-config/>
数据库链接
<context:property-placeholder location="classpath:db.properties"/> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.asms.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.asms.url}"/> <property name="user" value="${jdbc.asms.username}"/> <property name="password" value="${jdbc.asms.password}"/> <property name="initialPoolSize" value="10"/> <property name="minPoolSize" value="${jdbc.asms.minPoolSize}"/> <property name="maxPoolSize" value="${jdbc.asms.maxPoolSize}"/> </bean>
<!-- 配置jdbc模板对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
数据库源文件:db.properties
jdbc.asms.driverClassName = com.mysql.jdbc.Driver #jdbc.url =jdbc:oracle:thin:@localhost:1521:orcl jdbc.asms.url= jdbc:mysql://localhost:3306/spring_test?useUnicode=true&characterEncoding=utf-8 jdbc.asms.username = root jdbc.asms.password =root jdbc.asms.minPoolSize=5 jdbc.asms.maxPoolSize=10
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--通过这个注解就替换掉了XML配置里面的 处理器映射器 处理器适配器-->
<mvc:annotation-driven/>
配置默认处理器和视图解析器
<mvc:default-servlet-handler/> <mvc:resources mapping="/jsp/**" location="/jsp/"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/jsp</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
配置拦截器
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/user/userinfoget"/> <bean class="com.springmvc.MyHandlerInterceptor"> <property name="whiteList" value="abc"></property> </bean> </mvc:interceptor> </mvc:interceptors>