基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置
来源:互联网 发布:成都知美术馆平面图 编辑:程序博客网 时间:2024/05/22 15:30
我们用的数据库为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>
0 0
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之六:Hibernate的flush和clear
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十:Spring mvc & hibernate 事务处理(回滚)
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之三:hibernate4泛型dao的应用
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之五:Hibernate的事务管理、手动回滚
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之七:Hibernate Entity的多字段组成主键处理
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十二: hibernate利用PostgreSQL自带的SEQUENCE生成主键
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十四:hibernate关于缓冲机制的巨坑!
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之四:JdbcTemplate的事务管理
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之一 : Maven的pom.xml环境配置及注意方面
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之九:Hibernate Session Helper类,用于Entity属性与表字段的操作...
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十一:利用拦截器控制访问权限
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之八:一个简单例子演示Spring-MVC中多种参数传递方式
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十三:用Spring嵌套事务(Nested Transaction) 回滚并返回自定义Exception
- Spring MVC的事务配置和Spring4与Hibernate4的整合
- 配置Spring的Proxool数据源
- 使用Eclipse基于Maven的Spring MVC 调试心得(二)--- Spring配置
- Spring整合Hibernate的多种数据源配置
- Docker大行其道—初识
- 20160615,微软6月14日发布16个安全补丁
- TCP协议
- STM32之CAN ---CAN ID过滤器分析
- android保留两位小数
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置
- Fragment跳转
- 当遇到文本的颜色不一样时,可以使用html
- 第一章 HTML标记与文档结构 (HTML标记基础)
- 过滤器判断是否登录跳转主页
- 使用MyBatis(二)使用java注解查询单行数据
- 如何在Eclipse下查看JDK源代码
- web service开发时,wsdl2java 客户端代码的生成
- Hibernate自定义注解类