搭建SSH框架

SSH架构

  

开发登录功能

  

 添加所需的jar文件

  

  未整合的项目结构

  

package cn.houserent.entity;

public class HouseUser implements java.io.Serializable {
    private static final long serialVersionUID = -4357515988015534310L;

    // Fields
    private Integer id;
    private String password;
    private String telephone;
    private String username;
    private String isadmin;
    private String realname;

    // Constructors
    /** default constructor */
    public HouseUser() {
    }

    /** minimal constructor */
    public HouseUser(Integer id) {
        this.id = id;
    }

    /** full constructor */
    public HouseUser(Integer id, String password, String telephone,
            String username, String isadmin, String realname) {
        this.id = id;
        this.password = password;
        this.telephone = telephone;
        this.username = username;
        this.isadmin = isadmin;
        this.realname = realname;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPassword() {
        return this.password;
    }

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

    public String getTelephone() {
        return this.telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getIsadmin() {
        return this.isadmin;
    }

    public void setIsadmin(String isadmin) {
        this.isadmin = isadmin;
    }

    public String getRealname() {
        return this.realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }
}
HouseUser.java
<?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.houserent.entity.HouseUser" table="HOUSE_USER">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="9" scale="0" />
            <generator class="sequence">
                <param name="sequence">HOUSE_USER_ID</param>
            </generator>
        </id>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="50" />
        </property>
        <property name="telephone" type="java.lang.String">
            <column name="TELEPHONE" length="15" />
        </property>
        <property name="username" type="java.lang.String">
            <column name="USERNAME" length="50" />
        </property>
        <property name="isadmin" type="java.lang.String">
            <column name="ISADMIN" length="5" />
        </property>
        <property name="realname" type="java.lang.String">
            <column name="REALNAME" length="50" />
        </property>
    </class>
</hibernate-mapping>
HouseUser.hbm.xml
package cn.houserent.utils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static Configuration conf;
    private static SessionFactory sessionFactory;

    static {
        try {
            conf = new Configuration().configure();
            sessionFactory = conf.buildSessionFactory();
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new RuntimeException("配置文件初始化错误", e);
        }
    }

    private HibernateUtil() {
    }

    public static Session getSession() {
        return sessionFactory.getCurrentSession();
    }
}
HibernateUtil.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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-3.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
</beans>
applicationContext.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">

<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
        <property name="connection.username">fang</property>
        <property name="connection.password">fang</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="javax.persistence.validation.mode">none</property>
        <property name="current_session_context_class">thread</property>
        <property name="format_sql">true</property>

        <mapping resource="cn/houserent/entity/HouseUser.hbm.xml" />

    </session-factory>

</hibernate-configuration>
hibernate.cfg.xml
#
# Log4J Settings for log4j 1.2.x (via jakarta-commons-logging)
#
# The five logging levels used by Log are (in order):
#
#   1. DEBUG (the least serious)
#   2. INFO
#   3. WARN
#   4. ERROR
#   5. FATAL (the most serious)


# Set root logger level to WARN and append to stdout
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n

# Print only messages of level ERROR or above in the package noModule.
log4j.logger.noModule=FATAL

log4j.logger.com.opensymphony.xwork2=ERROR
log4j.logger.org.apache.struts2=ERROR
log4j.logger.freemarker=ERROR

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
log4j.properties
<?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>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="utf-8" />	
	<constant name="struts.multipart.maxSize" value="5000000"/>
	
	<package name="default" namespace="/" extends="json-default">
	   
	</package>
</struts>
struts.xml
public class testDao {

    @Test
    public void test() {
        Transaction tx = HibernateUtil.getSession().beginTransaction();
        List<HouseUser> users = HibernateUtil.getSession()
                .createQuery("from HouseUser").list();
        for (HouseUser user : users) {
            System.out.println(user.getUsername());
        }
        tx.commit();
    }

}
testDao.java

 在Spring中配置数据源和会话工厂

  

  

  

public class testDao {

    @Test
    public void testSpringSessionFactory() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx
                .getBean("sessionFactory");

        Transaction tx = sessionFactory.openSession().beginTransaction();
        List<HouseUser> users = sessionFactory.openSession()
                .createQuery("from HouseUser").list();
        for (HouseUser user : users) {
            System.out.println(user.getUsername());
        }
        tx.commit();

    }

}
testDao.java

 实现并配置DAO

  

  

  模板与回调机制

  

  

 实现并配置Service

  

 为业务层添加事务管理

  

  

 实现并配置Action

  

  

  

 配置web.xml文件

  

  

使用注解实现SSH集成

  

  

  

  

  

  

原文地址:https://www.cnblogs.com/xhddbky/p/9647979.html