本文使用的是Struts2+Hibernate3+Spring3框架整合开发的用户管理系统实例。
1.1数据库层实现
建数据库,名为surra;
建数据表,名为user。其中id设置为自增。
1.2Hibernate持久层设计
1.2.1 创建持久化类:User.java
package com.integration.entity;1.2.2 创建映射文件:User.hbm.xml
public class User {
private int id;
private String name;
private String password;
private String type;
public User() {
}
public User(int id, String name, String password, String type) {
this.id = id;
this.name = name;
this.password = password;
this.type = type;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.integration.entity.User" table="USER">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="22" scale="0" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="100" not-null="false">
<comment>用户名</comment>
</column>
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="100" not-null="false">
<comment>密码</comment>
</column>
</property>
<property name="type" type="java.lang.String">
<column name="TYPE" length="500" not-null="false">
<comment>类型</comment>
</column>
</property>
</class>
</hibernate-mapping>
1.3 DAO层实现
1.3.1Spring管理SessionFactory:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>1.3.2 创建DAO接口:UserDAO.java
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" >
<ref local="dataSource"/>
</property>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 指定HIbernate映射文件的路径 -->
<property name="mappingResources">
<list>
<value>com/integration/entity/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/surra
</value>
</property>
<property name="username">
<value>admin</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
</beans>
package com.integration.dao;
import java.util.List;
import com.integration.entity.User;
public interface UserDAO {
void save(User user);
User getUser(String name);
void delete(int id);
void update(User user);
User findById(int id);
List<User> findAll();
}
1.3.3 创建DAO接口实现类:UserDAOImpl.java
package com.integration.dao;
import java.util.List;
import com.integration.entity.User;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
public void save(User user) {
this.getHibernateTemplate().save(user);
}
public User getUser(String name)
{
String hsql="from User u where u.name='"+name+"'";
User result=(User)((Query) this.getHibernateTemplate().find(hsql)).uniqueResult();
return result;
}
public void delete(int id) {
this.getHibernateTemplate().delete(findById(id));
}
public void update(User user){
this.getHibernateTemplate().update(user);
}
public User findById(int id) {
User user = (User) this.getHibernateTemplate().get(User.class, id);
return user;
}
@SuppressWarnings("unchecked")
public List<User> findAll() {
String queryString = "from User";
List<User> list =this.getHibernateTemplate().find(queryString);
return list;
}
}
在applicationContext.java中添加如下内容:
<span style="white-space:pre"> </span><bean id="userDAO"
class="com.integration.dao.UserDAOImpl"
abstract="false" lazy-init="default" autowire="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
1.4 业务逻辑层实现
1.4.1 创建业务逻辑组件接口:UserService.java
package com.integration.service;
import java.util.List;
import com.integration.entity.User;
public interface UserService {
void saveUser(User user);
User getUser(String name);
void deleteUser(int id);
void updateUser(User user);
User findUserById(int id);
List<User> findAll();
}
1.4.2 创建业务逻辑组件实现类:UserServiceImpl.java
package com.integration.service;
import java.util.List;
import com.integration.dao.UserDAO;
import com.integration.entity.User;
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO){
this.userDAO=userDAO;
}
public void saveUser(User user) {
if(userDAO.findById(user.getId())==null)
userDAO.save(user);
}
public User getUser(String name) {
return userDAO.getUser(name);
}
public void deleteUser(int id) {
if(userDAO.findById(id)!=null)
userDAO.delete(id);
}
public void updateUser(User user) {
if(userDAO.findById(user.getId())!=null)
userDAO.update(user);
}
public User findUserById(int id) {
return userDAO.findById(id);
}
public List<User> findAll() {
return userDAO.findAll();
}
}
在applicationContext.java中进行配置,如下所示:
<span style="white-space:pre"> </span><bean id="userService" class="com.integration.service.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
1.5 完成用户登录设计
1.5.1 整合Struts2+Spring:先添加相应的jar包,然后修改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">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation </param-name>
<param-value>/WEB-INF/classes/applicationContext.xml </param-value>
</context-param>
</web-app>
1.5.2 创建用户登录Action:LoginAction.java
package com.integration.action;
import java.util.Iterator;
import java.util.List;
import com.integration.service.UserService;
import com.integration.entity.User;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
String username;
String password;
String usertype;
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
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;
}
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute(){
List<User> list = (List<User>) userService.findAll();
User u = new User();
Iterator<User> it=list.iterator();
while(it.hasNext()){
u = (User)it.next();
if(username.trim().equals(u.getName())&&password.trim().equals(u.getPassword())&&usertype.trim().equals(u.getType()))
return "success";
else return "failer";
}
String page = "failer";
return page;
}
}
1.5.3 用户登录页面:Login.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>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:textfield name="usertype" label="类型"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
1.5.4 配置控制器:applicationContext.xml中添加如下内容:
<span style="white-space:pre"> </span><bean id="loginAction" class="com.integration.action.LoginAction">
<property name="userService" ref="userService"></property>
</bean>
1.5.5 在struts.xml文件中配置loginAction,并定义处理结果与视图资源的关系:struts.xml
<span style="white-space:pre"> </span><?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.objectFactory" value="spring" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<span style="white-space:pre"> </span><constant name="struts.url.includeParams" value="none" />
<span style="white-space:pre"> </span><constant name="struts.ui.theme" value="xhtml"></constant>
<span style="white-space:pre"> </span><package name="struts2" extends="struts-default">
<span style="white-space:pre"> </span><action name="login" class="loginAction">
<span style="white-space:pre"> </span><result name="success">/success.jsp</result>
<span style="white-space:pre"> </span><result name="failer">/error.jsp</result>
<span style="white-space:pre"> </span></action>
<span style="white-space:pre"> </span></package>
</struts>
以上的代码可以测试用户登录,由于时间限制,剩余的查询用户、添加用户、删除用户、更新用户信息的代码同理可得。