Spring+Struts2+Mybatis整合
1. 创建项目
2. 添加Spring能力
使用MyEclipse自动加载Struts2和Spring的jar包和配置文件,并在web.xml文件中添加上下文和监听器
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"> <!-- 上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>3. 添加mybatis.jar、mybatis-spring-1.2.0.jar和mysql-connector-java-5.1.26-bin.jar
4. 编写mybatis和spring配置文件
创建mybatis的配置和实体类的映射文件
在spring配置文件中添加数据源和sqlSessionFactory,spring可以管理实体bean的映射文件,自动创建别名,这样子就可以省略mybatis.xml配置文件
实体java代码:
package com.it.entity; import java.sql.Date; public class Stu { private int sid; private String sname; private String ssex; private Date sbirth; public Stu() { super(); // TODO Auto-generated constructor stub } public Stu(int sid, String sname, String ssex, Date sbirth) { super(); this.sid = sid; this.sname = sname; this.ssex = ssex; this.sbirth = sbirth; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public Date getSbirth() { return sbirth; } public void setSbirth(Date sbirth) { this.sbirth = sbirth; } }实体映射文件Stu.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" > <mapper namespace="stu"> <select id="findAll" resultType="Stu"> <!-- CDATA节 --> <![CDATA[select * from stu]]> </select> </mapper>application.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-3.1.xsd"> <!-- 数据源 --> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="ok"/> </bean> <!-- session工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"/> <!-- spring管理实体bean的映射文件 --> <property name="mapperLocations"> <list> <value>classpath:com/it/entity/Stu.xml</value> </list> </property> <!-- 自动创建别名 --> <property name="typeAliasesPackage" value="com.it.entity"/> </bean> <!-- Dao --> <bean id="stuDao" class="com.it.dao.impl.StuDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- Biz --> <bean id="stuBiz" class="com.it.biz.impl.StuBizImpl"> <property name="stuDao" ref="stuDao"/> </bean> <!-- Action --> <bean id="stuAction" class="com.it.action.StuAction"> <property name="stuBiz" ref="stuBiz"/> </bean> </beans>
5. 编写代码
DaoImple继承SqlSessionDaoSupport,自动获取sqlSession,不需要人为的去创建和关闭
代码依次为entity、Dao、DaoImpl、Biz、BizImpl、Action和Test,最后创建jsp文件和配置struts.xml
DaoImpl.java文件如下:
package com.it.dao.impl; import java.util.List; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.it.dao.StuDao; import com.it.entity.Stu; public class StuDaoImpl extends SqlSessionDaoSupport implements StuDao { public List<Stu> findAll() { return this.getSqlSession().selectList("stu.findAll"); } }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> <package name="xx" namespace="/" extends="struts-default"> <action name="doList" class="stuAction" method="doList"> <result>list.jsp</result> </action> </package> </struts>