1.需要的架包:
2.项目结构
3.applicationContext-common.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 启动注解 -->
<context:annotation-config/>
<!-- 扫描自定义文件夹-->
<!-- 配置DataSource或者BasicDataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jwxt?userUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="zgq123"/>
</bean>
<!-- 创建sessionFactory工厂类 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<!-- 引入mapper配置文件 -->
<property name="mapperLocations" value="classpath:com/zgq/mapper/*.xml"/>
</bean>
<!-- 配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 隔离 传播特性 -->
<bean id="proxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<!-- 隔离 传播 事物 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="delete*" >PROPAGATION_REQUIRED</prop>
<prop key="updata*" >PROPAGATION_REQUIRED</prop>
<prop key="query*">readOnly</prop>
</props>
</property>
</bean>
</beans>
4.applicationContext-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 确定作用范围 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean" scope="prototype" >
<!-- 引入接口 -->
<property name="mapperInterface" value="com.zgq.dao.IStudent"/>
<property name="sqlSessionFactory" ref="sessionFactory"/>
</bean>
<bean id="studentServiceImpl" class="com.zgq.service.impl.StudentServiceImpl" scope="prototype">
<property name="studentImpl" ref="studentDao"/>
</bean>
<bean id="studentAction" class="com.zgq.action.StudentAction">
<property name="studentServiceImpl" ref="studentServiceImpl"/>
</bean>
</beans>
5.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>
<typeAliases>
<package name="com/zgq/enity"/>
</typeAliases>
<!--环境配置,连接的数据库,这里使用的是MySQL-->
<!-- <environments default="mysql">
<environment id="mysql">
指定事务管理的类型,这里简单使用Java的JDBC的提交和回滚设置
<transactionManager type="JDBC"></transactionManager>
dataSource 指连接源配置,POOLED是JDBC连接对象的数据源连接池的实现
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jwxt?userUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="zgq123"></property>
</dataSource>
</environment>
</environments> -->
<!-- <mappers>
这是告诉Mybatis区哪找持久化类的映射文件,对于在src下的文件直接写文件名,
如果在某包下,则要写明路径,如:com/mybatistest/config/User.xml
<mapper resource="com/zgq/mapper/StudentinfoMapper.xml"></mapper>
<mapper resource="com/zgq/mapper/SclassMapper.xml"></mapper>
<mapper resource="com/zgq/mapper/PowerMapper.xml"></mapper>
</mappers> -->
</configuration>
6.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="pk" extends="struts-default" namespace="/">
<action name="student_*" class="studentAction" method="{1}">
<result name="stuList">/student.jsp</result>
</action>
</package>
</struts>
7.web.xml
中修改为下面:::
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext-*.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml</param-value>
</init-param>
</filter>