Coin163

首页 > 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置

基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

      我们用的数据库为postgresql 9.4, 连接池用的是proxool。

      先全局看看文件分布位置:


      先配置web.xml,在里面定义Spring的编码CharacterEncodingFilter,设置为utf8,proxool初始化类,和spring 的初始化类DispatcherServlet, spring容器初始化、web mvc这些的入口都靠它完成,在这个类里面指定容器的指定配置参数为classpath下的applicationContext.xml。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>gpchat</display-name>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--管理proxool配置文件-->
	<servlet>
	    <servlet-name>proxoolServletConfigurator</servlet-name>
	    <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator
	    </servlet-class>
	    <init-param>
	        <param-name>xmlFile</param-name>
	        <param-value>WEB-INF/proxool.xml</param-value>
	    </init-param>
	    <load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 
<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>
        org.springframework.web.context.ContextLoaderServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>	
 -->
	<servlet>
		<servlet-name>gpchat</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:/applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>gpchat</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>
因为spring作为容器,管理的类别很多,为了方便管理,我将它们分开存放(看图中黄色部分),然后在applicationContext.xml里面一一引用。

打开applicationContext.xml,里面最先导入数据库配置文件spring-db.xml,再导入hibernate配置,然后自动扫描controller和dao的包并根据包里面类的依赖关键字进行加载;这里还配置了一个contextHolder的类,可以用它直接获得容器里面的类实例(ContextHolder.getBean("beanid"));最后加载spring-config/下面所有的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:mvc="http://www.springframework.org/schema/mvc"
	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.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
       
    <!--     
	<context:component-scan base-package="com.freestyle.test"/>
	 -->
	<import resource="classpath*:/spring-db-config/spring-db.xml"/>
	<import resource="classpath*:/hibernate-config/spring-hibernate.xml"/>
	 <context:component-scan base-package="com.freestyle.spring;com.freestyle.test.controller;com.freestyle.test.hibernate.dao;com.freestyle.test.hibernate.controller"/>
	 
	 
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 解决@ResponseBody 的实现类其默认的编码是 iso-8859-1的问题 -->
	<mvc:annotation-driven>
		<mvc:message-converters>
			<!-- default StringHttpMessageConverter, solve encoding problem -->
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg value="UTF-8" />
				<property name="writeAcceptCharset" value="false" />
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/views" />
		<property name="suffix" value=".jsp" />
	</bean>
    <!-- 通过配置contextHolder来获得ApplicationContext -->    
    <bean id="contextHolder" class="com.freestyle.spring.ContextHolder">
	</bean>    

	<import resource="classpath*:/spring-config/*.xml"/>	 
	
	<!-- ss 	
	<bean name="/hello" class="com.freestyle.test.controller.HelloController"/>
	 -->
</beans>

下面是spring-db.xml数据源的配置,里面除了配置dataSource,还有一个jdbcTemplate:

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
	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.1.xsd 
        http://www.springframework.org/schema/mvc         
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
		</property>
		<property name="url">
			<value>proxool.bmsdbv2</value>
		</property>
	</bean>
	<!-- 配置Jdbc Template -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource">			
		</constructor-arg>
	</bean>
		
	

</beans>


 

下面是hibernate插件的配置文件,spring-hibernate.xml,里面配置了sessionfactory和transactionmanager,个人比较抗拒为每个pojo配置一个xml(简直多此一举),所以用在pojo里面注解的方式作为hibernate的entity,所以指定了packagesToScan属性,传入entity所在的包,hibernate会在适当的时候使用这些entities:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre">	</span>xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre">	</span>xmlns:tx="http://www.springframework.org/schema/tx"
<span style="white-space:pre">	</span>xsi:schemaLocation="http://www.springframework.org/schema/beans 
<span style="white-space:pre">		</span>http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd 
        http://www.springframework.org/schema/mvc         
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">    	
	<!-- 配置hibernate的session factory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy">
		<property name="dataSource" ref="dataSource"></property>
		<property name="packagesToScan"> <!-- hibernate sessionFactory管理的类存放位置,自动装载 -->
			<list>
				<value>com.freestyle.test.hibernate.entities</value>
			</list>						
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
				<!-- 设置自动创建|更新|验证数据库表结构 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 是否在控制台显示sql -->
				<prop key="hibernate.show_sql">true</prop>
                <!-- 是否格式化sql,优化显示 -->
                <prop key="hibernate.format_sql">true</prop>
                <!-- 是否开启二级缓存 -->
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <!-- 是否开启查询缓存 -->
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <!-- 数据库批量查询最大数 -->
                <prop key="hibernate.jdbc.fetch_size">100</prop>
                <!-- 数据库批量更新、添加、删除操作最大数 -->                
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <!-- 是否自动提交事务 -->
                <prop key="hibernate.connection.autocommit">false</prop>
                <!-- 指定hibernate在何时释放JDBC连接 -->
                <prop key="hibernate.connection.release_mode">auto</prop>
			</props>
    </property>
	</bean>
    <!-- 定义Hibernate事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>     
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


原文

      我们用的数据库为postgresql 9.4, 连接池用的是proxool。       先全局看看文件分布位置:       先配置web.xml,在里面定义Spring的编码CharacterEncodingFilter,设置为utf8,proxool初

------分隔线----------------------------