SSH整合配置(Spring版)

resource 创建 dbconfig.properties 配置文件

jdbc.url=jdbc:mysql://localhost:3306/crm?serverTimezone=GMT%2B8&characterEncoding=utf8
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=root
jdbc.filters=stat
jdbc.maxActive=20
jdbc.initialSize=1
jdbc.maxWait=60000
jdbc.minIdle=10
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000
jdbc.validationQuery=SELECT 'x'
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
jdbc.maxOpenPreparedStatements=20
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=1800
jdbc.logAbandoned=true

resource 创建 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: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.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
		

    <!-- 2.引入外部配置文件 -->
    <context:property-placeholder location="classpath:dbconfig.properties" />
    <!-- 2-1.配置阿里巴巴数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name = "url" value = "${jdbc.url}" />
        <property name = "username" value = "${jdbc.username}" />
        <property name = "password" value = "${jdbc.password}" />
        <property name = "driverClassName" value = "${jdbc.driverClassName}" />
        <property name = "filters" value = "${jdbc.filters}" />

        <!-- 最大并发连接数 -->
        <property name = "maxActive" value = "${jdbc.maxActive}" />

        <!-- 初始化连接数量 -->
        <property name = "initialSize" value = "${jdbc.initialSize}" />

        <!-- 配置获取连接等待超时的时间 -->
        <property name = "maxWait" value = "${jdbc.maxWait}" />

        <!-- 最小空闲连接数 -->
        <property name = "minIdle" value = "${jdbc.minIdle}" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name = "timeBetweenEvictionRunsMillis" value ="${jdbc.timeBetweenEvictionRunsMillis}" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name = "minEvictableIdleTimeMillis" value ="${jdbc.minEvictableIdleTimeMillis}" />

        <property name = "validationQuery" value = "${jdbc.validationQuery}" />
        <property name = "testWhileIdle" value = "${jdbc.testWhileIdle}" />
        <property name = "testOnBorrow" value = "${jdbc.testOnBorrow}" />
        <property name = "testOnReturn" value = "${jdbc.testOnReturn}" />
        <property name = "maxOpenPreparedStatements" value ="${jdbc.maxOpenPreparedStatements}" />

        <!-- 打开 removeAbandoned 功能 -->
        <property name = "removeAbandoned" value = "${jdbc.removeAbandoned}" />

        <!-- 1800 秒,也就是 30 分钟 -->
        <property name = "removeAbandonedTimeout" value ="${jdbc.removeAbandonedTimeout}" />

        <!-- 关闭 abanded 连接时输出错误日志 -->
        <property name = "logAbandoned" value = "${jdbc.logAbandoned}" />
    </bean>


    <!-- 2.spring和Hibernate整合 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 注入Hibernate配置文件 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <!-- 配置Hibernate映射 -->
        <property name="packagesToScan" value="priv.linyu.gzbos.pojo" />

        <property name="hibernateProperties">
            <props>
                <!-- 方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!-- 格式化sql -->
                <prop key="hibernate.format_sql">true</prop>
                <!-- 打印sql -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 生成表策略: <prop key="hibernate.hbm2ddl.auto">update</prop> -->
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:hbm/*.hbm.xml</value>
            </list>
        </property>

    </bean>

    <!-- 3.事务控制器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 4.配置hiberante的模版 -->
    <bean class="org.springframework.orm.hibernate5.HibernateTemplate">
    	<property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 5开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 6.开启组件扫描 -->
    <context:component-scan base-package="priv.linyu.gzbos" />


</beans>

编写 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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>gzbos</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!-- 1.spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 2.解决懒加载问题 -->
	<filter>
   		<filter-name>openSessionInView</filter-name>
   		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	

	<!-- 3.配置struts2拦截器 -->
	<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>/*</url-pattern>
	</filter-mapping>


	<!-- 4.字符编码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<!-- 4-1.设置响应的编码格式 -->
		<init-param>
			<param-name>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forcesponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- 4-2.统一设置 -->
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



	<!-- 5.连接池 启动Web监控统计功能 -->
	<filter>
		<filter-name>DruidWebStatFilter</filter-name>
		<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
		<init-param>
			<param-name>exclusions</param-name>
			<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>DruidWebStatFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>DruidStatView</servlet-name>
		<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>DruidStatView</servlet-name>
		<url-pattern>/druid/*</url-pattern>
	</servlet-mapping>


</web-app>
posted @ 2021-03-27 00:44  q-linyu  阅读(99)  评论(0)    收藏  举报