SSH框架学习笔记(1)

来源:互联网 发布:网络优化塔工2017招聘 编辑:程序博客网 时间:2024/05/19 03:21

SSH框架学习笔记(1)


在ssh框架常用到的配置文件有applicationContext.xml、struts.xml、db.properties等,下面展示几种常见的配置:

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: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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 导入数据库配置文件 -->    <context:property-placeholder location="classpath:db.properties" />    <!-- <context:component-scan base-package="service.impl"></context:component-scan> -->    <!-- 配置c3p0数据源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="jdbcUrl" value="${jdbcUrl}"></property>        <property name="driverClass" value="${driverClass}"></property>        <property name="user" value="${user}"></property>        <property name="password" value="${password}"></property>        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->        <property name="initialPoolSize" value="3"></property>        <!--连接池中保留的最小连接数。Default: 3 -->        <property name="minPoolSize" value="3"></property>        <!--连接池中保留的最大连接数。Default: 15 -->        <property name="maxPoolSize" value="3"></property>        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->        <property name="acquireIncrement" value="3"></property>        <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->        <property name="maxIdleTime" value="1800"></property>    </bean>    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource">            <ref bean="dataSource" />        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>                <prop key="javax.persistence.validation.mode">none</prop>                <prop key="hibernate.autoReconnect">true</prop>            </props>        </property>        <property name="mappingResources">            <list>                <value>bean/User.hbm.xml</value>                <value>bean/Privileges.hbm.xml</value>            </list>        </property>    </bean>    <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <tx:annotation-driven transaction-manager="transactionManager" />    <bean id="PrivilegesDAO" class="dao.PrivilegesDAO">        <property name="sessionFactory">            <ref bean="sessionFactory" />        </property>    </bean>    <!-- <bean id="LoginAction" class="action.LoginAction"> <property name="dao">         <ref bean="UserDAO"/> </property> </bean> -->    <bean id="userDAO" class="dao.UserDAOImpl">        <property name="sessionFactory">            <ref bean="sessionFactory" />        </property>    </bean>    <bean id="userService" class="service.impl.UserServiceImpl">        <property name="userDAO">            <ref bean="userDAO" />        </property>    </bean>    <bean id="login" class="action.LoginAction">        <property name="userService">            <ref bean="userService" />        </property>    </bean>    <!-- 事务管理 -->    <bean id="txMananger"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!-- 事务通知  -->    <tx:advice id="txAdvice" transaction-manager="txMananger">        <tx:attributes>            <tx:method name="get*" read-only="true" />            <tx:method name="find*" read-only="true" />            <tx:method name="list*" read-only="true" />            <tx:method name="*" rollback-for="Throwable" />        </tx:attributes>    </tx:advice>    <!-- aop设置需要切入事务控制的类 -->    <aop:config>        <aop:pointcut expression="execution( * service.impl.*.*(..))"            id="serviceOperation" />        <!-- 配置切入点表达式 -->        <!-- <aop:pointcut expression="bean(*Service)" id="serviceOperation"/> -->        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />    </aop:config></beans>

struts.xml配置内容:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>    <package name="default" extends="struts-default">        <action name="Log" class="action.LoginAction" >            <result name="success">index.jsp</result>            <result name="error">login.jsp</result>        </action>    </package></struts>    

db.properties配置内容:

jdbcUrl=jdbc\:mysql\://localhost\:3306/yourdbnamedriverClass=com.mysql.jdbc.Driveruser=rootpassword=123456

小结:在整合spring+struts2+hibernate框架时,经常会遇到框架之间的jar包版本冲突,这个主要是由于版本更进会导致一些方法被遗弃或者修改。

原创文章,转载需注明出处http://blog.csdn.net/drohe/article/details/73755952

原创粉丝点击