01-Hibernate框架1

  • 框架是什么

    1. 框架是用来提高开发效率的

    2. 封装好了的一些功能我们需要使用这些功能的时候,调用即可,不需要再手动去实现。

    3. 所以框架可以理解成是一个半成品的项目,只要懂得如何驾驭这些功能即可。

  • hibernate框架

    • hibernate定义

      hibernate框架是一款orm(全称:object relation mapping)即对象关系映射框架

    • hibernate好处

      操作数据库的时候,可以以面向对象的方式来完成,不需要书写SQL语句

    • orm分三级

      hibernate属于3级: 完全面向对象操作数据库

      mybatis属于2级

      dbutils属于1级

  • hibernate框架的搭建

    • 导包

      1. hibernate核心包

      2. 数据库连接驱动包

    • 创建数据库,创建表

    • CREATE TABLE `cst_customer` (`cust_id` bigint(20) NOT NULL AUTO_INCREMENT,`cust_name` varchar(255) DEFAULT NULL,`cust_source` varchar(255) DEFAULT NULL,`cust_industry` varchar(255) DEFAULT NULL,`cust_level` varchar(255) DEFAULT NULL,`cust_linkman` varchar(255) DEFAULT NULL,`cust_phone` varchar(255) DEFAULT NULL,`cust_moblie` varchar(255) DEFAULT NULL,PRIMARY KEY (`cust_id`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;

    • 书写orm元数据

      1. 导入约束(文件头源文件名:hibernate-mapping-3.0.dtd)


      2. 实体类

      3. package com.qingzi.domain; ​ /** * CREATE TABLE `cst_customer` ( `cust_id` bigint(20) NOT NULL AUTO_INCREMENT, `cust_name` varchar(255) DEFAULT NULL, `cust_source` varchar(255) DEFAULT NULL, `cust_industry` varchar(255) DEFAULT NULL, `cust_level` varchar(255) DEFAULT NULL, `cust_linkman` varchar(255) DEFAULT NULL, `cust_phone` varchar(255) DEFAULT NULL, `cust_moblie` varchar(255) DEFAULT NULL, PRIMARY KEY (`cust_id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk; */ ​ 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_moblie; 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_moblie() { return cust_moblie; } public void setCust_moblie(String cust_moblie) { this.cust_moblie = cust_moblie; } @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_moblie=" + cust_moblie + "]"; } } ​
        orm元数据
        
        <?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">    <!-- 配置表与实体对象的关系-->    <!--package属性:填写一个包名 ,在元素内部凡是需要书写完整的类名的属性,可以直接写简单的类名 -->    <hibernate-mapping package="com.qingzi.domain">   <!--   class元素:配置实体与表的对应关系的   name:完整类名   table:数据库表名     -->   <class name="Customer" table="cst_customer">   <!-- id元素:配置主键映射的属性   name:填写主键对应属性名   column(可选):填写表中的主键列名 默认值:列名会使用属性名   type(可选):填写列(属性)的类型,hibernate会自动检测实体的属性类型。        每个类型有三种填法:java类型|hibernate类型|数据库类型       not-null(可选):配置该属性(列)是否不能为空.默认值:false       length(可选):配置 数据库列的长度.默认值:使用数据库类型的最大长度     -->   <id name="cust_id" column="cust_id">   <!--generator: 主键生成策略,就是每天记录录入时,主键的生成规则(7个)   identity:主键自增。由数据库来维护主键值,在录入时不需要指定主 键。   sequence:oracle中的主键生成策略   increment(了解) 主键自增,由hibernate来维护,每次插入前会先查 询表中id最大的,+1作为新主键值   hilo():高低位算法,主键自增,由hibernate。开发时不使用。   native:hilo+sequence+identity自动三选一策略   assigned:自然主键生成策略,hibernate不会管理主键值,由开发人员   自己录入。   -->   <generator class="native"></generator>   </id>   <!-- propert元素除id之外的普通属性映射   name:填写属性名       column(可选):填写列名       type(可选):填写列(属性)的类型,hibernate会自动检测实体的属性类型。        每个类型有三种填法:java类型|hibernate类型|数据库类型               not-null(可选):配置该属性(列)是否不能为空.默认值:false       length(可选):配置 数据库列的长度.默认值:使用数据库类型的最大长度     -->   <property name="cust_name" column="cust_name">   <!-- <column name="cust_name" sql-type="varchar"></column>-->   </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_moblie" column="cust_moblie"></property>   </class>    </hibernate-mapping>
        

    • 书写主配置文件

      1. 文件头(hibernate.cfg.xml,一般放在根目录下)文件头配置文件名:hibernate-configuration-3.0.dtd

        //文件名命名规范
        实体类名.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">
      2. 文件配置(数据库配置源文件名:)

猜你喜欢

转载自blog.csdn.net/qq_26270869/article/details/80809563