Hibernate(一)

什么是框架?

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

学习框架的要求:

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

2.流程

深入:框架原理、源码

JavaEE三层架构

web    service  dao

5种框架

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

搭建Hibernate框架的步骤:CRM

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


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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql:///test</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">123456</property>
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<mapping resource="Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.DTD">
<hibernate-mapping>
	<class name="hibernate.Customer" table="cst_customer">
	<id name ="cust_id" column="cust_id">
	<generator class="native"></generator>
	</id>
	<property name="cust_name" column="cust_name"></property>
	<property name="cust_source" column="cust_source"></property>
	<property name="cust_industry" column="cust_industry"></property>
	<property name="cust_level" column="cust_level"></property>
	<property name="cust_linkman" column="cust_linkman"></property>
	<property name="cust_phone" column="cust_phone"></property>
	<property name="cust_mobile" column="cust_mobile"></property>
	</class>
</hibernate-mapping>

    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");
		// 直接在主配置文件中添加了<mapping>
	}
	
	@Test
	// 学习SessionFactory/Session
	public void test03() {
		Configuration config = new Configuration().configure();
		// SessionFactory: <session-factory>
		//		作用简单来说就是为了得到操作数据库的核心对象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();
	}
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值