spring hibernate struts 整合ssh demo

entity包

public class dept {
private int id;
private String name;
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;
}

}
dept.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.entity"> <class name="dept" table="t_dept"> <id name="id" column="deptid"> <generator class="native"></generator> </id> <property name="name" column="deptname"></property> </class> </hibernate-mapping>
public class employee {
private int id;
private String empname;
private double salary;
private dept dept;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getEmpname() {
    return empname;
}
public void setEmpname(String empname) {
    this.empname = empname;
}
public double getSalary() {
    return salary;
}
public void setSalary(double salary) {
    this.salary = salary;
}
public dept getDept() {
    return dept;
}
public void setDept(dept dept) {
    this.dept = dept;
}

}
employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.entity"> <class name="employee" table="t_employ">


//该处 table表写错,IDE检测不出来错误 也不会报错;只是页面显示不出来数据


<id name="id" column="empid"> <generator class="native"></generator> </id> <property name="empname" ></property> <property name="salary"></property> <many-to-one name="dept" column="dept_id" class="dept"></many-to-one> </class> </hibernate-mapping>

bean-base.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">
  

<!-- 所有配置的公共部门 -->

<!-- 1) 连接池实例 -->

        
 <bean id="datasource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" >
       <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="6"></property>
        
 </bean>

<!-- 2) SessionFactory实例创建 -->
<!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- a. 连接池 -->
        <property name="dataSource" ref="datasource"></property>
        
        <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        <!-- c. 映射配置 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:cn/itcast/entity/*.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <!-- 3) 事务配置 -->
    <!-- # 事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- # 事务增强 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <!-- # AOP配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
 </beans> 

dao层

import java.io.Serializable;

import org.hibernate.SessionFactory;

import cn.itcast.entity.employee;

public class employeedao {


private SessionFactory sessionFactory;  //该处 类型名称大写注意

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

public employee findbyid(Serializable id){
return (employee)sessionFactory.getCurrentSession().get(employee.class,id);
}
}

bean-dao.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeeDao" class="cn.itcast.dao.EmployeeDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>

service层

import java.io.Serializable;

import cn.itcast.dao.*;
import cn.itcast.entity.employee;
public class employeeservice {
private employeedao employeedao;

public void setEmployeedao(employeedao employeedao) {
    this.employeedao = employeedao;
}
public  employee findbyid(Serializable id){
    employee emp=employeedao.findbyid(id);
    return emp;
}
}




bean-service.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeeservice" class="cn.itcast.service.employeeservice"> <property name="employeedao" ref="employeedao"></property> </bean> </beans>

action 层

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.itcast.entity.employee;
import cn.itcast.service.*;
public class employeeaction extends ActionSupport{
private employeeservice employeeservice;

public void setEmployeeservice(employeeservice employeeservice) {
    this.employeeservice = employeeservice;
}

@Override
public String execute() throws Exception {
    // TODO Auto-generated method stub
    
    int empid=1;
    employee emp= employeeservice.findbyid(empid);
    Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
    request.put("emp", emp);
    
    return SUCCESS;
}

}

bean-action.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="employeeaction" class="cn.itcast.action.employeeaction" scope="prototype">
        <property name="employeeservice" ref="employeeservice"></property>
    </bean>
    
</beans>  

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="emp"  extends="struts-default">
<action name="show"  class="employeeaction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>    

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">
     <filter>
        <filter-name>OpenSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInView</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    
    
    <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>
 
 
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

  <body>
       员工: ${emp.empname }
      部门: ${emp.dept.name }
  </body>

#########################################################################

注意事项:

bean-base.xml

<bean id="datasource"         //该处变量名称  可以随意取   ,但是后边需要引用该变量  就需要按这里的ID                                      
class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"></property> </bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

//该处id需要取 sessionFactory 必须写为sessionFactory
<!-- a. 连接池 --> <property name="dataSource" ref="datasource"></property>

//该处 dataSource 属性名称必须要这样写 大小写要严格注意 <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- c. 映射配置 --> <property name="mappingLocations"> <list> <value>classpath:cn/itcast/entity/*.hbm.xml</value> </list> </property> </bean> <!-- 3) 事务配置 --> <!-- # 事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property>


//<property name="sessionFactory" ref="sessionFactory"> 必须要这样写


</bean> <!-- # 事务增强 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- # AOP配置 --> <aop:config> <aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
public class employeedao {
private SessionFactory  sessionfactory;                         //注意该处  属性类型大小写 


public void setSessionfactory(SessionFactory sessionfactory) {
    this.sessionfactory = sessionfactory;
}


public employee findbyid(Serializable id){
    return (employee)sessionfactory.getCurrentSession().get(employee.class,id);
}
}
bean-dao   这里的  sessionFactory   要跟 bean-base.xml  sessionFactory   一致

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeedao" class="cn.itcast.dao.employeedao"> <property name="sessionfactory" ref="sessionFactory"></property> </bean> </beans>
原文地址:https://www.cnblogs.com/yimian/p/7991616.html