This article describes how to build the development environment of struts2 + spring3 + mybatis3.Struts and spring are not described here. Mybatis is an excellent persistent layer framework that supports common SQL queries, stored procedures, and advanced mappings. Mybatis eliminates the manual settings of almost all JDBC code and parameters and retrieves the result set. Mybatis uses simple XML or annotations for configuration and original ing. It maps interfaces and Java pojos (Plan old Java objects, common Java objects) into records in the database. Struts-2.3.14Spring-3.2.1MyBatis-3.2.2 Environment
Tested in Apache-Tomcat-7.0.39
Project directory structure
Dependent packages in the Lib Folder:
Portal web. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: Web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "webapp_id" version = "3.0"> <display-Name> test_ssm </display-Name> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome -File-List> <! -- Configure spring resources --> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value> classpath: config/applicationcontext -*. XML </param-value> </context-param> <! -- Configure spring --> <listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class> </listener> <! -- Configure struts2 --> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter </filter-class> <init-param> <param-Name> config </param-Name> <param-value> struts-default.xml, struts-plugin.xml,/config/struts. XML </param-value> </init-param> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/ * </url-pattern> </filter-mapping> </Web-app>
Spring profiles applicationContext-common.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: context = "http://www.springframework.org/schema/context" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3. 2. xsdhttp: // www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! -- Enable spring annotation support --> <context: annotation-config/> <! -- Configure the datasource data source --> <bean id = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource "> <property name =" driverclassname "value =" com. mySQL. JDBC. driver "/> <property name =" url "value =" JDBC: mysql: // localhost: 3306/mybatis "/> <property name =" username "value =" root "/> <property name =" password "value =" 123456 "/> </bean> <! -- Create sqlsessionfactory --> <bean id = "sqlsessionfactory" class = "org. mybatis. spring. sqlsessionfactorybean "> <property name =" configlocation "value =" classpath: config/mabatis-config.xml "/> <property name =" datasource "ref =" datasource "/> <property name =" mapperlocations "value =" classpath: COM/Jialin/mapper /**/*. XML "/> </bean> <! -- Configure the Transaction Manager. Note that the datasource here must be consistent with the datasource of sqlsessionfactorybean, otherwise the transaction will not work --> <bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. datasourcetransactionmanager "> <property name =" datasource "ref =" datasource "/> </bean> <! -- Configure the propagation feature of the transaction --> <bean id = "basetransactionproxy" class = "org. springframework. transaction. interceptor. transactionproxyfactorybean "abstract =" true "> <property name =" transactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <props> <prop key =" add * "> propagation_required </prop> <prop key =" Edit * "> propagation_required </prop> <prop key =" Remove * "> propagation_required </prop> <prop key =" insert * "> propagation_required </prop> <prop key =" Update * "> propagation_required </prop> <prop key =" del * "> propagation_required </prop> <prop key = "*"> readonly </prop> </props> </property> </bean> </beans>
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: AOP = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3. 2. xsdhttp: // www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <bean id =" userinfodao "class =" org. mybatis. Spring. Mapper. mapperfactorybean "SCO Pe = "prototype"> <property name = "mapperinterface" value = "com. jialin. dao. userinfodao "/> <property name =" sqlsessionfactory "ref =" sqlsessionfactory "/> </bean> <bean id =" userinfoservice "class =" com. jialin. service. userinfoservice "Scope =" prototype "> <property name =" userinfodao "ref =" userinfodao "/> </bean> <! -- Configure the transaction interceptor for the iregisterservice interface. The basetransactionproxy is the transaction Interceptor. Obtain this object from the Controller --> <bean id = "iuserinfoservice" parent = "basetransactionproxy"> <! -- Implementation class --> <property name = "target" ref = "userinfoservice"/> </bean> <bean id = "usermanageaction" class = "com. jialin. action. usermanageaction "Scope =" prototype "> <property name =" userinfoservice "ref =" iuserinfoservice "/> </bean> </beans>
Struts configuration file 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> <! -- Assign action creation to spring for Management --> <constant name = "struts. objectfactory" value = "Spring"/> <! -- Change the suffix of the struts2 request action. The default value is action. To remove the suffix, set it to "," --> <constant name = "struts. action. extension "value =", "> </constant> <package name =" abstract_struts "abstract =" true "extends =" struts-Default "namespace ="/"> <! -- Put public items in this abstract package --> </package> <! -- Include the configuration file --> <include file = "/config/struts-user.xml"> </include> </struts>
Struts-user.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> <package name="MyActions" extends="struts-default"> <action name="*_*" class="userManageAction" method="{1}"><result name="success" type="redirect">/{2}.jsp</result></action> </package></struts>
Mybatis profile mabatis-config.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> <typealiases> <typealias alias = "userinfo" type = "com. jialin. entity. userinfo "/> </typealiases> <! -- Because the location of the ing file has been specified in applicationcontext-common, --> <! -- <Mappers> <mapper resource = "com/Jialin/Dao/registermapper. xml"/> </mappers> --> </configuration>
Usermanageaction. Java
package com.jialin.action;import com.jialin.entity.UserInfo;import com.jialin.service.IUserInfoService;public class UserManageAction {private IUserInfoService userInfoService;public IUserInfoService getUserInfoService() {return userInfoService;}public void setUserInfoService(IUserInfoService userInfoService) {this.userInfoService = userInfoService;}private UserInfo userInfo;public UserInfo getUserInfo() {return userInfo;}public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo;} public String insertUser() {userInfoService.insertUser(userInfo);return "success";}public String editUser() {userInfoService.edit(userInfo);return "success";}public String removeUser() {userInfoService.remove(userInfo);return "success";}public String getUserById() {userInfoService.get(userInfo);return "success";}public String getListByName() {userInfoService.getList(userInfo);return "success";}public String getAllUser() {userInfoService.getAllUser();return "success";}}
Iuserinfoservice. Java
package com.jialin.service;import java.util.List;import com.jialin.entity.UserInfo;public interface IUserInfoService {public void insertUser(UserInfo user);public void edit(UserInfo user);public void remove(UserInfo user);public UserInfo get(UserInfo user);public List getList(UserInfo user);public List getAllUser();}
Userinfoservice. Java
package com.jialin.service;import java.util.List;import java.util.Iterator;import com.jialin.dao.UserInfoDao;import com.jialin.entity.UserInfo;public class UserInfoService implements IUserInfoService {private UserInfoDao userInfoDao;public UserInfoDao getUserInfoDao() {return userInfoDao;}public void setUserInfoDao(UserInfoDao userInfoDao) {this.userInfoDao = userInfoDao;}@Overridepublic void insertUser(UserInfo user){userInfoDao.insertUser(user);}@Overridepublic void edit(UserInfo user) {userInfoDao.edit(user);}@Overridepublic void remove(UserInfo user) {userInfoDao.remove(user);}@Overridepublic UserInfo get(UserInfo user) {UserInfo user1=userInfoDao.get(user);System.out.println(user1.getUsername());return user1;}@Overridepublic List getList(UserInfo user) {List list=userInfoDao.getList(user);for(Iterator iter=list.iterator();iter.hasNext();){UserInfo user1=(UserInfo)iter.next();System.out.println(user1.getUsername()+",");}return list;}@Overridepublic List getAllUser() {List list= userInfoDao.getAllUser();for(Iterator iter=list.iterator();iter.hasNext();){UserInfo user=(UserInfo)iter.next();System.out.println(user.getUsername()+",");}return list;}}
Userinfodao. Java
package com.jialin.dao;import java.util.List;import com.jialin.entity.UserInfo;public interface UserInfoDao {public void insertUser(UserInfo user);public void edit(UserInfo user);public void remove(UserInfo user);public UserInfo get(UserInfo user);public List getList(UserInfo user);public List getAllUser();}
Userinfo. Java
package com.jialin.entity;public class UserInfo {private int id;private String username;private String password;private String ismanager;public void setIsmanager(String ismanager) {this.ismanager = ismanager;}public String getIsmanager() {return ismanager;}public void setUsername(String username) {this.username = username;}public String getUsername() {return username;}public void setPassword(String password) {this.password = password;}public String getPassword() {return password;}public void setId(int id) {this.id = id;}public int getId() {return id;}}
Myecliptis ing file userinfo-mapper.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. Jialin. Dao. userinfodao "> <! -- Automatic Generation of ID policy --> <insert id = "insertuser" parametertype = "userinfo" usegeneratedkeys = "true" keyproperty = "ID"> insert into userinfo (username, password, ismanager) values (# {username}, # {password}, # {ismanager}) </Insert> <! -- Userinforesultmap is the resultmap defined in the userinfo-resultmap.xml --> <select id = "getlist" parametertype = "userinfo" resulttype = "list" resultmap = "userinforesultmap"> select * From userinfo where username like '%' # {username} '%' </SELECT> <select id = "getalluser" resulttype = "list" resultmap = "userinforesultmap"> select * From userinfo </SELECT> <select id = "get" parametertype = "userinfo" resulttype = "com. jialin. entity. Userinfo "resultmap =" userinforesultmap "> <! [CDATA [select * From userinfo where id = # {ID}]> </SELECT> <update id = "edit" parametertype = "userinfo"> Update userinfo setusername = #{ username }, password = # {password} Where id = # {ID} </update> <Delete id = "Remove" parametertype = "userinfo"> Delete from userinfo where id = # {ID} </delete> </mapper>
Userinfo-resultMap.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.jialin.dao.UserInfoDao"><resultMap type="com.jialin.entity.UserInfo" id="userInfoResultMap"><id property="id" column="id"/><result property="username" column="username"/><result property="password" column="password"/><result property="ismanager" column="ismanager"/></resultMap></mapper>
Test JSP
<% @ Page contenttype = "text/html; charset = UTF-8 "%> <HTML>
Code pasting is also a kind of physical activity. If it is unnecessary, descriptions of several core configuration files have been written into comments. Next I will share an article to discuss the differences between Hibernate and mybatis, and some articles on these frameworks. Thank you for your attention!