SSH架构的一个小Demo

最近,几天在初步学习SSH框架,Struts、Hibernate都算比较容易上手的,Spring与Struts、Hibernate的结合,倒是让我颇为费神。

下面将对该demo进行简单介绍:

1、首先建立User实体类

package com.sshdemo.entity;


import org.hibernate.annotations.Entity;


public class User {
private String userid;
private String name;
private String password;


@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}


@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}


public String getUserid() {
return userid;
}


public void setUserid(String userid) {
this.userid = userid;
}


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;
}


}

2、建立dao层

package com.sshdemo.dao;


import com.sshdemo.entity.User;


public interface UserDao {
public User getUser(String userid);
}

package com.sshdemo.dao;




import java.util.ArrayList;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.sshdemo.entity.User;
import com.sshdemo.util.HibernateUtil;


public class UserDaoImpl implements UserDao {


private  HibernateUtil util;
public User getUser(String userid) {
// util.getSessionHibernate().beginTransaction();
//applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//util=(HibernateUtil) applicationContext.getBean("util");
System.out.println("userdaoimpl is doing");
System.out.println(util);//util is null
System.out.println(userid);
ArrayList result = (ArrayList) util.getSessionHibernate().createQuery(
"from User where userid = ?").setString(0, userid).list();// hql语句,from后为类名,而不是数据库表名
// util.getSessionHibernate().getTransaction().commit();


User user = (User) result.get(0);


return user;
}


public void setUtil(HibernateUtil util) {
this.util = util;
}

}

3、接下来建立biz层

package com.sshdemo.biz;


import com.sshdemo.entity.*;
public interface UserBiz {
//根据userid 查询,返回User
public User getUser(String userid);
}

package com.sshdemo.biz;


import com.sshdemo.dao.UserDao;
import com.sshdemo.dao.UserDaoImpl;
import com.sshdemo.entity.User;


public class UserBizImpl implements UserBiz {

UserDao userdao ;


public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}


public User getUser(String userid) {
// TODO Auto-generated method stub
return this.userdao.getUser(userid);
}
}

4、建立对应的action文件

package com.sshdemo.action;


import com.opensymphony.xwork2.ActionSupport;
import com.sshdemo.biz.UserBiz;
import com.sshdemo.biz.UserBizImpl;
import com.sshdemo.entity.User;


public class LoginAction extends ActionSupport {


private String message;
private String userid;
private String password;
private UserBizImpl userBiz=null;

public void setUserBiz(UserBizImpl userBiz) {
this.userBiz = userBiz;
}


@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("loginaction doing");


User user = userBiz.getUser(userid);
if (user.getUserid().equals(userid)
&& user.getPassword().equals(password)) {
message = "成功登陆";
} else {
message = "登陆失败";
}
return "success";
}


public String getMessage() {
return message;
}


public void setMessage(String message) {
this.message = message;
}


public String getUserid() {
return userid;
}


public void setUserid(String userid) {
this.userid = userid;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}

}

这样,初步建立了demo的目录结构,如下图


接下来就是配置Struts,首先在web.xml文件中添加

<!-- 添加对struts2的支持 -->
<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>

之后建立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">  
      
<struts>  
    <package name="s2sh" namespace="/" extends="struts-default">  
        <action name="Login" class="loginAction">  
            <result name="success">/login.jsp</result>  
        </action>  
    </package>  
      
</struts>   

然后配置Hibernate文件,先建立可以取得Session的HibernateUtil工具类

package com.sshdemo.util;




import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class HibernateUtil {
private SessionFactory sessionFactory;

public Session getSessionHibernate() {
return  sessionFactory.openSession();
}




public SessionFactory getSessionFactory() {
return sessionFactory;
}


public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}

建立对应User类的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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.sshdemo.entity.User" table="user" schema="logindemo">
        <id name="userid" type="java.lang.String">
            <column name="userid"  length="45" not-null="false" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" not-null="false" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="45" not-null="false" />
        </property>
    </class>
</hibernate-mapping>

最后也是最关键的把Hibernate、Struts与Spring结合

加入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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 定义数据源,指定访问的数据的基本信息及连接池信息 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/logindemo" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="maxActive" value="100" />
</bean>
<!-- 定义SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/sshdemo/entity/User.hbm.xml</value>
</list>
</property>
</bean>

<bean id="util" class="com.sshdemo.util.HibernateUtil"  >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userdao" class="com.sshdemo.dao.UserDaoImpl" >
<property name="util" ref="util"></property>
</bean>
<bean id="userBiz" class="com.sshdemo.biz.UserBizImpl" >
<property name="userdao" ref="userdao"></property>
</bean>

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

在web.xml添加对spring的支持

<!-- 添加对spring的支持 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<filter>
<filter-name>opensession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

在struts.xml中加入spring支持

      <constant name="stuts.objectFactory" value="spring"/>

通过SSH框架的训练,感觉Spring的依赖注入,之间的引用关系

LoginAction中定义UserBizImp,UserBizImp中定义UserDaoImp,UserDaoImp定义HibernateUtil工具类
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值