首页 > 代码库 > SSH (Struts2+Spring+Hibernate)框架搭建

SSH (Struts2+Spring+Hibernate)框架搭建

1.相关jar包下载地址:http://download.csdn.net/detail/harderxin/4420066

2.项目结构截图:

技术分享

3.相关配置文件:

(1)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>index.jsp</welcome-file>
</welcome-file-list>
<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 定义Struts 2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

(2)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:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- Spring框架配置文件 -->
<!-- 属性注入配置 -->
<context:annotation-config/>
<!-- 实现数据库配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property>
<property name="username" value="http://www.mamicode.com/root"></property>
<property name="password" value="http://www.mamicode.com/123"></property>
<property name="maxActive" value="http://www.mamicode.com/100"></property>
<property name="maxIdle" value="http://www.mamicode.com/60"></property>
<property name="maxWait" value="http://www.mamicode.com/10000"></property>
<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 开启Spring框架的事务管理 ,开启之后@Transaction就可以用了 -->
<tx:annotation-driven transaction-manager="txManager"/>

<!--scope默认采用的是单例模式,scope="prototype" 可以保证 当有请求的时候都创建一个Action对象,保证Struts的Action线程安全 -->

<!-- 实现用户信息管理需要配置的Bean -->
<bean id="user" class="com.ssh.entity.User">

</bean>
<!-- 数据库交互组件 -->
<bean id="userDao" class="com.ssh.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 业务逻辑组件 -->
<bean id="userService" class="com.ssh.service.impl.UserServiceBean">
<property name="userDao" ref="userDao"/>
</bean>
<!--控制组件-->
<bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean>

</beans>

(3)struts.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- Struts2框架配置文件 -->
<struts>
<!-- 配置struts2可以受理的请求扩展名 -->
<constant name="struts.action.extension" value="http://www.mamicode.com/action,do,"></constant>
<!-- struts2的package对应于项目的模块 -->
<package name="action" extends="struts-default" namespace="/">
<!-- 配置action -->
<!--
SSH项目WEB-INF下面的页面跳转要通过Servlet来实现,这样确实是麻烦了点,
不过安全性就提高上去了,因为放在WEB-INF下面的JSP页面,是不可以直接访问的
-->
<action name="login" class="loginAction" method="canLogin">
<result name="success">/WEB-INF/jsp/suc.jsp</result>
<result name="fail">/WEB-INF/jsp//fail.jsp</result>
</action>
</package>

</struts>

(4)hibernate.cfg.xml文件配置

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<!-- connection基本配置 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 常量配置 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- <property name="hibernate.hbm2ddl.auto">create</property> -->

<!-- 核心配置 -->
<mapping resource="com/ssh/mapper/User.hbm.xml" />


</session-factory>

</hibernate-configuration>

(5)映射关系User.hbm.xml文件配置

<?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.ssh.entity.User" table="user">
<id name="id" column="id">
<!-- 交由数据库处理。如果有设置自增则自增,否则由java代码指定 -->
<generator class="native" />
</id>
<property name="name" >
<column name="name"></column>
</property>
<property name="password" column="password"></property>
</class>

</hibernate-mapping>

4.相关类与接口的代码实现:

技术分享

(1)控制组件 LoginAction.java

package com.ssh.action;

import com.ssh.entity.User;
import com.ssh.service.InterfaceUserService;

/**
* 登录测试
* @author Administrator
*
*/
public class LoginAction {
//用于封装用户请求的参数
private User user;
//处理业务逻辑的组件
private InterfaceUserService userService;
/*
* user 属性相应的setter、getter方法
*/
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

//依赖注入所需要的setter 方法
public void setUserService(InterfaceUserService userService) {
this.userService = userService;
}

/**
* 判断是否可以成功登录
* @return
*/
public String canLogin(){
String result = "fail";
if(userService.canLogin(user)){
result="success";
}
return result;
}
}

(2)接口 InterfaceUserDAO.java

package com.ssh.action;

import com.ssh.entity.User;
import com.ssh.service.InterfaceUserService;

/**
* 登录测试
* @author Administrator
*
*/
public class LoginAction {
//用于封装用户请求的参数
private User user;
//处理业务逻辑的组件
private InterfaceUserService userService;
/*
* user 属性相应的setter、getter方法
*/
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

//依赖注入所需要的setter 方法
public void setUserService(InterfaceUserService userService) {
this.userService = userService;
}

/**
* 判断是否可以成功登录
* @return
*/
public String canLogin(){
String result = "fail";
if(userService.canLogin(user)){
result="success";
}
return result;
}
}

(3)实现类 UserDaoImpl.java

package com.ssh.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh.dao.InterfaceUserDAO;
import com.ssh.entity.User;

/**
* 必须继承HibernateDaoSupport
* @author Administrator
*
*/
public class UserDaoImpl extends HibernateDaoSupport implements InterfaceUserDAO{
/**
* 根据用户名和密码判断是否登录成功
*/
@Override
public boolean canLogin(User user) {
boolean flag = false;
//通过用户名获取数据库的对应记录,若相同则可登录
if(user==null && user.getName()==null){
return false;
}
// System.out.println(user);
List<User> list = getHibernateTemplate()
.find("from User where name=?", user.getName());
if(list.size()!=1){
return false;
}
User real =list .get(0);
// System.out.println(real);
if(user.equals(real)){
flag=true;
}
return flag;
}
}

(4)实体类User

package com.ssh.entity;
/**
* 与数据库user表对应的尸体类
* @author Administrator
*
*/
public class User {
private int id;
private String name;
private String password;

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password="
+ password + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

(5)接口InterfaceUserService.java

package com.ssh.service;

import com.ssh.entity.User;

public interface InterfaceUserService {

//判断是否登录成功
public boolean canLogin(User user);
}

 (6)实现类 UserServiceBean.java

package com.ssh.service.impl;

import com.ssh.dao.InterfaceUserDAO;
import com.ssh.entity.User;
import com.ssh.service.InterfaceUserService;

public class UserServiceBean implements InterfaceUserService {
//定义成员变量UserDao并创建相应的setter方法用于依赖注入
private InterfaceUserDAO userDao;

public void setUserDao(InterfaceUserDAO userDao) {
this.userDao = userDao;
}

/**
* 判断是否可以成功登录
*/
@Override
public boolean canLogin(User user) {
return userDao.canLogin(user);
}

}

++++++++++++++++++++++++++++++++恭喜你,框架搭建的demo已经完成++++++++++++++++++++++++

SSH (Struts2+Spring+Hibernate)框架搭建