Hibernate基础

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {   //工具类,将对象生成表,将实体类生成数据库表

	public static void main(String[] args) {
		
		//读取hibernate.cfg.xml文件
		//Configuration 加载系统配置文件
		Configuration cfg = new Configuration().configure();
		
		SchemaExport export = new SchemaExport(cfg);  //将类导成表
		
		export.create(true, true);
	}
}


//Annotation
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {   //工具类,将对象生成表,将实体类生成数据库表

	public static void main(String[] args) {
		
		//读取hibernate.cfg.xml文件
		//Configuration 加载系统配置文件
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);


new SchemaExport(new AnnotationConfiguration().configure()).create(
				true, true);
		// 第一个输出SQL语句 第二个导出表
	}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

//创建一个工具类,对SessionFactory和session对象进行封装

public class HibernateUtils {
	
	private static SessionFactory factory;
	
	static { //只要创建初始化一次
		try {
			//读取hibernate.cfg.xml文件
			Configuration cfg = new Configuration().configure();
			//创建SessionFactory,一个数据库对应一个SessionFactory
			factory = cfg.buildSessionFactory();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static SessionFactory getSessionFactory() {
		return factory;
	}
	
	public static Session getSession() {
		return factory.openSession();
	}
	
	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.close();
			}
		}
	}
}


//Annotation 默认自动close session
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

//创建一个工具类,对SessionFactory和session对象进行封装

public class HibernateUtil {

	private static final SessionFactory sessionFactory;

	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			sessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			//ex.printStackTrace();
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}
// Annotation test
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateIDTest {
	private static SessionFactory sessionFactory;
	// 创建一个 SessionFactory 
	@BeforeClass //注解
	public static void beforeClass() {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	@AfterClass
	public static void afterClass() {
		sessionFactory.close();
	}
	
	@Test
	public void testStudentSave() {
		StudentPK pk = new StudentPK();
		pk.setId(1);
		pk.setName("zhangsan");
		Student s = new Student();
		s.setPk(pk);
		s.setAge(8);
		
		
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(s);
		session.getTransaction().commit();
	}
	
	@Test
	public void testTeacherSave() {
	
		Teacher t = new Teacher();
		t.setId(1);
		t.setName("t1");
		t.setTitle("middle");
		t.setBirthDate(new Date());
		
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
	}
	
         // 用main函数和try catch调试找junit的bug
	public static void main(String[] args) {
		beforeClass();
	}
}
import java.util.Date;

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

public class Client {

	public static void main(String[] args) {
		
		//读取hibernate.cfg.xml文件
		//Configuration 加载系统配置文件
		Configuration cfg = new Configuration().configure();
		
		//创建SessionFactory,绑定数据库,一个数据库对应一个SessionFactory
		SessionFactory factory = cfg.buildSessionFactory();
		
		Session session = null;
		try {
			//可以看成是对connection进行包装,还管理缓存
			session = factory.openSession();
			
			//开启事务 Transaction
			session.beginTransaction();
			
			User1 user = new User1();
			user.setName("张三");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			
			//保存数据
			session.save(user);
			
			//提交事务
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			//回滚事务
			session.getTransaction().rollback();
		}finally {
			if (session != null) {
				if (session.isOpen()) {
					//关闭session
					session.close();
				}
			}
		}
		
	}
}

你可能感兴趣的:(Hibernate,xml,配置管理)