Hibernate初级入门

小编:管理员 50阅读 2022.09.14

一、Hibernate扮演的是什么角色

Hibernate在javaee中的三层结构中扮演的是DAO层框架

使用Hibernate框架简化了JDBC复杂的代码,是一种开源的轻量级的框架

二、如何搭建Hibernate的环境

1.

记得build path哟!!!!

三、ORM

Object Reational Mapping—-对象关系模型

在web阶段学习的javabean,现在叫实体类;

一个实体类对应数据库中一个表,,实体类中的属性对应数据库表字段

比如:

private String userName;<—————>t_userName(这个我们要通过映射文件来创建)

四、如何写一个实体类的映射文件:

Customer:客户 LinkMan:联系人

场景:一个客户对应多个联系,一个联系人对应一个客户

package cn.cq.enty;

import java.util.HashSet; import java.util.Set;

public class Customer { //客户id private Integer cid; //客户名称 private String custName; //客户级别 private String custLevel; //客户来源 private String custSource; //联系电话 private String custPhone; //手机 private String custMobile; //客户实体类中表示对个联系人 private Set<LinkMan> setLinkMan = new HashSet<LinkMan>(); public Set<LinkMan> getSetLinkMan() { return setLinkMan; } public void setSetLinkMan(Set<LinkMan> setLinkMan) { this.setLinkMan = setLinkMan; } public Customer() { super(); } public Customer(Integer cid, String custName, String custLevel, String custSource, String custPhone, String custMobile) { super(); this.cid = cid; this.custName = custName; this.custLevel = custLevel; this.custSource = custSource; this.custPhone = custPhone; this.custMobile = custMobile; } public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } public String getCustMobile() { return custMobile; } public void setCustMobile(String custMobile) { this.custMobile = custMobile; }

}

package cn.cq.enty;

public class LinkMan { private Integer lkm_id; // 联系人编号(主键) private String lkm_name;// 联系人姓名 private String lkm_gender;// 联系人性别 private String lkm_phone;// 联系人办公电话 //表示一个联系人对应一个客户 private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public LinkMan() { super(); } public LinkMan(Integer lkm_id, String lkm_name, String lkm_gender, String lkm_phone) { super(); this.lkm_id = lkm_id; this.lkm_name = lkm_name; this.lkm_gender = lkm_gender; this.lkm_phone = lkm_phone; } public Integer getLkm_id() { return lkm_id; } public void setLkm_id(Integer lkm_id) { this.lkm_id = lkm_id; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } public String getLkm_gender() { return lkm_gender; } public void setLkm_gender(String lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_phone() { return lkm_phone; } public void setLkm_phone(String lkm_phone) { this.lkm_phone = lkm_phone; } }

Customer.hbm.xml(放在同一个包下)

<?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属性:实体类全路径 table属性:数据库中表的名称 –> <class name=”cn.cq.enty.Customer” table=”t_customer”> <!– hibernate要求实体类有一个属性唯一值 要求表字段作为唯一值 –> <!– 配置实体类id和表中的id的关系 name属性:表示实体类中id属性名称 column属性:表示数据中表字段 –> <id name=”cid” column=”cid”> <!– 设置数据中id字段增长策略 native:表示id自动增长 –> <generator class=”native”></generator> </id> <!– 配置实体类属性和表字段对应关系 name属性:实体类属性名称 column属性:表字段名称 –> <property name=”custName” column=”custName”></property> <property name=”custLevel” column=”custLevel”></property> <property name=”custSource” column=”custSource”></property> <property name=”custPhone” column=”custPhone”></property> <property name=”custMobile” column=”custMobile”></property> <!– 使用set标签表示所有联系人 set标签name属性:客户实体类中set集合的名称 Column:表示外键 –> <set name=”setLinkMan” cascade=”save-update,delete” inverse=”true”> <key column=”cid”></key> <!– 客户所有的联系人 class:写联系人实体类的全路径 –> <one-to-many class=”cn.cq.enty.LinkMan”/> </set> </class> </hibernate-mapping>

LinkMan.hbm.xml:

<?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=”cn.cq.enty.LinkMan” table=”t_linkMan”> <id name=”lkm_id” column=”lkm_id”> <generator class=”native”></generator> </id> <property name=”lkm_name” column=”lkm_name”></property> <property name=”lkm_gender” column=”lkm_gender”></property> <property name=”lkm_phone” column=”lkm_phone”></property> <many-to-one name=”customer” column=”cid” class=”cn.cq.enty.Customer”></many-to-one> </class> </hibernate-mapping>

五。创建核心配置文件:

要求:放到src目录下

名称必须写成:hibernate.cfg.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.connection.driver_class”>com.mysql.jdbc.Driver</property> <property name=”hibernate.connection.url”>jdbc:mysql:///hibernate03</property> <property name=”hibernate.connection.username”>root</property> <property name=”hibernate.connection.password”>bdqn</property>

<!–mysql数据库的方言–> <property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>

<!–输出底层sql语句–> <property name=”hibernate.format_sql”>true</property>

<!–输出底层sql语句–> <property name=”hibernate.show_sql”>true</property>

<!–hibenate绑创建表

update:如果有表,更新,如果没得表,创建表

–> <property name=”hibernate.hbm2ddl.auto”>update</property> <!–和本地session绑定–> <property name=”hibernate.current_session_context_class”>thread</property>

<!–把映射文件放到核心配置文件中–> <mapping resource=”cn/cq/enty/Customer.hbm.xml”/> <mapping resource=”cn/cq/enty/LinkMan.hbm.xml”/> </session-factory> </hibernate-configuration>

六:HibernateUtils:

package cn.cq.test;

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

public class HibernateUtils { private static Configuration cfg = null; private static SessionFactory sessionFactory = null; static { cfg = new Configuration().configure(); sessionFactory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static Session getSession(){ return sessionFactory.getCurrentSession(); } }

七,实现CURD操作:

package cn.cq.test;

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

import cn.itcast.enty.Customer; import cn.itcast.enty.LinkMan;

public class HibernateOneToMang { @Test public void add(){ Session session = null; Transaction tx = null; try { session = HibernateUtils.getSession(); tx = session.beginTransaction(); Customer cus = new Customer(); cus.setCustName(“baidu”); cus.setCustLevel(“vip”); cus.setCustSource(“network”); cus.setCustPhone(“111”); cus.setCustMobile(“999”); LinkMan linkMan = new LinkMan(); linkMan.setLkm_name(“kevin”); linkMan.setLkm_gender(“nan”); linkMan.setLkm_phone(“3333”); cus.getSetLinkMan().add(linkMan); session.save(cus); tx.commit(); } catch (Exception e) { tx.rollback(); }finally{ } } @Test public void delete(){ Session session = null; Transaction tx = null; try { session = HibernateUtils.getSession(); tx = session.beginTransaction(); Customer cus = session.get(Customer.class, 1); session.delete(cus); tx.commit(); } catch (Exception e) { tx.rollback(); }finally{ } } @Test public void update(){ Session session = null; Transaction tx = null; try { session = HibernateUtils.getSession(); tx = session.beginTransaction(); Customer qq = session.get(Customer.class,3); LinkMan cidy = session.get(LinkMan.class, 3); qq.getSetLinkMan().add(cidy); cidy.setCustomer(qq); tx.commit(); } catch (Exception e) { tx.rollback(); }finally{ } } }

关联标签: