1。多出的配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<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/yay</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>qwe123</value>
</property>
</bean>
<!--
从单纯的Hibernate转到这里得Spring+Hibernate,原来得bean都不用修改
这里只是需要增加这个部分合下面的两个bean定义
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method="close">
<property name="dataSource">
<ref bean="datasource"/>
</property>
<property name="mappingResources">
<list>
<value>computer.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
<bean id="computerDao" class="com.yinbodotcc.springHibernate.transaction.ComputerDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
2。测试文件
package com.yinbodotcc.springHibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test
{
public static void main(String agrs[])
{
//这里和上一个例子用法就不一样了,这里是因为用了Spring+Hibernate
ApplicationContext context=new FileSystemXmlApplicationContext("dbcp-spring-config1.xml");
IComputerDao ic=(ComputerDao)context.getBean("computerDao");
Computer c=new Computer();
c.setId(8);//注意设置为auto_increment则这里的设置不起作用
c.setUser("yay");
c.setType("notepad");
ic.addComputer(c);
c=ic.findComputer(2);
if(c!=null)
System.out.println("发现得电脑使用者是: "+c.getUser());
}
}