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">
<display-name>ESB</display-name>
<!-- spring监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-beans.xml
</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring的配置文件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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ESB" />
<property name="username" value="system" />
<property name="password" value="ngbss" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="DataSource" />
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="up*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.huawei.ngbss.impl.*.*(..))"
id="ImplPoincut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="ImplPoincut" />
</aop:config>
<bean id="userDao" class="com.huawei.ngbss.dao.UserDao">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
<bean id="userImpl" class="com.huawei.ngbss.impl.UserImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean name="UserAction" class="com.huawei.ngbss.action.UserAction">
<property name="service" ref="userImpl"></property>
</bean>
</beans>
SqlMapConfig的配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- Configure a built-in transaction manager. If you're using an app server,
you probably want to use its transaction manager and a managed datasource -->
<!--<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:ESB" />
<property name="JDBC.Username" value="system" />
<property name="JDBC.Password" value="ngbss" />
</dataSource>
</transactionManager>
--><!-- List the SQL Map XML files. They can be loaded from the classpath,
as they are here (com.domain.data...) -->
<sqlMap resource="User.xml" />
<!-- List more here... <sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/> -->
</sqlMapConfig>
User.xml的sql语句文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
<typeAlias alias="UserVo" type="com.huawei.ngbss.vo.User" />
<resultMap class="UserVo" id="UserResult">
<result property="s_id" column="S_ID" />
<result property="c_id" column="C_ID" />
<result property="s_name" column="S_NAME" />
</resultMap>
<!-- select all User-->
<select id="selectAllUser" resultMap="UserResult">
select * from T_Stu
</select>
<!-- select one user by Id -->
<select id="selectUserById" parameterClass="String" resultClass="UserVo">
select * from T_Stu where s_id=#s_id#
</select>
<!-- insert a new user -->
<insert id="insertNewUser" parameterClass="UserVo">
insert into T_Stu (s_id,c_id,s_name) values(#s_id#,#c_id#,#s_name#)
</insert>
<!-- update one user by id-->
<update id="updateUserInfo" parameterClass="UserVo">
update T_Stu set s_id=#s_id#,c_id=#c_id#,s_name=#s_name# where s_id=#s_id#
</update>
<delete id="deleteUserByid" parameterClass="String">
delete from T_Stu where s_id=#s_id#
</delete>
</sqlMap>
Struts2的Struts配置文件的内容
<?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>
<!--下述语句是将struts交给spring管理 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.objectFactory" value="spring" />
<package name="struts2" extends="struts-default">
<action name="user" class="UserAction">
<result name="error">/system/error.jsp</result>
<result name="AllUserInfo">/user/listUserInfo.jsp</result>
<result name="updateUserInfo">/user/updateUserInfo.jsp</result>
</action>
</package>
</struts>
UserDao类的内容:
/**
*
*/
package com.huawei.ngbss.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import com.huawei.ngbss.vo.User;
/**
* @author c00187960
*
*/
public class UserDao {
private SqlMapClientTemplate sqlMapClientTemplate;
public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
public boolean addUser(User user)
{
try {
sqlMapClientTemplate.insert("insertNewUser", user);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public boolean updateUser(User user)
{
try {
sqlMapClientTemplate.update("updateUserInfo", user);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public boolean deleteUser(String s_id)
{
try {
sqlMapClientTemplate.delete("deleteUserByid", s_id);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public User selectOneUser(String s_id)
{
User user=new User();
try {
user=(User) sqlMapClientTemplate.queryForObject("selectUserById", s_id);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
public List selectAllUser()
{
List listUser=new ArrayList();
try {
listUser=sqlMapClientTemplate.queryForList("selectAllUser");
} catch (Exception e) {
e.printStackTrace();
}
return listUser;
}
}
<?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">
<display-name>ESB</display-name>
<!-- spring监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-beans.xml
</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring的配置文件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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ESB" />
<property name="username" value="system" />
<property name="password" value="ngbss" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="DataSource" />
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="up*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.huawei.ngbss.impl.*.*(..))"
id="ImplPoincut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="ImplPoincut" />
</aop:config>
<bean id="userDao" class="com.huawei.ngbss.dao.UserDao">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
<bean id="userImpl" class="com.huawei.ngbss.impl.UserImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean name="UserAction" class="com.huawei.ngbss.action.UserAction">
<property name="service" ref="userImpl"></property>
</bean>
</beans>
SqlMapConfig的配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- Configure a built-in transaction manager. If you're using an app server,
you probably want to use its transaction manager and a managed datasource -->
<!--<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:ESB" />
<property name="JDBC.Username" value="system" />
<property name="JDBC.Password" value="ngbss" />
</dataSource>
</transactionManager>
--><!-- List the SQL Map XML files. They can be loaded from the classpath,
as they are here (com.domain.data...) -->
<sqlMap resource="User.xml" />
<!-- List more here... <sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/> -->
</sqlMapConfig>
User.xml的sql语句文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
<typeAlias alias="UserVo" type="com.huawei.ngbss.vo.User" />
<resultMap class="UserVo" id="UserResult">
<result property="s_id" column="S_ID" />
<result property="c_id" column="C_ID" />
<result property="s_name" column="S_NAME" />
</resultMap>
<!-- select all User-->
<select id="selectAllUser" resultMap="UserResult">
select * from T_Stu
</select>
<!-- select one user by Id -->
<select id="selectUserById" parameterClass="String" resultClass="UserVo">
select * from T_Stu where s_id=#s_id#
</select>
<!-- insert a new user -->
<insert id="insertNewUser" parameterClass="UserVo">
insert into T_Stu (s_id,c_id,s_name) values(#s_id#,#c_id#,#s_name#)
</insert>
<!-- update one user by id-->
<update id="updateUserInfo" parameterClass="UserVo">
update T_Stu set s_id=#s_id#,c_id=#c_id#,s_name=#s_name# where s_id=#s_id#
</update>
<delete id="deleteUserByid" parameterClass="String">
delete from T_Stu where s_id=#s_id#
</delete>
</sqlMap>
Struts2的Struts配置文件的内容
<?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>
<!--下述语句是将struts交给spring管理 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.objectFactory" value="spring" />
<package name="struts2" extends="struts-default">
<action name="user" class="UserAction">
<result name="error">/system/error.jsp</result>
<result name="AllUserInfo">/user/listUserInfo.jsp</result>
<result name="updateUserInfo">/user/updateUserInfo.jsp</result>
</action>
</package>
</struts>
UserDao类的内容:
/**
*
*/
package com.huawei.ngbss.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import com.huawei.ngbss.vo.User;
/**
* @author c00187960
*
*/
public class UserDao {
private SqlMapClientTemplate sqlMapClientTemplate;
public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
public boolean addUser(User user)
{
try {
sqlMapClientTemplate.insert("insertNewUser", user);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public boolean updateUser(User user)
{
try {
sqlMapClientTemplate.update("updateUserInfo", user);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public boolean deleteUser(String s_id)
{
try {
sqlMapClientTemplate.delete("deleteUserByid", s_id);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public User selectOneUser(String s_id)
{
User user=new User();
try {
user=(User) sqlMapClientTemplate.queryForObject("selectUserById", s_id);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
public List selectAllUser()
{
List listUser=new ArrayList();
try {
listUser=sqlMapClientTemplate.queryForList("selectAllUser");
} catch (Exception e) {
e.printStackTrace();
}
return listUser;
}
}