1、新建一工程S2SI,拷贝以下jar包到lib文件夹里面
commons-dbcp.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
commons-pool.jar
freemarker-2.3.13.jar
ibatis-2.3.0.677.jar
mysql-connector.jar
ognl-2.6.11.jar
spring.jar
struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
xwork-2.1.2.jar
2、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app 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"> <!-- spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/jp/com/syspro/config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- encode filter --> <filter> <filter-name>encodeFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodeFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 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> <!-- welcome-file-list --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3、struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.action.extension" value="do,action" /> <constant name="struts.objectFactory" value="spring" /> <package name="user" namespace="/user" extends="struts-default"> <action name="*" class="userAction" method="{1}" /> </package> </struts>
4、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" 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" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="WEB-INF/classes/jp/com/syspro/config/sqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* jp.com.syspro.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" /> </tx:attributes> </tx:advice> <bean id="userDao" class="jp.com.syspro.dao.impl.UserDaoImpl"> <property name="sqlMapClient"> <ref local="sqlMapClient" /> </property> </bean> <bean id="userService" class="jp.com.syspro.service.impl.UserServiceImpl"> <property name="userDao"> <ref local="userDao" /> </property> </bean> <bean name="userAction" class="jp.com.syspro.action.UserAction"> <property name="userService"> <ref local="userService" /> </property> </bean> </beans>
5、sqlMapConfig.xml配置
<?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> <sqlMap resource="jp/com/syspro/dao/map/user.xml" /> </sqlMapConfig>
6、sqlMap文件user.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> <select id="getUserById" parameterClass="java.lang.Integer" resultClass="jp.com.syspro.domain.UserVo"> select * from tb_user where id=#id# </select> <select id="getAll" resultClass="jp.com.syspro.domain.UserVo"> select * from tb_user </select> <insert id="addUser" parameterClass="jp.com.syspro.domain.UserVo"> <selectKey resultClass="java.lang.Integer" keyProperty="id"> select @@identity as id </selectKey> insert into tb_user values (#username#,#password#,#id#,#name#,#gender#,#birthday#,#phone#,#address#,#email#) </insert> <delete id="deleteUser" parameterClass="java.lang.Integer"> delete from tb_user where id=#id# </delete> <update id="updateUser" parameterClass="jp.com.syspro.domain.UserVo"> update tb_user set username=#username#,password=#password#,name=#name#,gender=#gender#,birthday=#birthday#,phone=#phone#,address=#address#,email=#email# where id=#id# </update> </sqlMap>
7、UserDaoImpl.java
package jp.com.syspro.dao.impl; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import jp.com.syspro.dao.UserDao; import jp.com.syspro.domain.UserVo; public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao { public Integer addUser(UserVo userVo) { return (Integer)getSqlMapClientTemplate().insert("addUser", userVo); } public Integer deleteUser(Integer id) { return getSqlMapClientTemplate().delete("deleteUser", id); } @SuppressWarnings("unchecked") public List<UserVo> getAll() { return getSqlMapClientTemplate().queryForList("getAll"); } public UserVo getUserById(Integer id) { return (UserVo)getSqlMapClientTemplate().queryForObject("getUserById", id); } public List<UserVo> getUserByProperty(String property) { return null; } public Integer updateUser(UserVo userVo) { return getSqlMapClientTemplate().update("updateUser", userVo); } }