hiberbate入门

一、什么是hibernate

ORM框架/持久层框架

dao dao
jdbc hibernate
jdbc

ORM(Object Relational Mapping):对象关系映射。
对象与关系型数据库之间的映射管理框架

优势:跨数据库的无缝移植(SqlServer、Oracle、MySql)

二、如何在项目中添加hibernate

1、添加hiberbate相关依赖
在src/main/resources下面添加hibernate.cfg.xml(核心配置文件)
在这里插入图片描述
2 添加DTD支持
直接复制下面代码到第二行即可

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

** 3 添加Hibernate的配置 mysql的jar使用5.x即可**
①数据库相关(connection.username|connection.password|connection.url|connection.driver_class|dialect)

<session-factory>
		<!-- 数据库相关配置 -->
		<!-- 链接账户名称 -->
		<property name="connection.username">root</property>
		<!-- 链接账户密码 -->
		<property name="connection.password">123456</property>
		<!-- 链接地址的绝对路径 -->
		<property name="connection.url">
			jdbc:mysql://127.0.0.1:3306/books?useUnicode=true&amp;characterEncoding=UTF-8&amp;userSSL=false
		</property>
		<!-- 驱动的绝对路径 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 数据库方言配置 -->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
	</session-factory>

②调试相关(show_sql|format_sql)

<session-factory>
		<!-- 调试相关配置 -->
		<!-- hibernate运行过程是否展示生成的sql代码 -->
		<property name="show_sql">true</property>
		<!-- 是否规范化输出sql代码 -->
		<property name="format_sql"></property>
		<!-- 实体映射相关配置 -->
		<mapping resource="com/chendongyang/entity/User.hbm.xml"/>
	</session-factory>

4、小结
hibernate.cfg.xml(1)/*.hbm.xml(N)
实体映射文件一定要加到核心配置文件

三、hibernate 核心api分析

User实体类

package com.chendongyang.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

public class User implements Serializable {

	private static final long serialVersionUID = 5177318365071086065L;
	private Integer id;
	private String userName;
	private String userPwd;
	private String realName;
	private String sex;
	private Date birthday;
	private Timestamp createDatetime;
	private String remark;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Timestamp getCreateDatetime() {
		return createDatetime;
	}
	public void setCreateDatetime(Timestamp createDatetime) {
		this.createDatetime = createDatetime;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
				+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
				+ remark + "]";
	}
	public User() {}
	public User(Integer id, String userName, String userPwd, String realName, String sex, Date birthday,
			Timestamp createDatetime, String remark) {
		super();
		this.id = id;
		this.userName = userName;
		this.userPwd = userPwd;
		this.realName = realName;
		this.sex = sex;
		this.birthday = birthday;
		this.createDatetime = createDatetime;
		this.remark = remark;
	}
	
	
}
Demo 
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页