Spring3.0+Struts2.1.8+Hibernate3.5整合

Spring3.0+Struts2.1.8+Hibernate3.5整合

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>Spring Struts Hibernate</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml,classpath*:com/lpw/ssh/**/spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>*.do</url-pattern>
    </filter-mapping>
</web-app>

configuration.properties

## database driver name.
database.driver = com.mysql.jdbc.Driver
## database url address.
database.url = jdbc:mysql://localhost:3306/d_spring_struts_hibernate
## database login username.
database.username = root
## database login password.
database.password = root

 全局spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  <context:property-placeholder location="classpath:configuration.properties"/>
</beans>

 全局struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  <constant name="struts.objectFactory" value="spring"/>
  <constant name="struts.action.extension" value="do"/>
  <constant name="struts.multipart.maxSize" value="1073741824"/>
  <constant name="struts.devMode" value="true"/>
  
  <include file="com/lpw/ssh/item/index/struts.xml"/>
</struts>

 Hibernate spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${database.driver}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:hibernate.xml"/>
  </bean>
</beans>

 hibernate.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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">false</property>

    <mapping class="com.lpw.ssh.item.index.IndexModel"/>
  </session-factory>
</hibernate-configuration>

 create.sql

DROP SCHEMA IF EXISTS d_spring_struts_hibernate;
CREATE SCHEMA d_spring_struts_hibernate;
USE d_spring_struts_hibernate;

DROP TABLE IF EXISTS t_index;
CREATE TABLE t_index
(
  c_id DECIMAL(20, 0) DEFAULT 0,
  c_name VARCHAR(255) DEFAULT '',

  CONSTRAINT pk_index_id PRIMARY KEY(c_id)
);

 IndexModel.java

package com.lpw.ssh.item.index;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * @author lpw
 */
@Entity(name = "com.lpw.ssh.item.index.IndexModel")
@Table(name = "t_index")
public class IndexModel
    implements Serializable
{
    private static final long serialVersionUID = 3768248564175148141L;
    
    private long id;
    private String name;

    @Column(name = "c_id")
    @Id()
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    public long getId()
    {
        return id;
    }
    
    public void setId(long id)
    {
        this.id = id;
    }

    @Column(name = "c_name")
    public String getName()
    {
        return name;
    }
    
    public void setName(String name)
    {
        this.name = name;
    }
}
 

IndexDao.java

package com.lpw.ssh.item.index;

/**
 * @author lpw
 */
public interface IndexDao
{
    public void save(IndexModel index);
}
 

DaoImpl.java

package com.lpw.ssh.item.index.impl;

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

import com.lpw.ssh.item.index.IndexDao;
import com.lpw.ssh.item.index.IndexModel;

/**
 * @author lpw
 */
public class DaoImpl
    implements IndexDao
{
    private SessionFactory sessionFactory;
    
    @Override
    public void save(IndexModel index)
    {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        
        session.saveOrUpdate(index);
        
        transaction.commit();
        session.close();
    }

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

 IndexService.java

package com.lpw.ssh.item.index;

/**
 * @author lpw
 */
public interface IndexService
{
    public void saveName(String name);
    
    public String sayHi(String name);
}
 

ServiceImpl.java

 

package com.lpw.ssh.item.index.impl;

import com.lpw.ssh.item.index.IndexDao;
import com.lpw.ssh.item.index.IndexModel;
import com.lpw.ssh.item.index.IndexService;

/**
 * @author lpw
 */
public class ServiceImpl
    implements IndexService
{
    private IndexDao indexDao;
    
    @Override
    public void saveName(String name)
    {
        IndexModel index = new IndexModel();
        index.setName(name);
        
        indexDao.save(index);
    }

    @Override
    public String sayHi(String name)
    {
        return "hello "+name;
    }

    public void setIndexDao(IndexDao indexDao)
    {
        this.indexDao = indexDao;
    }
}

 IndexAction.java

package com.lpw.ssh.item.index;

/**
 * @author lpw
 */
public class IndexAction
{
    private String name;
    private IndexService indexService;
    
    public String execute()
    {
        indexService.saveName(name);
        
        return "success";
    }
    
    public String getHi()
    {
        return "hi "+name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setIndexService(IndexService indexService)
    {
        this.indexService = indexService;
    }
}
 

index/spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="index.dao.target" class="com.lpw.ssh.item.index.impl.DaoImpl" scope="prototype">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  <bean id="index.dao.source" class="org.springframework.aop.target.CommonsPoolTargetSource">
    <property name="targetBeanName" value="index.dao.target"/>
    <property name="maxSize" value="25"/>
  </bean>
  <bean id="index.dao" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource" ref="index.dao.source"/>
  </bean>

  <bean id="index.service.target" class="com.lpw.ssh.item.index.impl.ServiceImpl" scope="prototype">
    <property name="indexDao" ref="index.dao"/>
  </bean>
  <bean id="index.service.source" class="org.springframework.aop.target.CommonsPoolTargetSource">
    <property name="targetBeanName" value="index.service.target"/>
    <property name="maxSize" value="25"/>
  </bean>
  <bean id="index.service" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource" ref="index.service.source"/>
  </bean>

  <bean id="index.action" class="com.lpw.ssh.item.index.IndexAction" scope="prototype">
    <property name="indexService" ref="index.service"/>
  </bean>
</beans>

 index/struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <package name="index" namespace="/" extends="struts-default">
    <action name="index" class="index.action">
      <result>index.ftl</result>
    </action>
  </package>
</struts>
 

index.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring3.0+Struts2.1.8+Hibernate3.5</title>
</head>
<body>
<h1>${hi}</h1>
</body>
</html>
 

 

 

你可能感兴趣的:(spring,Hibernate,bean,struts,ssh)