(注:如果刚创建的maven项目中没显示src/main/resources与src/test/java目录,可以右键项目,再properties,在Java Build Path中,将JRE System Library修改为1.7版本,如下)
配置文件中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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.hin.dao" /> <context:component-scan base-package="com.hin.service" /> <!-- 配置数据源 --> <bean> <property value="com.mysql.jdbc.Driver"/> <property value="jdbc:mysql://localhost:3306/db_news"/> <property value="root"/> <property value="root"/> </bean> <!-- 配置mybatis的sqlSessionFactory --> <bean> <property ref="dataSource" /> <!-- 自动扫描mappers.xml文件 ,要加上classpath:com/...--> <property value="classpath:com/hin/mappers/*.xml"></property> <!-- mybatis配置文件 --> <property value="classpath:mybatis-config.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean> <property value="com.hin.dao" /> <property value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean> <property ref="dataSource" /> </bean> <!-- 配置事务通知属性 --> <tx:advice transaction-manager="transactionManager"> <!-- 定义事务传播属性 --> <tx:attributes> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" /> <tx:method propagation="REQUIRED" read-only="true" /> <tx:method propagation="REQUIRED" read-only="true" /> <tx:method propagation="REQUIRED" read-only="true" /> <tx:method propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.hin.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> </beans>
spring-mvc.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <bean> <property value="/WEB-INF/html/"/> </bean> <!-- 使用注解的包,包括子集 --> <context:component-scan base-package="com.hin.controller" /> <bean> <property value="true"/> <property value=""/> <property value=".html"/> <property value="true"/> </bean> </beans>
完后配置web.xml,如下: