Hibernate框架_搭建第一个Hibernate框架

一、eclipse搭建

A.创建动态web项目

  New-->Dynamic web project(web project)

B.导入jar包

  1.数据库驱动包

   

  2.hibernate开发必须jar包(去官网下载hibernate,解压后里面lib-->required,里面jar包是hibernate必须jar包)

  

  3.日志记录包

  

C.创建表(也可以不创建)

D.创建实体类(数据库中如一个客户表就是客户实体,用Java语言描述出来)

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_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_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_phone=" + cust_phone
                + ", cust_mobile=" + cust_mobile + "]";
    }
    
    
    
}

E.根据实体创建映射文件(统一一下文件命名规范:类名.hbm.xml。文件位置一般和实体类放在一起)

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="com.xxx.pojo.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_phone" column="cust_phone"></property> <property name="cust_mobile" column="cust_mobile"></property> </class> </hibernate-mapping>

  标签及其属性所代表的含义:

  

class标签用来建立类和表的关系映射
            *name 表示该类的全路径(带包路径)
            *table 与该类映射的表名,如果表名和类名一致,表名可以省略不写
            *catalog:数据库名称,可以省略
 id标签用来建立类中属性域表中主键字段的对应
                *name 类中的属性名
                *column 表中的字段名,如果类中的属性名和表中的字段名一致,可以省略column
                *length:字段的长度,用于hibernate帮你建表时候指定表中字段的长度,默认为sql类型中长度最大值
                *type:类型,有三种写法:
                    java数据类型:如java.lang.String
                    hibernate类型: string  (type属性默认值)
                    sql类型: 如varchar

  对于type扩展:

                                              

 

property标签:用来建立表中类的普通属性域表中普通字段的对应
                这里面也同样有四个属性,name,column,length,type,和上面id标签用法一样,只是多了not-null,unique,可以设置属性非空或者唯一

 

F.创建hibernate配置文件。(该文件命名为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://localhost:3306/hibernate_crm?useUnicode=true&amp;characterEncoding=UTF8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!-- 配置hibernate方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- hibernate显示发送sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- hibernate格式化sql语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 数据定义语言,主要是对表的操作 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- hibernate映射文件加载 -->
        <mapping resource="com/xxx/pojo/customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

  对于hibernate.hbm2dd.autol属性,有四个值

      • none:不用hibernate自动生成表
      • create:每次运行hibernate都会创建一个新的表
      • create-drop:每次创建都会创建一个新的表,执行程序结束后删除这个表
      • update:如果数据库中有该表,使用数据库中原有的表,如果没有,hibernate会帮你创建一个新表,可以更新表的结构
      • validate:只会使用原有的表。对映射关系进行校验      

G.编写测试类

    /**
     * 插入数据
     * */
    @Test
    public void demo1() {
//        加载配置文件
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
//        根据配置文件获取session工厂对象
        SessionFactory sessionFactory = cfg.buildSessionFactory();
//        从session工厂里面获取session对象
        Session session = sessionFactory.openSession();
//        开启事务
        Transaction tx = session.beginTransaction();
//        保存更新数据
        Customer cust = new Customer();
        cust.setCust_name("张三");
        cust.setCust_source("介绍");
        cust.setCust_phone("13085469875");
        Serializable uid = session.save(cust);
        System.out.println(uid);
//        提交事务
        tx.commit();
//        关闭资源
        session.close();
    }

至此,eclipse手动搭建的hibernate框架就创建好了。

 

二.MyEclipse自动创建hibernate框架

 A.创建web项目

B.增加hibernate框架

  右键项目名称 选择Configure facet--->install hibernate facet(安装hibernate方面)

  

 

         

  

  

接下来直接finish。

c.生成hibernate映射文件(其实主要就是开启myeclipse数据库,利用数据库反转生成pojo)

  https://blog.csdn.net/tuke_tuke/article/details/49803293

     

myeclipse创建hibernate就大致这样