Spring 简单整合 hibernate 例子_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Spring 简单整合 hibernate 例子

Spring 简单整合 hibernate 例子

 2010/12/29 8:11:02  tianjun309  http://tianjun309.javaeye.com  我要评论(0)
  • 摘要:applicationContext.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi
  • 标签:例子 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" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!--
	 org.apache.commons.dbcp.BasicDataSource 
	 里面的 driverClassName,url,username,password
	-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="org.firebirdsql.jdbc.FBDriver">
		</property>
		<property name="url" value="jdbc:firebirdsql://192.168.1.36:18306/USMDB">
		</property>
		<property name="username" value="sysdba"></property>
		<property name="password" value="masterkey"></property>
	</bean>

	<!-- 
		org.springframework.orm.hibernate3.LocalSessionFactoryBean
		里面的dataSource,hibernateProperties,mappingResources
	 -->
	<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.FirebirdDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/test/Test1.hbm.xml</value></list>
		</property></bean>
		
	<!-- 
		sessionFactory是org.springframework.orm.hibernate3.support.HibernateDaoSupport
		里面需要设置的
	 -->	
	<bean id="test1dao" class="com.test.Test1Dao">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
</beans>

?

MainTest.java

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainTest
{
	public static void main(String[] args)
	{
		/*
		 * 下面读配置文件和取bean的代码,可以写成factory
		 */
		ApplicationContext ctx = new FileSystemXmlApplicationContext(
				"src/applicationContext.xml");
		
		Test1Dao test1Dao = (Test1Dao) ctx
				.getBean("test1dao");
		
		
		Test1 test1 = new Test1();
		test1.setName("tianjuntest");
		
		test1Dao.saveOrUpdate(test1);
	}
}

?

最低要求JAR包:

commons-collections.jar
commons-dbcp.jar
commons-logging.jar
commons-pool.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jaybird-full-2.0.1.jar
log4j-1.2.14.jar
slf4j-api-1.5.8.jar
slf4j-nop-1.5.2.jar
spring.jar

?

?

?

发表评论
用户名: 匿名