当前位置: 代码迷 >> Web前端 >> Spring+Webwork+iBatis 结合
  详细解决方案

Spring+Webwork+iBatis 结合

热度:1105   发布时间:2012-11-08 08:48:11.0
Spring+Webwork+iBatis 组合

一、/WEB-INF/web.xml????? (配置WebWork + Spring)

?

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
      <filter-name>webwork</filter-name>
      <filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
      <filter-name>webwork</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

?

二、/WEB-INF/applicationContext.xml (配置Spring + iBatis)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans default-autowire="autodetect">


<!-- 读取Mysql驱动信息参数文件      -->
<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
       <list>
        <value>WEB-INF/jdbc.properties</value>
       </list>
      </property>
</bean>

<!-- C3P0 数据源      -->
<bean id="dataSource"
      class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
      <property name="driverClass" value="${jdbc.driverClassName}" />
      <property name="jdbcUrl" value="${jdbc.url}" />
      <property name="user" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sqlMapClient"
      class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
      <property name="configLocation"
       value="WEB-INF/sql-map-config.xml" />
      <property name="dataSource" ref="dataSource" />
</bean>

<bean id="userservices" class="userServices"/>   
<bean id="mymysqldriver" class="MyMysqlDriver"/>  

<!-- 在personservices 对象中注入 sqlMapClient -->
<bean id="personservices" class="PersonServices"> 
  <property name="sqlMapClient" ref="sqlMapClient"/> 
</bean>

</beans>

?

三、/WEB-INF/jdbc.properties (配置 iBatis 的数据库驱动(MySql驱动))

?

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql1
jdbc.username=root
jdbc.password=1234

??

四、/classes/xwork.xml (配置 WebWork )

?

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">

<!-- START SNIPPET: xworkSample -->

<xwork>
<include file="webwork-default.xml"/>

<include file="xwork-Global.xml"/>
<include file="xwork-PersonAction.xml"/> 
</xwork>

?


五、/classes/xwork-Global.xml (配置 WebWork )

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<package name="global" extends="webwork-default">
      <global-results>
       <result name="error" type="redirect">/isnull.jsp</result>
      </global-results>
</package>
</xwork>

?

六、/classes/xwork-PersonAction.xml (配置 WebWork )

?

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
      <package name="ppp" extends="global">
       <action name="personaction" class="PersonAction">
        <result name="success" type="dispatcher">/Person.jsp</result>
        <interceptor-ref name="params"/>
       </action>
      </package>
</xwork>

?

七、/WEB-INF/sql-map-config.xml (配置 iBatis)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>

        <settings
            cacheModelsEnabled="true"
            enhancementEnabled="true"
            lazyLoadingEnabled="true"
            maxRequests="32"
            maxSessions="10"
            maxTransactions="5"
            useStatementNamespaces="false"
        />
        <sqlMap resource="/PersonDataEntity.xml" />
</sqlMapConfig>

?

八、/classes/PersonDataEntity.xml

?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
      "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Person">
<select id="selectPerson" resultClass="fizi.PersonDataEntity">
      SELECT PER_ID as id,
      PER_FIRST_NAME as firstName,
      PER_LAST_NAME as lastName,
      PER_BIRTH_DATE as birthDate,
      PER_WEIGHT_KG as weightInKilograms,
      PER_HEIGHT_M as heightInMeters
      FROM PERSON
      WHERE PER_ID = #value#
</select>
<insert id="insertPerson" parameterClass="fizi.PersonDataEntity">
      <![CDATA[
       INSERT INTO
       PERSON (PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
       PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
       VALUES (#id#, #firstName#, #lastName#,
                 #birthDate#, #weightInKilograms#, #heightInMeters#)
      ]]>
</insert>
<update id="updatePerson" parameterClass="fizi.PersonDataEntity">
      <![CDATA[
       UPDATE PERSON
       SET PER_FIRST_NAME = #firstName#,
       PER_LAST_NAME = #lastName#, PER_BIRTH_DATE = #birthDate#,
       PER_WEIGHT_KG = #weightInKilograms#,
       PER_HEIGHT_M = #heightInMeters#
       WHERE PER_ID = #id#
      ]]>
</update> 
<delete id="deletePerson" parameterClass="fizi.PersonDataEntity">
      <![CDATA[
       DELETE PERSON
       WHERE PER_ID = #id#
      ]]>
</delete>     
</sqlMap>

?

九、/classes/webwork.properties (配置????? WebWork )

?

### extension for actions  
webwork.action.extension=action,java,aspx,jhtml  

### Configuration reloading
### This will cause the configuration to reload xwork.xml when it is changed
webwork.i18n.reload=true
webwork.configuration.xml.reload=true

### Load custom default resource bundles
webwork.custom.i18n.resources=default

### character encoding
### webwork.locale=zh_CN
webwork.i18n.encoding=UTF-8

### upload jakarta, temp dir, maxSize = 10M
webwork.multipart.parser=jakarta

### webwork.ui.theme=xhtml
webwork.ui.theme=simple

### WebWork-Spring Integration
webwork.objectFactory = spring
webwork.objectFactory.spring.autoWire = name

?

十、部分代码 PersonDataEntity.java

?

import java.util.Date;

public class PersonDataEntity {
private int id;
private String firstName;
private String lastName;
private Date birthDate;
private double weightInKilograms;
private double heightInMeters;
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public Date getBirthDate() {
      return birthDate;
}
public void setBirthDate(Date birthDate) {
      this.birthDate = birthDate;
}
public String getFirstName() {
      return firstName;
}
public void setFirstName(String firstName) {
      this.firstName = firstName;
}
public double getHeightInMeters() {
      return heightInMeters;
}
public void setHeightInMeters(double heightInMeters) {
      this.heightInMeters = heightInMeters;
}
public String getLastName() {
      return lastName;
}
public void setLastName(String lastName) {
      this.lastName = lastName;
}
public double getWeightInKilograms() {
      return weightInKilograms;
}
public void setWeightInKilograms(double weightInKilograms) {
      this.weightInKilograms = weightInKilograms;
}
}

?十一、PersonServices.java (要使用iBatis 则需要 继承 类 SqlMapClientDaoSupport )

?

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

public class PersonServices extends SqlMapClientDaoSupport {

public PersonDataEntity GetPerson(Integer pID) {

      return (PersonDataEntity) getSqlMapClientTemplate()
        .queryForObject("selectPerson", pID);
}
}

?

十二、PersonAction.java 在WebWork中方便调用的Action所以需要实现 接口Action

?

import Socket.sclient;

import com.opensymphony.xwork.Action;

public class PersonAction implements Action{
private PersonDataEntity persondataentity = new PersonDataEntity();
private PersonServices personservices;
public String execute() throws Exception {
      // TODO Auto-generated method stub
      Integer pid = new Integer(100001);
      sclient client = new sclient();       //调用Socket,这里你可以不需要(我写的:)
      client.ReadServer();
      persondataentity = personservices.GetPerson(pid);
      if (persondataentity!=null) 
      {
       return SUCCESS;
      }
      else
      {
       return ERROR;
      }
}
public void setPersondataentity(PersonDataEntity persondataentity) {
      this.persondataentity = persondataentity;
}
public void setPersonservices(PersonServices personservices) {
      this.personservices = personservices;
}
public PersonDataEntity getPersondataentity() {
      return persondataentity;
}

}

?

十三、实际调用 PersonAction的 jsp文件 QueryPerson.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Person查询</title>
</head>
<body>
<table border=0 width=97%>
      <tr><td align="left">
       <form name="personaction" action="personaction.action" method="post">
       <form name="personaction" action="personaction.aspx" method="post">
        PersonID:&nbsp;<input type="text" name="persondataentity.id"><br>
        <input type="submit" name="查询"><br>
       </form>
      </td></tr>
</table>
</body>
</html>

?十四、做个简单的查询结果返回页面 person.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/webwork" prefix="ww" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
人员:<label style="color: red;" ><ww:property value='persondataentity.firstName'/></label>查询成功!
</body>
</html>

?
?

  相关解决方案