hibernate环境配置

本文介绍了一个使用Hibernate框架的示例应用,包括配置文件hibernate.cfg.xml的详细设置,展示了如何连接SQL Server数据库并自动管理数据表。此外,还提供了Order类及其对应的Hibernate映射文件Order.hbm.xml,用于演示实体类与数据库表之间的映射关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hibernate.cfg.xml(hibernate配置文件)

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

	<session-factory>
	<!--配置SQLServer连接属性//172.16.40.5-->
		<property name="dialect">
			org.hibernate.dialect.SQLServerDialect
		</property>
		<property name="connection.url">
			jdbc:sqlserver://172.16.40.5:1433;databaseName=dev_his
		</property>
		<property name="connection.username">sa</property>
		<property name="connection.password">bsoft</property>
		<property name="connection.driver_class">
			com.microsoft.sqlserver.jdbc.SQLServerDriver
		</property>
		<!--在控制台显示SQL语句-->
 		<property name="hibernate.show_sql">true</property>
 		<property name="show_sqlserver">true</property> 
		 <!--根据需要自动生成、更新数据表-->
		 <property name="myeclipse.connection.profile">sqlserver</property>
		 <!--注册所有ORM映射文件-->

			<mapping resource="com/bsoft/emrviewws/pojo/Order.hbm.xml" />
			<mapping resource="com/bsoft/emrviewws/pojo/InspectionResult.hbm.xml" />
			<mapping resource="com/bsoft/emrviewws/pojo/PlantResult.hbm.xml" />
			<mapping resource="com/bsoft/emrviewws/pojo/BioResult.hbm.xml" />
			<mapping resource="com/bsoft/emrviewws/pojo/AntiResult.hbm.xml" />
			<mapping resource="com/bsoft/emrviewws/pojo/PatientInfo.hbm.xml" />
			<mapping resource="com/bsoft/emrviewws/pojo/DescribeResult.hbm.xml" />
			
			<mapping resource="com/bsoft/emrviewws/pojo/OrderDetail.hbm.xml" />
			
	</session-factory>

</hibernate-configuration>


Order.java(pojo类):

package com.bsoft.emrviewws.pojo;

import java.sql.Timestamp;
import java.util.List;


public class Order {
	private String orderNumber;
	private double orderStatus;
	private Timestamp operateTime;
	private String operator;
	private List<OrderDetail> orderDetails;


	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	
	public double getOrderStatus() {
		return orderStatus;
	}
	public void setOrderStatus(double orderStatus) {
		this.orderStatus = orderStatus;
	}
	public Timestamp getOperateTime() {
		return operateTime;
	}
	public void setOperateTime(Timestamp operateTime) {
		this.operateTime = operateTime;
	}
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public List<OrderDetail> getOrderDetails() {
		return orderDetails;
	}
	public void setOrderDetails(List<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}
	
}

Order.hbm.xml(pojo对应数据库的xml配置文件):

<?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">
      
<hibernate-mapping>  
    <class name="com.bsoft.emrviewws.pojo.Order" table="l_lis_sqd">  
   	 <id name="orderNumber" column="DOCTREQUESTNO" type="string"/>
        <property name="orderStatus" column="SQDSTATUS" type="double"/>  
        <property name="operateTime" column="REQUESTTIME" type="timestamp"/>  
        <property name="operator" column="REQUESTER" type="string"/>  
        <list name="orderDetails" table="l_lis_sqdmx" cascade="all">  
      		<key>
      			<column name="orderNumber"/>
      		</key>  
      		<index column="[index]" type="string"/>
     		<one-to-many class="com.bsoft.emrviewws.pojo.OrderDetail"/>  
		</list>  
    </class>  
</hibernate-mapping>  

HibernateUtil.java(调用hibernate.cfg.xml产生Session):

package com.bsoft.emrviewws.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateUtil() {
    }
	
    public static synchronized  Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);
		}

        return session;
    }

	public static void rebuildSessionFactory() {
		try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}


    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

	public static org.hibernate.SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void setConfigFile(String configFile) {
		HibernateUtil.configFile = configFile;
		sessionFactory = null;
	}

	public static Configuration getConfiguration() {
		return configuration;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值