JavaEE笔记(十四)

#SSH配置文件整合笔记实例

spring-BaseBean.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 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/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- 读取db.propertieds配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置连接池c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${jdbcUrl}" />
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />
        <!--当连接池中的连接用完时,C3P0一次性创建新连接的数目 -->
        <property name="acquireIncrement" value="${acquireIncrement}" />
        <!--初始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值 -->
        <property name="initialPoolSize" value="${initialPoolSize}" />
        <!--minPoolSize连接池 最少连接数 -->
        <property name="minPoolSize" value="${minPoolSize}" />
        <!--maxPoolSize连接池 最大连接数 -->
        <property name="maxPoolSize" value="${maxPoolSize}" />
        <!--maxIdleTime最大空闲时间 -->
        <property name="maxIdleTime" value="${maxIdleTime}" />
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <!-- 配置hibernate文件 -->
            <list>
                <value>com/pet/bean/xml/PetDiary.hbm.xml</value>
                <value>com/pet/bean/xml/PetInfo.hbm.xml</value>
            </list>
        </property>
        <!-- 配置hibernate系统执行读取配置文件 -->
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=true
                hibernate.hbm2ddl.auto=update
                hibernate.format_sql=true
            </value>
        </property>
    </bean>

    <!-- 开启事物管理 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.pet.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="select*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置bean对象 -->
    <bean id="PetDiaryDaoImpl" class="com.pet.dao.impl.PetDiaryDaoImpl">

        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="PetInfoDaoImpl" class="com.pet.dao.impl.PetInfoDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="PetDiaryServiceImpl" class="com.pet.service.impl.PetDiaryServiceImpl">
        <property name="petDiaryDaoImpl" ref="PetDiaryDaoImpl" />
    </bean>
    <bean id="PetInfoServiceImpl" class="com.pet.service.impl.PetInfoServiceImpl">
        <property name="petInfoDaoImpl" ref="PetInfoDaoImpl" />
    </bean>
</beans>    

db.properties

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/javaee
user=root
password=765800
acquireIncrement=10
initialPoolSize=10
minPoolSize=3
maxPoolSize=500
maxIdleTime=1800

spring-ActionBean.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.2.xsd">

    <bean id="PetInfoAction" class="com.pet.struts.PetInfoAction">
        <property name="petInfoServiceImpl" ref="PetInfoServiceImpl"/>
    </bean>
</beans>    

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!-- 开启开发者模式 -->
    <constant name="struts.devMode" value="true" />
    <!--把struts生产action的权利交给spring -->
    <constant name="struts.objectFactory" value="spring" />
    
    <package name="student" extends="json-default" namespace="/">
        <action name="pet_*" class="PetInfoAction"
            method="{1}">
            <result type="json"/>
            <allowed-methods>
                selectAllPets,delete,selectById,insert
            </allowed-methods>
        </action>
    </package>
</struts>

ps:配置文件仅供参考

我不作恶

但有权拒绝为善

我不赞同

但是我捍卫你不为善的权力

原文地址:https://www.cnblogs.com/HackerBlog/p/6170850.html