1. 程式人生 > >spring4和hibernate4.0.0的整合

spring4和hibernate4.0.0的整合

myeclipse setting src blog start itl 3.0 uil spring

1.在myeclipse以下創建一個javaproject或者webproject,我創建的時webproject,用的myeclipse2013

2.導入spring的依賴包

技術分享

3.導入hibernate的依賴包

技術分享

4.在src文件夾以下配置hibernate的核心文件hibernate.cfg.xml

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate‘s automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<mapping resource="org/hibernate/domain/Event.hbm.xml"/>
<!-- annotation的配置方式 -->
<mapping class="org.hibernate.domain.Teacher"/>

</session-factory>

</hibernate-configuration>

5.在WEB-INFO文件夾以下配置spring的核心配置文件applicationContext

<?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.xsd">
<!-- <bean id="..." class="..."> collaborators and configuration for this
bean go here </bean> <bean id="..." class="..."> collaborators and configuration
for this bean go here </bean> more bean definitions go here -->
<!-- 配置sessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<bean id="DaoImp" class="com.persistent.DaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>



</beans>

6.編寫持久化類

package org.hibernate.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
//定義一個實體類
@Entity
public class Teacher {

private int id;//id
private String title;//職場
private String name;//姓名
//定義一個主鍵
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

7.編寫保存之久話的類和方法

package com.persistent;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.domain.Teacher;

public class DaoImp {

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public void save(){
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher teacher = new Teacher();
teacher.setId(6);
teacher.setName("hibernate!!");
teacher.setTitle("we will");
session.save(teacher);//利用annotation進行插入操作
session.getTransaction().commit();
}
}

8.在hibernate.cfg.xml文件裏配置

<!-- annotation的配置方式 -->
<mapping class="org.hibernate.domain.Teacher"/>

9.進行測試

package com.demo.test;

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

import com.persistent.DaoImp;

public class TestMain2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub





ApplicationContext context =
new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
/*Hello hello=(Hello) context.getBean("hello");
hello.display();*/
DaoImp di = (DaoImp)context.getBean("DaoImp");
di.save();

}

}

10.能夠看查看數據庫,已經有數據了,控制臺也打印出了sql語句

spring4和hibernate4.0.0的整合