ssh整合(1)

来源:互联网 发布:微信朋友圈数据采集 编辑:程序博客网 时间:2024/06/04 19:45

struts2+spring+hibernate整合

本文只是一个随笔,仅供参考,本文里面说的是一些配置
是我第一次ssh框架整合的配置,里面还有些不用的”肥肉“,我会在以后完善

首先是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.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"     destroy-method="close">     <property name="driverClass" value="com.mysql.jdbc.Driver"/>     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/weixin"/>     <property name="user" value="root"/>     <property name="password" value="root"/>     <property name="maxPoolSize" value="40"/>     <property name="minPoolSize" value="1"/>     <property name="initialPoolSize" value="1"/>     <property name="maxIdleTime" value="20"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">     <property name="dataSource" ref="dataSource"/>     <property name="mappingResources">         <list>             <value>resources/User.hbm.xml</value>         </list>     </property>     <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>             <prop key="hibernate.hbm2ddl.auto">update</prop>             <prop key="hibernate.jdbc.fetch_size">50</prop>             <prop key="hibernate.jdbc.batch_size">50</prop>         </props>     </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">     <property name="sessionFactory" ref="sessionFactory"/>     <!-- <property name="allowCreate" value="true"/> --> </bean> <bean id="dao" class="src.java.main.dao.daoImpl.BaseDaoImpl"/> <bean id="userService" class="src.java.main.service.serviceImpl.UserServiceImpl"/> <context:annotation-config/> <context:component-scan base-package="src.java.main"/></beans>

还有另一个hibernate的配置文件

<?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="src.java.main.entity.User" table="db_user">        <id name="id" column="id">            <generator class="assigned"/>        </id>        <property name="name" column="name"/>        <property name="password" column="password"/>        <property name="remarks" column="remarks"/>        <property name="createUser" column="createUser"/>        <property name="createDate" column="createDate"/>        <property name="modifyUser" column="modifyUser"/>        <property name="modifyDate" column="modifyDate"/>        <!-- cascade表明操作是否从父对象级联到被关联的对象,有一下值none,save-update,delete,all,delete-orphan -->        <one-to-one name="message" cascade="all" class="src.java.main.entity.Message"/>    </class> </hibernate-mapping>

然后因为这是hibernate的配置在spring里面配置了,所以这里只再贴下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>  <!--  <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> -->  <constant name="struts.objectFactory" value="spring" />   <package name="userPackage" extends="struts-default">   <action name="login" class="src.java.main.controller.UserAction" method="login">      <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">  <display-name></display-name>   <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <filter>      <filter-name>struts2</filter-name>      <!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->      <!--这里是有个规则的,根据版本的不同而发生变化,这里是定义一个拦截器-->      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter>      <filter-name>openSessionInViewFilter</filter-name>      <filter-class>          org.springframework.orm.hibernate3.support.OpenSessionInViewFilter      </filter-class>      <init-param>          <param-name>openSessionInViewFilter</param-name>          <param-value>/*</param-value>      </init-param>  </filter>  <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>  <listener>      <listener-class>          org.springframework.web.context.ContextLoaderListener      </listener-class>  </listener>  <session-config>      <session-timeout>          30      </session-timeout>  </session-config>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

还有一个daoImpl的代码:

public class BaseDaoImpl implements BaseDao {    @Resource(name="hibernateTemplate")    HibernateTemplate hibernateTemplate;    @SuppressWarnings("unchecked")    public Object loadById(Class clazz, Serializable id) {        return hibernateTemplate.get(clazz, id);    }    public List<?> query(String hql) {        List<?> list = null;        try {            list = hibernateTemplate.find(hql);        } catch (Exception e) {            e.printStackTrace();        }        return list;    }    @SuppressWarnings("deprecation")    public List<?> listAll(String clazz, int pageNo, int pageSize) {        final int pNo = pageNo;        final int pSize = pageSize;        final String hqlStr = "from "+clazz+" as c order by c.id desc";        List<?> list = hibernateTemplate.executeFind(                new HibernateCallback<Object>(){                    public Object doInHibernate(Session sn) throws                     HibernateException,SQLException{                        Query query = sn.createQuery(hqlStr);                        query.setMaxResults(pSize);                        query.setFirstResult((pNo - 1)*pSize);                        List<?> result = query.list();                        if(!Hibernate.isInitialized(result)){                            Hibernate.initialize(result);                        }                        return result;                    }                });        return list;    }    public void saveOrObject(Object obj) {        hibernateTemplate.saveOrUpdate(obj);    }}

这只是个开始

原创粉丝点击