hibernate学习笔记二
上一篇關于hibernate學習筆記一,主要是作為hibernate的入門知識。沒有和spring發生任何關系,這一篇我將把spring集成進去,看spring如何管理hibernate,還有和未使用spring之前有什么區別?將在文章后面附上使用spring集成和不使用spring集成,不同的地方。好,開始spring集成hibernate的學習之旅。
?
還是準備必要的jar包,如圖:
?
下面,開始新建項目工程,新建普通的java project,如圖:
?
其實,跟上一篇博客中的工程文件沒多大區別,主要的區別是去掉了hibernate.cfg.xml,增加了applicationContext.xml配置文件,主要原因是把hibernate交給spring來管理,所以hibernate.cfg.xml文件就不需要了
?
其中,model類中的實體類沒多大區別,主要看一下spring配置文件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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost/test"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mappingLocations"><list><value>classpath*:/cn/***/hibernate/model/*.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property></bean><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="hibernateSessionFactory"></property></bean><bean id="teacher" class="cn.***.hibernate.model.Teacher"></bean> </beans>?
其中,數據源配置,我們采用了dbcp,代碼如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost/test"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean>
這一部分和未使用spring集成之前的區別是,我們把數據源datasource配置在了hibernate.cfg.xml中
?
還有,使用了spring之后我們采用如下的配置方式,加載*.hbm.xml文件(其實加載該配置文件的方式有很多中,具體參照spring的文檔),代碼如下:
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mappingLocations"><list><value>classpath*:/cn/***/hibernate/model/*.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property></bean>?
比較關鍵的配置,就是下面的配置方法(spring采用這種配置去加載多個*.hbm.xml文件):
<property name="mappingLocations"><list><value>classpath*:/cn/***/hibernate/model/*.hbm.xml</value></list></property>
下面,就是我們真正看到spring和hibernate集成的主要地方,也是我們要使用hibernate的核心:
我們使用hibernateTemplate來保存實體到數據庫。
好到此為止,我們使用spring來管理hibernate的學習已經完成,下面使用代碼來進行測試
import java.util.Date;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.orm.hibernate3.HibernateTemplate;import cn.git.hibernate.model.Teacher;public class TeacherTest {/*** @param args*/public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");HibernateTemplate template = (HibernateTemplate) context.getBean("hibernateTemplate");Teacher teacher = (Teacher) context.getBean("teacher");teacher.setName("spring_hibernate");teacher.setAddress("spring");teacher.setYear(new Date());template.save(teacher);}}?
查看一下數據庫,看采用該方式是否能成功保存數據到數據庫
數據成功保存到數據庫,該篇學習筆記大功告成,結束。。。
?
總結
以上是生活随笔為你收集整理的hibernate学习笔记二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SharePoint 2010 cha
- 下一篇: VMware vSphere 5.1 群