`
tianmo2008
  • 浏览: 66952 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring整合hibernate

阅读更多
项目要用到spring整合hibernate,因为以前只是看过一些介绍,
没有在项目上具体操作过,所以不得不上网找资料,虽然资料很多,
但配置起来还真烦琐。
spring整合hibernate,主要一点就是拿掉hibernate的hibernate.cfg.xml配置文件,
把数据库连接信息、sessionFactory和映射关系的配置文件*.hbm.xml统一
交由spring来管理。
一下是主要的一些操作:
下面操作都在spring自带的applicationContext.xml配置文件中进行,
<1> 配置数据原:
<!-- 配置数据原 -->
<bean id="dataSource"
	class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
		value="com.mysql.jdbc.Driver">
    </property>
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="000000"></property>
</bean>


<2>  配置session工厂
<!-- 配置session工厂 -->
<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource">
	<ref bean="dataSource" />
     </property>
     <property name="mappingResources">
        <list>
          <value>com/database/TUseInfo.hbm.xml</value>
          <value>com/database/*.hbm.xml</value>     
          <value>............</value>
          <value>............</value>     
        </list>
     </property>
     <property name="hibernateProperties">
        <props>
           <prop key="hibernate.dialect">
                 org.hibernate.dialect.MySQLDialect
           </prop>
        </props>
     </property>
</bean>

以上两个bean的配置可以由myeclipse的向导来完成,不用手工写
(<list>节点的子节点<value>则根据自己的情况添加相应的*.hbm.xml配置文件)。

接下来是配置Hibernate模板类,也是在applicationContext.xml配置文件中进行,
配置Hibernate模板类
这个bean好象只能手工填写...
<!-- 配置Hibernate模板类 -->
<bean id="hibernateTemplate"
      class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory">
         <ref bean="sessionFactory"/>
      </property>
      <property name="allowCreate">
        <value>true</value>
      </property>
</bean>


接下来是编写测试代码,一下是我测试用的java代码
package com.database;

public class TUseInfo implements java.io.Serializable {

	// Fields

	private Integer intId;
	private String name;
	private String password;

	// Constructors

	/** default constructor */
	public TUseInfo() {
	}

	/** full constructor */
	public TUseInfo(Integer intId, String name, String password) {
		this.intId = intId;
		this.name = name;
		this.password = password;
	}

	// Property accessors

	public Integer getIntId() {
		return this.intId;
	}

	public void setIntId(Integer intId) {
		this.intId = intId;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}


对应的映射配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.database.TUseInfo" table="t_use_info" catalog="test">
        <id name="intId" type="java.lang.Integer">
            <column name="int_id" />
            <generator class="assigned"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="32" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="32" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


测试用的main函数
package com.database;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class TeH {
	public static void main(String[] args) {
		 ApplicationContext ctx = 
new FileSystemXmlApplicationContext("src/applicationContext.xml");
		 HibernateTemplate hibernateTemplate = 
(HibernateTemplate) ctx.getBean("hibernateTemplate");
		 try {
			 TUseInfo t = 
(TUseInfo)hibernateTemplate.get(TUseInfo.class,0);
			 System.err.println(t.getName());
		 } catch (Exception e) {
			 	System.err.println(e);
		 }
	}
}

以上是spring整合hibernate大体的配置步骤,具体的很多细节,
只能随着项目的进展不断的完善了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics