Hibernate学习笔记 -- day01 Hibernate介绍及入门案例环境搭建

一、什么是ORM

object  Relation  Mapping:对象关系映射,即把实体类和数据库表建立起来的对应关系

二、Hibernate开发包介绍


三、搭建 Hibernate 的前期开发环境

1、创建数据库,导入数据

/*创建客户表*/
CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  `cust_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;

/*创建联系人表*/
CREATE TABLE `cst_linkman` (
  `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
  `lkm_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名',
  `lkm_gender` char(1) DEFAULT NULL COMMENT '联系人性别',
  `lkm_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
  `lkm_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机',
  `lkm_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱',
  `lkm_position` varchar(16) DEFAULT NULL COMMENT '联系人职位',
  `lkm_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
  PRIMARY KEY (`lkm_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

2、创建java工程,导入jar包

(1)、9个必要的jar包


(2)、3个日志依赖的包


(3)、mysql数据库驱动包


3、根据数据库编写对应的实体类

package cn.itcast.domain;

import java.io.Serializable;

/**
 * 客户的实体类
 * 
 * @Description: TODO
 * @author wingzhe
 * @date 2017年7月26日 下午1:54:44
 * @version V1.0
 */
public class Customer implements Serializable {

	private Long custId;
	private String custName;
	private String custSource;
	private String custIndustry;
	private String custLevel;
	private String custAddress;
	private String custPhone;

	public Long getCustId() {
		return custId;
	}

	public void setCustId(Long custId) {
		this.custId = custId;
	}

	public String getCustName() {
		return custName;
	}

	public void setCustName(String custName) {
		this.custName = custName;
	}

	public String getCustSource() {
		return custSource;
	}

	public void setCustSource(String custSource) {
		this.custSource = custSource;
	}

	public String getCustIndustry() {
		return custIndustry;
	}

	public void setCustIndustry(String custIndustry) {
		this.custIndustry = custIndustry;
	}

	public String getCustLevel() {
		return custLevel;
	}

	public void setCustLevel(String custLevel) {
		this.custLevel = custLevel;
	}

	public String getCustAddress() {
		return custAddress;
	}

	public void setCustAddress(String custAddress) {
		this.custAddress = custAddress;
	}

	public String getCustPhone() {
		return custPhone;
	}

	public void setCustPhone(String custPhone) {
		this.custPhone = custPhone;
	}

}
4、编写实体类对应的映射文件


(1)、导入dtd约束文件


(2)、编写映射文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入dtd约束:约束文件在hibernate的核心jar包中
	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">
<hibernate-mapping package="cn.itcast.domain"><!-- package用于导入一个包,接下来再配置文件中再出现此包的类都可以不用导了。 -->
	<!-- class标签:
			作用:
				用于建立实体类和表的对应关系
			属性:
			 	name:实体类的名称
			 	table:数据库的表名
	-->
	<class name="Customer" table="cst_customer">
		<!-- id标签:
				作用:用于映射主键
				属性:
					name:指定实体类中属性的名称(注意它找的是get/set方法后面的部分。不是私有成员变量名称)
					column:指定的数据库表的列名称
		-->
		<id name="custId" column="cust_id">
			<!-- generator标签:
					作用:指定主键的生成策略
					取值是hibernate提供的。
					我们今天只用一种:native
					它的含义:使用本地数据库的自动增长能力。
			-->
			<generator class="native"></generator>
		</id>
		
		<!-- property标签:
				作用:映射其他字段。
				属性:
				  name:指定的是实体类中属性get/set方法后面的部分。
				  column:指定的数据库表的列名称
		-->
		<property name="custName" column="cust_name"></property>
		<property name="custSource" column="cust_source"></property>
		<property name="custIndustry" column="cust_industry"></property>
		<property name="custLevel" column="cust_level"></property>
		<property name="custAddress" column="cust_address"></property>
		<property name="custPhone" column="cust_phone"></property>
	</class>
</hibernate-mapping>
5、配置hibernate核心配置文件


<?xml version="1.0" encoding="UTF-8"?>
<!-- hibernate的主配置文件,里面配置的都是和连接数据库相关的 -->
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<!-- 配置SessionFactory:
			SessionFactory它的作用就是用于生产Session对象的
			Session干吗的,它就是和数据库交互的。用于对数据库表进行增删改查的。
			
	此配置文件不需要大家背下来。但是必须要知道:
			创建SessionFactory必须由3部分信息组成。
			第一部分:连接数据库的信息
			第二部分:hibernate的基本配置
			第三部分:映射文件的位置
			
			配置文件的key都在:\hibernate-release-5.0.7.Final\project\etc\hibernate.properties中
	-->
	<session-factory>
		<!-- 1、连接数据库的信息 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/day36_ee247_hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1234</property>
		<!-- 2、hibernate的基本配置 -->
		<!-- 数据库的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 是否显示SQL语句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 是否格式化SQL语句 -->
		<property name="hibernate.format_sql">true</property>
		<!-- 选择生成DDL语句的策略 
			它只能用于更新表结构,不能用于创建数据库
		-->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 3、指定映射文件的位置 -->
		<mapping resource="cn/itcast/domain/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值