如果你对于这三个框架都还不怎么清楚,建议先把每个框架弄懂了再来看,压力会比较小
首先你要清楚 每一个框架在整合中起到一个什么作用,以及他们相互之间有什么联系
1、spring一般是用于管理 数据源,注入 mybatis , struts2所配置的 javabean 的属性
2、mybatis负责提供对数据库的操作,他的数据库配置连接操作 就交给spring 的配置文件
3、struts2 负责对前端的响应 ,(假如在action中要操作数据库)则通过封装一个操作数据库的类(由mybatis管理)的实例(该实例可以通过spring在配置文件中注入),并把数据结果呈现到页面
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/lib/applicationContext.xml</param-value>
</context-param>
<!-- 加载spring配置文件applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置struts2文件 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="" extends="struts-default">
<action name="testAction" class="com.icreate.service.testAction">
<result name="success">/index.jsp</result>
</action>
</package>
<package name="ajaxDemo" namespace="" extends="json-default"> <!-- 返回json数据 -->
<action name="tAjax" class="com.icreate.service.tAjax" method="loadData">
<result name="success" type="json">
<param name="root">map</param>
</result>
</action>
<action name="getPos" class="com.icreate.service.tAjax" method="loadPosition">
<result name="position" type="json">
<param name="root">posList</param>
</result>
</action>
</package>
</struts>
mybatis配置文件中的 数据查询映射文件(mapper) ,这里mybatis的版本是3.2.0
user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.icreate.dao.UserDao">
<resultMap type="com.icreate.entity.User" id="resultList">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<!-- 查询表中记录总数 -->
<select id="countAll" resultType="int">
select count(*) c from tb_user
</select>
<select id="selectAll" resultMap="resultList">
select * from tb_user order by username asc
</select>
<insert id="insert" parameterType="com.icreate.entity.User">
insert into tb_user(username,password) values(#{username},#{password})
</insert>
<update id="update" parameterType="com.icreate.entity.User">
update tb_user set username=#{username},password=#{password}
where username=#{username}
</update>
<delete id="delete" parameterType="String">
delete form tb_user where username=#{username}
</delete>
<select id="findByUserName" parameterType="String" resultType="com.icreate.entity.User">
select * from tb_user where username=#{username}
</select>
</mapper>
mybatis的config文件
mybatis-configuration.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>
<mappers>
<mapper resource="com/icreate/dao/UserDao.xml"/>
</mappers>
</configuration>
spring的配置文件,这个是结合的关键,它管理着mybatis同数据库的连接,以及各个属性的注入
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 配置数据源 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/myshop?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="5817513"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework
.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 产生一个会话工厂(主要是为mybatis产生实例服务),具体理解可以先看mybatis教程 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="/WEB-INF/classes/Mybatis-Configuration.xml"></property>
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 根据映射关系,产生一个具体的实例 -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.icreate.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 注入mybatis中的一个属性 -->
<bean id="userService" class="com.icreate.service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 注入struts2中一个action的属性,userService -->
<bean id="testAction" class="com.icreate.service.testAction">
<property name="userService" ref="userService">
</property>
</bean>
</beans>
action注入中,必须要求action定义的变量名字和注入bean的id名字保持一致,否者注入失败~~~