Hibernate之入门

第一步:下载Hibernate的运行环境

第二步:创建表结构

  • 打开MySQL数据库
  • 建表语句如下:
    • Create database hibernate_day01;
      Use hibernate_day01;
      CREATE TABLE
      cst_customer(
      cust_idbigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
      cust_namevarchar(32) NOT NULL COMMENT '客户名称(公司名称)',
      cust_user_idbigint(32) DEFAULT NULL COMMENT '负责人id',
      cust_create_idbigint(32) DEFAULT NULL COMMENT '创建人id',
      cust_sourcevarchar(32) DEFAULT NULL COMMENT '客户信息来源',
      cust_industryvarchar(32) DEFAULT NULL COMMENT '客户所属行业',
      cust_levelvarchar(32) DEFAULT NULL COMMENT '客户级别',
      cust_linkmanvarchar(64) DEFAULT NULL COMMENT '联系人',
      cust_phonevarchar(64) DEFAULT NULL COMMENT '固定电话',
      cust_mobilevarchar(16) DEFAULT NULL COMMENT '移动电话',
      PRIMARY KEY (
      cust_id)
      ) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;

第三步:搭建hibernate开发环境

  • 在IDEA中,创建java工程,工程名:hibernateDemo1
  • 在工程目录下,创建lib文件夹,复制需要导入的jar包。
    • File–project structure—Modules–Dependencies–”+”号(jars or dictionary)–选中工程名下的lib文件夹,点击OK、
    • File–project structure—Libraries–”+”号,选java—选中要导入的jar包—运用,OK
  • 需要导入的jar包有:
    • MySQL的驱动jar包
    • Hibernate开发需要的包
    • 日志jar包

第四步:编写JavaBean实体类

  • 在src目录下,创建包结构,创建Customer类如下:
    • public class Customer {
      private Long cust_id;
      private String cust_name;
      private Long cust_user_id;
      private Long cust_create_id;
      private String cust_source;
      private String cust_industry;
      private String cust_level;
      private String cust_linkman;
      private String cust_phone;
      private String cust_mobile;
      // 省略get和set方法
      }
  • 注意:在创建实体类时,成员变量的类型用对应的包装类类型

第五步:创建类与表结构的映射

  • 在JavaBean所在的包下创建映射的配置文件
    • 默认的命名规则:实体类名.hbm.xml
    • 在xml配置文件中引入约束(引入的是hibernate3.0的dtd约束,不要引入4的约束)
    • 约束的查找方式: lib—-hibernate-core-5.0.7.final.jar—-org—-hibernate—-hibernate-mapping-3.0.dtd
      • <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    • 在eclipse中如果不能上网,编写配置文件是没有提示的,需要自己来配置
    • 编写映射的配置文件
      • <?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.itheima.domain.Customer" table="cst_customer">
        <id name="cust_id" column="cust_id">
        <generator class="native"/>
        </id>
        <property name="cust_name" column="cust_name"/>
        <property name="cust_user_id" column="cust_user_id"/>
        <property name="cust_create_id" column="cust_create_id"/>
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_linkman" column="cust_linkman"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
        </class>
        </hibernate-mapping>

第六步:编写Hibernate核心的配置文件

  • 在src目录下,创建名称为Hibernate.cfg.xml的配置文件
  • 在xml中引入DTD约束(约束的查找方式和上面的相同)
    • <!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  • 在资料/hibernate-release-5.0.7.Final/project/etc/hibernate.properties中,可以查看具体的配置信息
    • 必须配置的四个参数
      • hibernate.connection.driver_class com.mysql.jdbc.Driver
      • hibernate.connection.url jdbc:mysql:///xxx
      • hibernate.connection.username XXX
      • hibernate.connection.password XXXX
    • 数据库的方言(必须配置的)
      • hibernate.dialect org.hibernate.dialect.MySQLDialect
    • 可选的配置
      • hibernate.show_sql true
      • hibernate.format_sql true
      • hibernate.hbm2ddl.auto update
    • 引入映射配置文件(一定哟啊注意,要引入映射文件,框架需要加载映射文件)
      • <mapping resource="com/itheima/domain/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.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">root</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <mapping resource="com/itheima/domain/Customer.hbm.xml"/>
      </session-factory>
      </hibernate-configuration>

第七步:编写入门代码

/**
* 测试保存客户
*/
@Test
public void testSave(){
// 先加载配置文件
Configuration config = new Configuration();
// 默认加载src目录下的配置文件
config.configure();
// 创建SessionFactory对象
SessionFactory factory = config.buildSessionFactory();
// 创建session对象
Session session = factory.openSession();
// 开启事务
Transaction tr = session.beginTransaction();
// 编写保存代码
Customer c = new Customer();
// c.setCust_id(cust_id); 已经自动递增
c.setCust_name("测试名称");
c.setCust_mobile("110");
// 保存客户
session.save(c);
// 提交事务
tr.commit();
// 释放资源
session.close();
factory.close();
}

猜你喜欢

转载自blog.csdn.net/u011301372/article/details/81275602