Hibernate操作SQLServer

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
    <session-factory>
       <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
       <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;database=table</property>
       <property name="connection.username">sa</property>
       <property name="connection.password"></property>
       <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
	   
	   <mapping resource="com/hibernate/beans/Customer.hbm.xml"/>
	   <mapping resource="com/hibernate/beans/Order.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
package com.hibernate.beans;

public class Customer {
	
	private Integer sysNo;
	private String account; 
	private String password;
	
	public Integer getSysNo() {
		return sysNo;
	}
	public void setSysNo(Integer sysNo) {
		this.sysNo = sysNo;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return 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">
<hibernate-mapping>
  <class name="com.hibernate.beans.Customer" table="Customer">
    <id name="sysNo" column="sysNo" type="integer">
       <generator class="identity"/>
    </id>
    <property  name="account" column="account" type="string"/>
    <property  name="password" column="password" type="string"/>
  </class>
</hibernate-mapping>
package com.hibernate.service;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hibernate.beans.Customer;
import com.hibernate.util.HibernateSessionFactory;

public class CustomerService {

	public Customer GetModel(Integer SysNo) throws HibernateException{
		Session session = null;
		Transaction tx = null;
		Customer customer = null;
		try{
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery("from Customer where sysno=?");
			query.setInteger(0, SysNo);
			customer = (Customer)query.uniqueResult();
			query = null;
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(tx!=null){
				tx.rollback();
			}
			HibernateSessionFactory.closeSession();
		}
		return customer;
	}
	
	public void saveCustomer(Customer customer) throws HibernateException{
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			session.save(customer);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(tx!=null){
				tx.rollback();
			}
			HibernateSessionFactory.closeSession();
		}
	}
	
	public List<Customer> getUsers(){
		Session session = null;
		Transaction tx = null;
		List users = null;
		try{
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			Criteria criteria = session.createCriteria(Customer.class);
			criteria.setMaxResults(50);
			users = criteria.list();
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return users;
	}
}
package com.hibernate.util;

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

public class HibernateSessionFactory {
	private static String CONFIG_FILE_LOCATION="hibernate.cfg.xml";
	
	private static final ThreadLocal threadLocal = new ThreadLocal();
	
	private static final Configuration cfg = new Configuration();
	
	private static SessionFactory sessionFactory;
	
	public static Session currentSession() throws HibernateException{
		Session session = (Session)threadLocal.get();
		if(session == null){
			if(sessionFactory == null){
				try{
				cfg.configure(CONFIG_FILE_LOCATION);
				sessionFactory = cfg.buildSessionFactory();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}
		return session;
	}
	
	public static void closeSession() throws HibernateException{
		Session session = (Session)threadLocal.get();
		threadLocal.set(null);
		if(session!=null){
			session.close();
		}
	}
}
GO
/****** 对象:  Table [dbo].[Customer]    脚本日期: 04/01/2015 16:51:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Customer](
	[SysNo] [int] IDENTITY(1,1) NOT NULL,
	[Account] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
	[Password] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
	[SysNo] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

猜你喜欢

转载自yiminhu.iteye.com/blog/2197755
今日推荐