struts2+ibatis+spring框架整合

lspengjiang 2011-09-05

近期公司要开发新的项目,要用struts2+ibatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!下

边我把我的myschool开发的源代码以及数据库贴出来!


开发环境 myeclips+tomcate+sql server

开发技术 struts2+ibatis+spring jsp+java

一.创建web项目导入必需的jar文件

                


二.创建并编写配置文件,配置文件比较多。可能有点繁琐。

  1.创建并编写ApplicationContext.xml     

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.        
  7.     <!--配置数据源属性文件  -->  
  8.     <bean id="propertyConfigurer"  
  9.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  10.         <property name="location">  
  11.             <value>/WEB-INF/configs/sqlServer.properties</value>  
  12.         </property>  
  13.     </bean>  
  14.        
  15.     <!--配置数据源  -->  
  16.     <bean id="dataSource"  
  17.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  18.         <property name="driverClassName">  
  19.             <value>${jdbc.driver}</value>  
  20.         </property>  
  21.         <property name="url">  
  22.             <value>${jdbc.url}</value>  
  23.         </property>  
  24.         <property name="username">  
  25.             <value>${jdbc.user}</value>  
  26.         </property>  
  27.         <property name="password">  
  28.             <value>${jdbc.pwd}</value>  
  29.         </property>  
  30.     </bean>  
  31.        
  32.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">     
  33.         <property name="configLocation" value="classpath:com/test/sqlMapper/mybatis_config.xml" />     
  34.         <property name="dataSource" ref="dataSource" />     
  35.     </bean>    
  36.        
  37.        
  38.     <bean id="loginDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  39.         <property name="mapperInterface" value="com.test.dao.ILoginDao"/>  
  40.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  41.     </bean>  
  42.   
  43.     <bean id="loginAction" class="com.test.action.LoginAction">  
  44.         <property name="loginDao" ref="loginDao"></property>  
  45.     </bean>  
  46. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<!--配置数据源属性文件  -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>/WEB-INF/configs/sqlServer.properties</value>
		</property>
	</bean>
	
	<!--配置数据源  -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>${jdbc.driver}</value>
		</property>
		<property name="url">
			<value>${jdbc.url}</value>
		</property>
		<property name="username">
			<value>${jdbc.user}</value>
		</property>
		<property name="password">
			<value>${jdbc.pwd}</value>
		</property>
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
	    <property name="configLocation" value="classpath:com/test/sqlMapper/mybatis_config.xml" />  
	    <property name="dataSource" ref="dataSource" />  
	</bean> 
	
    
    <bean id="loginDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.test.dao.ILoginDao"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>

	<bean id="loginAction" class="com.test.action.LoginAction">
		<property name="loginDao" ref="loginDao"></property>
	</bean>
</beans>

 

  2.配置数据源当然要有个数据源 属性文件了sqlServer.properties     

Xml代码 复制代码 收藏代码
  1. jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=login  
  2. jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver   
  3. jdbc.user=sa  
  4. jdbc.pwd=  
jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=login
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.user=sa
jdbc.pwd=

 

      我的数据库密码为空pwd当然也要空了。

  3.mybatis的配置文件mybatis_config.xml 

 

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">    
  3. <configuration>  
  4.     <typeAliases>  
  5.         <typeAlias alias="userinfo" type="com.test.entity.UserInfo"/>  
  6.     </typeAliases>  
  7.     <mappers>  
  8.         <mapper resource="com/test/sqlMapper/loginMapper.xml"/>  
  9.     </mappers>  
  10. </configuration>  
<?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.test.entity.UserInfo"/>
	</typeAliases>
	<mappers>
		<mapper resource="com/test/sqlMapper/loginMapper.xml"/>
	</mappers>
</configuration>

 
  4.在mybatis_config.xml中的中有个映射文件loginMapper.xml      

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">    
  3. <mapper namespace="com.test.dao.ILoginDao">  
  4.        
  5.     <resultMap type="userinfo" id="userMap">  
  6.         <id property="id" column="id"/>  
  7.         <result property="username" column="username"/>  
  8.         <result property="password" column="password"/>      
  9.     </resultMap>  
  10.        
  11.     <select id="getUser" parameterType="String" resultMap="userMap">  
  12.         select * from userinfo where username=#{userName}   
  13.     </select>  
  14. </mapper>  
<?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.test.dao.ILoginDao">
	
	<resultMap type="userinfo" id="userMap">
		<id property="id" column="id"/>
		<result property="username" column="username"/>
		<result property="password" column="password"/>	
	</resultMap>
	
	<select id="getUser" parameterType="String" resultMap="userMap">
		select * from userinfo where username=#{userName}
	</select>
</mapper>

 

    5.接下来就是web.xml      

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   
  8. <context-param>  
  9.     <param-name>contextConfigLocation</param-name>  
  10.     <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  
  11. </context-param>  
  12.   
  13. <listener>  
  14.         <listener-class>  
  15.             org.springframework.web.context.ContextLoaderListener   
  16.         </listener-class>  
  17. </listener>  
  18.   
  19.   <filter>     
  20.     <filter-name>struts2</filter-name>     
  21.     <filter-class>     
  22.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter      
  23.     </filter-class>     
  24.     
  25.   </filter>     
  26.   
  27.   <filter-mapping>     
  28.     <filter-name>struts2</filter-name>     
  29.     <url-pattern>/*</url-pattern>     
  30.   </filter-mapping>     
  31.   
  32. </web-app>  
<?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">

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/classes/applicationContext.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>/*</url-pattern>  
  </filter-mapping>  

</web-app>

 

好了所有的配置文件都已经配置好了
三.该写JAVA文件了 接口,实体辅助类,还有action
  
     1.写接口 ILoginDao.java 

Java代码 复制代码 收藏代码
  1. package com.test.dao;   
  2.   
  3. import java.util.List;   
  4.   
  5. public interface ILoginDao {   
  6.     public List getUser(String userName);   
  7. }  
package com.test.dao;

import java.util.List;

public interface ILoginDao {
	public List getUser(String userName);
}

 
     2.实体辅助类UserInfo.java 

Java代码 复制代码 收藏代码
  1. package com.test.entity;   
  2.   
  3. public class UserInfo {   
  4.        
  5.     private int id;   
  6.     private String username;   
  7.     private String password;   
  8.     public String getUsername() {   
  9.         return username;   
  10.     }   
  11.     public void setUsername(String username) {   
  12.         this.username = username;   
  13.     }   
  14.     public String getPassword() {   
  15.         return password;   
  16.     }   
  17.     public void setPassword(String password) {   
  18.         this.password = password;   
  19.     }   
  20.     public int getId() {   
  21.         return id;   
  22.     }   
  23.     public void setId(int id) {   
  24.         this.id = id;   
  25.     }   
  26. }  
package com.test.entity;

public class UserInfo {
	
	private int id;
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}

 


   3.action 类 LoginAction.java

Java代码 复制代码 收藏代码
  1. package com.test.action;   
  2.   
  3. import java.util.List;   
  4.   
  5. import com.test.dao.ILoginDao;   
  6.   
  7. public class LoginAction {   
  8.   
  9.        
  10.         private ILoginDao loginDao;   
  11.         private String username;   
  12.         private String password;   
  13.            
  14.   
  15.         public String getUsername() {   
  16.             return username;   
  17.         }   
  18.         public void setUsername(String username) {   
  19.             this.username = username;   
  20.         }   
  21.         public String getPassword() {   
  22.             return password;   
  23.         }   
  24.         public void setPassword(String password) {   
  25.             this.password = password;   
  26.         }   
  27.            
  28.         public ILoginDao getLoginDao() {   
  29.             return loginDao;   
  30.         }   
  31.         public void setLoginDao(ILoginDao loginDao) {   
  32.             this.loginDao = loginDao;   
  33.         }   
  34.            
  35.         public String execute(){   
  36.             String userName = getUsername();   
  37.             String password = getPassword();   
  38.             System.out.println("userName:"+userName+"\n"+"password:"+password);   
  39.             List list = loginDao.getUser(userName);   
  40.             if(list.size()>0){   
  41.                 return "success";   
  42.             }else{   
  43.                 return "error";   
  44.             }   
  45.                
  46.         }   
  47.            
  48. }  
package com.test.action;

import java.util.List;

import com.test.dao.ILoginDao;

public class LoginAction {

	
		private ILoginDao loginDao;
		private String username;
		private String password;
		

		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		
		public ILoginDao getLoginDao() {
			return loginDao;
		}
		public void setLoginDao(ILoginDao loginDao) {
			this.loginDao = loginDao;
		}
		
		public String execute(){
			String userName = getUsername();
			String password = getPassword();
			System.out.println("userName:"+userName+"\n"+"password:"+password);
			List list = loginDao.getUser(userName);
			if(list.size()>0){
				return "success";
			}else{
				return "error";
			}
			
		}
		
}

 
四.该写页面了 页面有三个如下
     1. login.jsp   

Struts2标签代码 复制代码 收藏代码
  1. <body>    
  2.     <s:form action="login" method="post">    
  3.     <s:textfield name="username" label="用户名:"></s:textfield>    
  4.     <s:textfield name="password" label="密码:"></s:textfield>    
  5.     <s:submit value="提交"></s:submit>    
  6.     </s:form>    
  7.   </body>   
<body> 
    <s:form action="login" method="post"> 
    <s:textfield name="username" label="用户名:"></s:textfield> 
    <s:textfield name="password" label="密码:"></s:textfield> 
    <s:submit value="提交"></s:submit> 
    </s:form> 
  </body> 

 
     2.success.jsp 就几个字  登陆成功
      3.error,jsp      就几个字 登录失败
五.创建数据库 login 创建表 userinfo  里面就三个字段 id  username password  数据库我已经传到这文章里边了,下载后可以直接用

六.好了快启动tomcat看看成功了

afro 2011-09-21
你都抽象出来了一个user类,为什么登陆action还要get,set username和password属性呢???直接 get set user 不就行了吗?有点多此一举的感觉。。。
李学力 2011-09-27
写得太简单了吧,至少自己封装点东西才好用啊,不如关联对象的保存....
lzeus 2011-11-02
spring用的2。5还是3?
可以支持myibatis3了?
我一直用的ibatis2.3
Global site tag (gtag.js) - Google Analytics