Hibernate(一)

什么是框架?

可以完成一部分功能的半成品项目

学习框架的要求:

1.配置文件 - 关系、规范

2.流程

深入:框架原理、源码

JavaEE三层架构

web    service  dao

Hibernate(一)_第1张图片

5种框架

    SSH:Spring + Struts2 + Hibernate
    SSM:Spring + SpringMVC + MyBatis

搭建Hibernate框架的步骤:CRM

1.导jar包
    hibernate/lib/required
    数据库驱动包

Hibernate(一)_第2张图片
2.准备数据库/实体类

package beans;

public class Customer {
	private Long cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_linkman;
	private String cust_phone;
	private String cust_mobile;
	
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_linkman() {
		return cust_linkman;
	}
	public void setCust_linkman(String cust_linkman) {
		this.cust_linkman = cust_linkman;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman
				+ ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]";
	}
	public Customer() {
		super();
	}
	
}

 Customer
3.配置文件
    Hibernate主配置文件:src/hibernate.cfg.xml
            连接数据库:url username password driver
            配置文件键值对:解压/project/etc/hibernate.properties
    对象关系映射配置文件:hibernate-mapping.hbm.xml
                      ORM元数据
            位置随意,名字推荐:Customer.hbm.xml




	
	org.hibernate.dialect.MySQLDialect
	com.mysql.jdbc.Driver
	jdbc:mysql:///test
	root
	123456
	true
	true
	update
	
	



	
	
	
	
	
	
	
	
	
	
	
	

    3.1 Eclipse本地导入约束的步骤 - 破解联网


    3.2 复制Doctype
4.使用 - Java代码

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import beans.Customer;

public class HibernateTest01 {
	@Test
	public void test01() {
		// 1.加载配置文件  src/hibernate.cfg.xml
		Configuration config = new Configuration().configure();
		// 2.通过配置,获得session-factory
		SessionFactory factory = config.buildSessionFactory();
		// 3.获得Session对象,取代Connection
		Session session = factory.openSession();
		// 4.开启事务 Transaction
		Transaction tx = session.beginTransaction();
		// 5.Session操作数据库 增删改查
		// -----------------------
		// 根据ID查询 Customer 对象
//		Customer customer = session.get(Customer.class, 1);
//		System.out.println(customer);
		Customer c = new Customer();
		c.setCust_name("**");
		c.setCust_linkman("***");
		// 添加新增Customer
		session.save(c);
		// 6.关闭事务
		tx.commit();
		// 7.关闭资源 
		session.close();
		factory.close();
	}
	@Test
	// 学习Configuration
	public void test02() {
//		Configuration config = new Configuration();
//		// 必须调用,并且就返回对象本身
//		config.configure();
		
		Configuration config = new Configuration().configure();
		
		// configure() 无参,默认加载 classpath[src]/hibernate.cfg.xml
		
		// 添加ORM映射文件
		// config.addClass(Customer.class);
		// config.addResource("beans/Customer.hbm.xml");
		// 直接在主配置文件中添加了
	}
	
	@Test
	// 学习SessionFactory/Session
	public void test03() {
		Configuration config = new Configuration().configure();
		// SessionFactory: 
		//		作用简单来说就是为了得到操作数据库的核心对象session
		//		SessionFactory其中保存了所有的配置信息,非常消耗资源
		// 		SessionFactory设计,本身就是线程安全的
		// 结论:项目运行中,SessionFactory只有一个 - 单例模式
		SessionFactory factory = config.buildSessionFactory();
		
		// 每次都获得新的Session对象
		factory.openSession();
		// 获得当前Session对象 - 事务 - ThreadLocal
		factory.getCurrentSession();
	}
	@Test
	// 学习Transaction
	public void test04() {
		Configuration config = new Configuration().configure();
		SessionFactory factory = config.buildSessionFactory();
		
		Session session = factory.openSession();
		// 开启事务两种方式
		// 1.获得并且直接开启事务 - 推荐使用
		Transaction tx = session.beginTransaction();
		
		// 2.单纯的获得事务对象
//		Transaction tx = session.getTransaction();
//		tx.begin(); // 单独开启事务
		
		tx.commit(); // 提交事务 - try/finally
		//tx.rollback();// 回滚事务 - catch
	}
}
package test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import beans.Customer;
import utils.HibernateUtils;

public class HibernateTest02 {
	@Test
	// 新增对象
	public void test01() {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 5.Session操作数据库 增删改查
		// -----------------------
		// 新增 - id 自增长,不需要传入
		Customer c = new Customer();
		c.setCust_name("**");
		c.setCust_mobile("123456677");
		
		session.save(c);
		// -----------------------
		// 6.关闭事务
		tx.commit();
		// 7.关闭资源 
		session.close();
	}
	
	@Test
	// 根据ID查询对象
	public void test02() {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 5.Session操作数据库 增删改查
		// -----------------------
		// 传参:对象类型,id
//		Customer customer = session.get(Customer.class, 1L);
		Customer customer = session.load(Customer.class, 2L);
		System.out.println(customer);
		// -----------------------
		// 6.关闭事务
		tx.commit();
		// 7.关闭资源 
		session.close();
	}
	
	@Test
	// 修改对象
	public void test03() {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 5.Session操作数据库 增删改查
		// -----------------------
		// 先根据ID查询
		Customer customer = session.get(Customer.class, 1l);
		customer.setCust_name("**");
		customer.setCust_level("VVIP");
		session.update(customer);
		
		// -----------------------
		// 6.关闭事务
		tx.commit();
		// 7.关闭资源 
		session.close();
	}
	@Test
	// 删除对象
	public void test04() {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 5.Session操作数据库 增删改查
		// -----------------------
		// 先根据ID查询
		Customer customer = session.get(Customer.class, 1l);
		session.delete(customer);
		// -----------------------
		// 6.关闭事务
		tx.commit();
		// 7.关闭资源 
		session.close();
	}
}

创建Hibernate工厂类

package utils;

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

public class HibernateUtils {
	private static SessionFactory factory;
	
	static {
		Configuration config = new Configuration().configure();
		factory = config.buildSessionFactory();
	}
	
	public static  Session openSession() {
		return factory.openSession();
	}
	
	public static  Session getCurrentSession() {
		return factory.getCurrentSession();
	}
}

 

 

你可能感兴趣的:(框架,框架,SSH)