`
xyliufeng
  • 浏览: 86052 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring Hibernate集成配置文件

阅读更多
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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


	<bean id="userDao" class="dao.DaoIMPL">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</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.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<!--  prop key="hibernate.hbm2ddl.auto">create</prop -->
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>po/BookPO.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>111111</value>
		</property>
	</bean>
</beans>


Spring C3p0配置
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="/WEB-INF/jdbc.properties" />
	</bean>

	<bean id="oracleDataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass">
			<value>${mysqljdbc.driverClassName}</value>
		</property>
		<property name="jdbcUrl">
			<value>${mysqljdbc.url}</value>
		</property>
		<property name="user">
			<value>${mysqljdbc.username}</value>
		</property>
		<property name="password">
			<value>${mysqljdbc.password}</value>
		</property>
		<property name="minPoolSize">
			<value>5</value>
		</property>

		<!--  达到最大连接数后可以增加的连接数  个 -->
		<property name="acquireIncrement">
			<value>2</value>
		</property>
		<property name="maxPoolSize">
			<value>3</value>
		</property>
		<!--  最大闲置时间  秒 -->
		<property name="maxIdleTime">
			<value>600</value>
		</property>

		<property name="maxStatements">
			<value>100</value>
		</property>

		<!--  闲置的连接测试周期 秒  -->
		<property name="idleConnectionTestPeriod">
			<value>1200</value>
		</property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingResources">
			<list>
				<value>po/BookPO.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>

				<prop key="hibernate.generate_statistics">
					${hibernate.generate_statistics}
				</prop>
				<prop key="hibernate.dialect">
					${mysqlhibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>

			</props>
		</property>

		<property name="dataSource">
			<ref local="oracleDataSource" />
		</property>

	</bean>

	<!--事务管理-->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!--配置事务传播性-->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="query*" read-only="true" />
			<tx:method name="*Query" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="save*" rollback-for="Exception" />
			<tx:method name="*Save" rollback-for="Exception" />
			<tx:method name="update*" rollback-for="Exception" />
			<tx:method name="*Update" rollback-for="Exception" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 那些类使用事务 -->
	<aop:config proxy-target-class="true">
		<aop:pointcut id="serviceMethod" expression="execution(* net..*Service.*(..))" />
		<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
	</aop:config>

	<bean id="userDao" class="dao.DaoIMPL">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
</beans>


属性文件配置jdbc.properties
mysqljdbc.driverClassName=com.mysql.jdbc.Driver
mysqljdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
mysqljdbc.username=root
mysqljdbc.password=111111
hibernate.generate_statistics=true
mysqlhibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true

web.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">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>tesths</servlet-name>
    <servlet-class>action.tesths</servlet-class>
  </servlet>
  
  <!-- 普通spring上下文环境 -->
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>
	        /WEB-INF/applicationContext.xml
		</param-value>
	</context-param>
	
	<!-- log4j配置-->	
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	
	<!-- 字符集过滤 -->	
	<filter>
		<filter-name>SetCharacterEncodingFilter</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>
	    <init-param>
   		<param-name>forceEncoding</param-name>
        <param-value>true</param-value>
        </init-param>
	</filter>
	
	<!-- 加载applicationContext.xml的listener -->
	<listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	 </listener>

  <servlet-mapping>
    <servlet-name>tesths</servlet-name>
    <url-pattern>/tesths</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



servlet 使用spring
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("hibernate,spring测试操作");
		
	String bname=request.getParameter("bname");
	String bno=request.getParameter("bno");
	
	if(bname!=null&&bno!=null)
	{
		
		ServletContext application;  
		WebApplicationContext wac;  
		application = getServletContext();  
		 wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
		
		dao daoimpl = (dao)wac.getBean("userDao");		
		
		BookPO book= new BookPO();
		book.setBookname(bname);
		book.setBookno(bno);
		
		daoimpl.addBook(book);
		
		out.println("数据添加成功");
	}
		
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
分享到:
评论

相关推荐

    spring整合struts2与hibernate核心配置文件

    spring整合struts2与hibernate核心配置文件

    firebird embedded 嵌入式——Spring hibernate 集成连接配置

    你还可以查看 有详细极介绍。我从第一天中午到第二天中午才搞好,早网上查到配置方法我试了试都不行,一直不知道错的什么地方了。 该文件不需要 把dll文件放入 system32 目录下面,完全可迁移。

    spring+springMVC+hibernate集成

    spring+springMVC+hibernate集成,包括配置文件applicationContext.xml、web.xml、springMVC-servlet.xml

    spring_struts2_hibernate_mysql集成

    使用maven集成spring,struts2,hibernate三大框架,mysql数据库 密码账号 都在配置文件里 不懂可以联系我qq:1193298924

    Spring hibernate SpringMVC整合对数据库操作

    Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD。Hibernate使用方便,配置响应的XML文件即可。由于spring3.x,基于asm的某些特征,而这些asm还没有用jdk8编译,所以采用Spring 3+JDK8就会报错,...

    Spring+Hibernate+struts是如何整合起来的

    首先添加一个WEB工程 然后依次添加Struts框架,spring框架,...在添加Hibernate时注意选择现有的Spring配置文件,Hibernate的配置文件集成在Spring的配置文件中 最后在映射表的时候选择Spring的DAO作为IoC的实现

    新SSH(Spring+SpringMVC+Hibernate)框架结构的java web案例实践

    随着struts的安全问题的暴露,原由Struts2+spring+hibernate构成的SSH2已经被越来越多的开发者所弃用,反而,由Spring+SpringMVC+Hibernate构成的SSH框架越来越受欢迎!这里提供了一个案例代码,希望对大家搭建环境...

    HIbernate PPT

    全套的hibernatePPt,hibernate集成Struts2 hibernate集成spring hibernate的HQL注释的方式配置文件 hibernate状态 主键映射

    spring+springmvc+hibernate框架配置源码

    该demo集成了spring+springmvc+hibernate框架,里面的dao、service、entity均采用注解形式,容易开发,另外该demo中dao采用注解形式,将所有实体dao需要用到的通用方法如insert,update,delete,分页查询等均采用...

    spring如何摒弃hibernate.cfg.xml

    由于spring对hibernate配置文件hibernate.cfg.xml的集成相当好,可以在项目中放弃hibernate.cfg.xml文件,介绍mappingLocations、mappingDirectoryLocations与mappingJarLocations的用法和区别

    springMVC + Hibernate 工程模板

    两种配置:oracle mysql,切换数据库只要把SessionFactory的配置文件改成对应就可以了 c3p0配置:mysql调试通过,oracle由于存在问题,未配置 spring配置式事务管理(jdk动态代理,每个service必须对应一个接口) ...

    集成struts+spring+hibernate 网站后台管理系统

    本人自己动手集成的struts+spring+hibernate 网站后台管理系统,希望对刚学习struts的朋友带来一定的帮助,这是我上传此文件的最大愿望,里面有三个框架所需要的jar包和所有的配置文件,只要下载即可运行,不需要...

    Struts2+hibernate+spring集成

    本压缩文件中包含在struts2,hibernate,spring集成的时候 所需要用到的 三者的配置,又具体dao,service层以及action的配置,是学习S2SH集成好助手,能很好帮助你完成相关配置

    jersey+spring2.5+hibernate3.3+jpa

    基于 spring2.5 hibernate3.3 jpa 的annotation注解来减少配置文件的大小 通过jersey实现restful形式请求

    Struts2 + Spring3 + Hibernate3.5 整合(集成测试配套jar包更新构建脚本使用说明)

    本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...

    集成springmvc、hibernate、Mybatis和freemarker 的完整web系统架构

    基于springmvc、hibernate、Mybatis和freemarker 的完整web体统架构 数据持久层采用了hibernate和Mybatis技术集成。...配置文件在更目录下的config目录下,有freemarker,spring和Mybatis配置子目录。

    spring2.5学习PPT 传智博客

    使用Spring配置文件实现AOP 20.aspectj的切入点语法定义细节 21.搭建和配置Spring与jdbc整合的环境 22.Spring集成的jdbc编码和事务管理 23.使用Spring注解方式管理事务与传播行为详解 24.使用Spring配置文件...

    Struts2+Spring+Hibernate整合入门详解

    Spring: 是一个轻型的容器,利用它可以使用一个外部 XML 配置文件方便地将对象连接在一起。每个对象都可以通过显示一个 JavaBean 属性收到一个到依赖对象的引用,留给您的简单任务就只是在一个 XML 配置文件中把...

    struts2-hibernate-spring 集成jar包

    struts2-hibernate-spring 集成jar包,但在使用时要注意struts2的配置文件中的&lt;package&gt; 标签中不要配置 “namespace” 属性 且在页面中访问 Action 的请求要带“.action” 跑。

    springMVC完美集成hibernate实例

    springMVC完美集成hibernate实例及详细说明。采用注解方式和配置文件。适合入手的框架学习者

Global site tag (gtag.js) - Google Analytics