spring,structs2,hibernate 整合 xml版

来源:互联网 发布:广电网络 待遇 编辑:程序博客网 时间:2024/06/06 08:40

action :

package cn.gh.action;import cn.gh.bean.Dept;import cn.gh.service.IDeptService;import com.opensymphony.xwork2.ActionSupport;import javax.annotation.Resource;/** * Created by guo on 2017/10/29. */public class DeptAction extends ActionSupport {    private IDeptService deptService;    private Dept dept;    public String addDept(){        deptService.addDept(dept);        return SUCCESS;    }    public Dept getDept() {        return dept;    }    public void setDept(Dept dept) {        this.dept = dept;    }    public IDeptService getDeptService() {        return deptService;    }    public void setDeptService(IDeptService deptService) {        this.deptService = deptService;    }}

bean :
 
package cn.gh.bean;/** * Created by guo on 2017/10/29. */public class Dept {    private int deptno;    private String deptname;    public int getDeptno() {        return deptno;    }    public void setDeptno(int deptno) {        this.deptno = deptno;    }    public String getDeptname() {        return deptname;    }    public void setDeptname(String deptname) {        this.deptname = deptname;    }}
  小配置 :
 
<?xml version="1.0"?><!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.gh.bean">    <class name="Dept" table="Dept" schema="guohua">        <id name="deptno" column="deptno">            <generator class="native"></generator>        </id>        <property name="deptname" />    </class></hibernate-mapping>
 
  dao   :

   
package cn.gh.dao;import cn.gh.bean.Dept;/** * Created by guo on 2017/10/29. */public interface IDeptDAO {    public void addDept(Dept dept);}

daoimpl  :
  
package cn.gh.dao;import cn.gh.bean.Dept;import org.hibernate.SessionFactory;import org.springframework.stereotype.Repository;/** * Created by guo on 2017/10/29. */public class DeptDAOImpl  implements IDeptDAO{    private SessionFactory sessionFactory;    public void addDept(Dept dept) {        sessionFactory.getCurrentSession().save(dept);    }    public SessionFactory getSessionFactory() {        return sessionFactory;    }    public void setSessionFactory(SessionFactory sessionFactory) {        this.sessionFactory = sessionFactory;    }}
 
service  :
package cn.gh.service;import cn.gh.bean.Dept;/** * Created by guo on 2017/10/29. */public interface IDeptService {    public void addDept(Dept dept);}

 serviceimpl  :
 
package cn.gh.service;import cn.gh.bean.Dept;import cn.gh.dao.IDeptDAO;import org.springframework.stereotype.Service;import javax.annotation.Resource;/** * Created by guo on 2017/10/29. */public class DeptServiceImpl implements IDeptService {    private IDeptDAO deptDAO;    public void addDept(Dept dept) {         deptDAO.addDept(dept);    }    public IDeptDAO getDeptDAO() {        return deptDAO;    }    public void setDeptDAO(IDeptDAO deptDAO) {        this.deptDAO = deptDAO;    }}
  
  applicationContext.xml :
  
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       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.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.配置数据源c3p0-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="${jdbc.driverClassName}"/>        <property name="user" value="${jdbc.username}"/>        <property name="password" value=""/>        <property name="jdbcUrl" value="${jdbc.url}"/>    </bean>    <!--jdbc.properties-->    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>    <!--2.SessionFactory         类:Local-->    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="hibernateProperties">            <props>                <!--hibernate.xxxxxx必须以hibernate-->                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="hibernate.hbm2ddl.auto">create</prop>                <!--with current thread bind session和线程绑定的session-->                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>            </props>        </property>        <!--扫描小配置文件 所有的hbm文件-->        <property name="mappingDirectoryLocations" value="classpath:cn/gh/bean"></property>    </bean>    <!--3.dao-->    <bean id="deptDAO" class="cn.gh.dao.DeptDAOImpl">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--4.service-->    <bean id="deptService" class="cn.gh.service.DeptServiceImpl">        <property name="deptDAO" ref="deptDAO"></property>    </bean>    <!--要用Spring去创建Action对象  Struts2的Action是多例的-->    <bean id="deptAction" class="cn.gh.action.DeptAction" scope="prototype">        <property name="deptService" ref="deptService"></property>    </bean>    <!--  6.事务管理器-->    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--7.事务-->    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>
    
jdbc.properties  :
   
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///test?characterEncoding=utf8jdbc.username=root

structs.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>    <constant name="struts.devMode" value="true"></constant>    <!--将对象工厂的生成权设置成spring-->    <!-- <constant name="struts.objectFactory" value="spring"></constant>-->    <package name="default" namespace="/" extends="struts-default">        <action name="addDept" class="deptAction" method="addDept">            <result>/jsp/index.jsp</result>        </action>    </package></struts>


 web.xml   :

   
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Archetype Created Web Application</display-name>  <!--有点东西-->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <filter>    <filter-name>struts</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>struts</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <!--1.监听器-->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener></web-app>


log4j.properties :
nnnnlog4j.rootLogger=info,Console,Rlog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayout#log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c - %m%nlog4j.appender.Console.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%nlog4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=${catalina.home}/logs/tomcat.loglog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss} %5p %c{1}(%L):? %m%nlog4j.logger.org.apache=info,Rlog4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, Rlog4j.logger.org.apache.catalina.core=info,Rlog4j.logger.org.apache.catalina.session=info,R  


 测试页面     add.jsp  :
  
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>添加部门</title></head><body>   <form method="post" action="/addDept">      部门名称: <input name="dept.deptname"/>       <input type="submit" value="添加"/>   </form></body></html>




 


原创粉丝点击