Struts2+MyBatis3.1.0+Spring3.1.0 整合之道一:数据映射器整合解析

java哥 阅读:238 2021-03-31 22:57:53 评论:0


第一步:导入相应的jar包

 

第二步:Web.xml文件中整合Struts2和Spring3.1.0

<?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></display-name>	 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
    <!--配置spring 监听器  --> 
  <listener> 
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  </listener> 
  <!-- 添加 Tomact 启动配置参数 --> 
  <context-param> 
 		<param-name>contextConfigLocation</param-name> 
 		 <param-value>WEB-INF/applicationContext.xml</param-value> 
  </context-param> 
   
  <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> 


第三步:Spring的配置文件配置:数据源、MyBatis和事务管理

 

<?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:context="http://www.springframework.org/schema/context"  
	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/context 
	 http://www.springframework.org/schema/context/spring-context-2.5.xsd 
	 http://www.springframework.org/schema/tx  
	 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	 http://www.springframework.org/schema/aop  
	 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
	 "	 
	>	 
<!--数据源配置  --> 
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
 		<property name="driverClassName"> 
			<value>com.mysql.jdbc.Driver</value> 		 
 		</property>   
 		<property name="url"> 
 			<value>jdbc:mysql://127.0.0.1:3306/skydrive</value>		 
 		</property> 
 		<property name="username"> 
 				<value>root</value> 
 		</property> 
 		<property name="password"> 
 				<value>123456</value> 
 		</property> 
 		<property name="maxIdle"> 
			<value>40</value> 		 
 		</property> 
 		<property name="maxWait"> 
 			<value>40</value> 
 		</property> 		 
   </bean> 
   <!--SqlSessionFactoryBean 配置  --> 
   <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
   			<property name="configLocation"> 
   				<value>classpath:mybatis.xml</value> 
   			</property> 
   			<property name="dataSource"> 
   				<ref local="dataSource"></ref> 
   			</property> 
   </bean> 
<span style="color:#ff0000;"><!--创建数据映射器,数据映射器必须为接口-->  </span>
  <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
   <property name="mapperInterface" value="com.schoolmanager.dao.UserMapper"></property>   
    <property name="sqlSessionFactory" ref="SqlSessionFactory"></property>   
  </bean>   
  <bean id="userServerImpl" class="com.schoolmanager.serverimpl.UserServerImpl"> 
  <property name="userMapper" ref="userMapper"></property> 
  </bean> 
 
    
   <!--配置事务管理器  --> 
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
   		<property name="dataSource"> 
   		<ref local="dataSource"></ref> 
   		</property> 
   </bean> 
    
     <!-- 使用annotation注解方式配置事务 -->   
    <tx:annotation-driven transaction-manager="transactionManager"/>  
    
     
   
     
  
  
 
</beans>


第四步:Mybatis配置文件

注意:采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。

<?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>  
    <mappers>   
        <mapper resource="com/schoolmanager/dao/UserMapper.xml" />   
    </mappers> 
</configuration>	


第五步:编写实体类------User

package com.schoolmanager.model; 
 
import java.io.Serializable; 
 
public class User implements Serializable { 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
	private String user; 
	private String pass; 
	private String xm; 
	private String email; 
	private String phone; 
	private String qq; 
	//默认构造函数 
	public User(){ 
		 
	} 
	//set和get 
	public String getUser() { 
		return user; 
	} 
	public void setUser(String user) { 
		this.user = user; 
	} 
	public String getPass() { 
		return pass; 
	} 
	public void setPass(String pass) { 
		this.pass = pass; 
	} 
	public String getXm() { 
		return xm; 
	} 
	public void setXm(String xm) { 
		this.xm = xm; 
	} 
	public String getEmail() { 
		return email; 
	} 
	public void setEmail(String email) { 
		this.email = email; 
	} 
	public String getPhone() { 
		return phone; 
	} 
	public void setPhone(String phone) { 
		this.phone = phone; 
	} 
	public String getQq() { 
		return qq; 
	} 
	public void setQq(String qq) { 
		this.qq = qq; 
	} 
	 
 
}


第六步:编写User实体类---数据映射接口(UserMapper)

package com.schoolmanager.dao; 
 
import com.schoolmanager.model.User; 
 
public interface UserMapper { 
	public User selectUser(User user);    
	public void insertUser(User user);    
	public void updateUser(User user);    
	public void deleteUser(String username);  
} 


第七步:配置UserMapper.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.schoolmanager.dao.UserMapper"> 
<select id="selectUser" parameterType="com.schoolmanager.model.User" resultType="com.schoolmanager.model.User"> 
	SELECT * FROM t_user WHERE user=#{user} AND pass=#{pass} 
</select> 
<insert id="insertUser" parameterType="com.schoolmanager.model.User" flushCache="true"> 
   INSERT INTO t_user (user,pass,xm,email,phone,qq) VALUES (#{user},#{pass},#{xm},#{email},#{phone},#{qq}) 
</insert> 
<update id="updateUser" parameterType="com.schoolmanager.model.User"> 
	UPDATE t_user SET pass=#{pass} WHERE user=#{user} 
</update> 
<delete id="deleteUser" parameterType="String"> 
	DELETE FROM t_user WHERE user=#{user} 
</delete> 
</mapper>


第八步:编写UserServer接口和UserServer接口实现(UserServerImpl)

package com.schoolmanager.service; 
 
import com.schoolmanager.model.User; 
 
public interface UserServer { 
	public User selectUser(User user);    
	public void insertUser(User user);    
	public void updateUser(User user);    
	public void deleteUser(String username);  
 
} 


 

package com.schoolmanager.serverimpl; 
 
import com.schoolmanager.dao.UserMapper; 
import com.schoolmanager.model.User; 
import com.schoolmanager.service.UserServer; 
 
public class UserServerImpl implements UserServer { 
	private UserMapper userMapper;  
	 
 
	public UserMapper getUserMapper() { 
		return userMapper; 
	} 
 
	public void setUserMapper(UserMapper userMapper) { 
		this.userMapper = userMapper; 
	} 
 
	public User selectUser(User user) { 
		// TODO Auto-generated method stub 
		return userMapper.selectUser(user); 
	} 
 
	public void insertUser(User user) { 
		// TODO Auto-generated method stub 
		userMapper.insertUser(user); 
	} 
 
	public void updateUser(User user) { 
		// TODO Auto-generated method stub 
		userMapper.updateUser(user); 
	} 
 
	public void deleteUser(String username) { 
		// TODO Auto-generated method stub 
		userMapper.deleteUser(username); 
	} 
 
}


第九步:编写相关页面(add.jsp、update.jsp和delete.jsp)

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
<%@ taglib prefix="s"  uri="/struts-tags"%>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   
       
    <title>添加新用户</title>   
       
    <meta http-equiv="pragma" content="no-cache">   
    <meta http-equiv="cache-control" content="no-cache">   
    <meta http-equiv="expires" content="0">      
   
  </head>   
     
  <body>   
    <center>   
        <h1>添加新用户</h1>   
        <s:form action="user_add" namespace="/user" method="post">   
            <s:textfield label="用户名" name="user.user"></s:textfield>   
            <s:password label="密码" name="user.pass"></s:password>   
            <s:textfield label="姓名" name="user.xm"></s:textfield> 
            <s:textfield label="邮箱" name="user.email"></s:textfield> 
            <s:textfield label="电话" name="user.phone"></s:textfield> 
            <s:textfield label="qq" name="user.qq"></s:textfield> 
            <s:submit value="提交"></s:submit>   
        </s:form>   
    </center>   
  </body>   
</html>   


update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
<%@ taglib prefix="s"  uri="/struts-tags"%>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   
       
    <title>修改用户</title>   
       
    <meta http-equiv="pragma" content="no-cache">   
    <meta http-equiv="cache-control" content="no-cache">   
    <meta http-equiv="expires" content="0">      
   
  </head>   
     
  <body>   
    <center>   
        <h1>修改用户</h1>   
        <s:form action="user_update" namespace="/user" method="post">   
            <s:hidden name="user.xm"></s:hidden>   
            <s:textfield label="用户名" name="user.user"></s:textfield>   
            <s:password label="密码" name="user.pass"></s:password>   
            <s:submit value="提交"></s:submit>   
        </s:form>   
    </center>   
  </body>   
</html>   
 


delete.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
<%@ taglib prefix="s"  uri="/struts-tags"%>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   
       
    <title>添加新用户</title>   
       
    <meta http-equiv="pragma" content="no-cache">   
    <meta http-equiv="cache-control" content="no-cache">   
    <meta http-equiv="expires" content="0">      
   
  </head>   
     
  <body>   
    <center>   
        <h1>添加新用户</h1>   
        <s:form action="user_delete" namespace="/user" method="post">    
         <s:textfield label="用户名" name="user.user"></s:textfield>              
            <s:submit value="提交"></s:submit>   
        </s:form>   
    </center>   
  </body>   
</html>   
 


第十步:编写相关Action和配置Action(UserAction)

package com.schoolmanager.action; 
 
import com.opensymphony.xwork2.ActionSupport; 
import com.schoolmanager.model.User; 
import com.schoolmanager.serverimpl.UserServerImpl; 
 
 
 
public class UserAction extends ActionSupport{ 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
	private User user; 
	private UserServerImpl userServerImpl;  
	 
 
	public User getUser() { 
		return user; 
	} 
 
	public void setUser(User user) { 
		this.user = user; 
	} 
 
	 
 
	public UserServerImpl getUserServerImpl() { 
		return userServerImpl; 
	} 
 
	public void setUserServerImpl(UserServerImpl userServerImpl) { 
		this.userServerImpl = userServerImpl; 
	} 
 
	@Override 
	public String execute() throws Exception { 
		// TODO Auto-generated method stub 
		return null; 
	} 
	 
	public String add() { 		     
		userServerImpl.insertUser(user); 
	        return SUCCESS;   
	    }   
	   
	    public String delete() {   
	    	userServerImpl.deleteUser(user.getUser()); 
	        return SUCCESS;   
	    }   
	   
	    public String update() {   
	    	userServerImpl.updateUser(user); 
	        return SUCCESS;   
	    } 
 
	 
	 
	 
	 
 
} 


Struts2.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>        
	<constant name="struts.i18n.encoding" value="UTF-8"/>   
            <!-- 指定默认编码集 ,作用于HttpServletRequest的setCharacterEncoding()和freemarker,vilocity的输出 -->   
    <constant name="struts.configuration.xmlreload" value="true"/>   
            <!-- 当struts配置文件修改时是否自动加载 -->   
    <constant name="struts.devMode" value="false"/>   
            <!-- 开发模式下打印详细的错误信息 -->   
    <constant name="struts.ui.theme" value="xhtml"/>   
     
     <package name="users" namespace="/user" extends="struts-default">   
        <action name="user_*" class="com.schoolmanager.action.UserAction" method="{1}">   
            <result name="success">/index.jsp</result>              
        </action>   
    </package>   
     
     
       
   
     
 
 
</struts>     


该项目结构图:

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

关注我们