hibernate基础

hibernate概念

Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系,是一个全自动的 ORM(Object - Relationship - Mapping)框架,Hibernate 可以自动生成 SQL 语句,自动执行,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。
Hibernate 可以应用在任何使用 JDBC 的场合。

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

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

hibernate的状态

1.临时状态(transient):刚用new 语句创建,还没有被持久化,并且不处于Sesssion 的缓存中。处于临时状态的Java 对象被称为临时对象。
2.持久化状态(persistent):已经被持久化,并且加入到Session 的缓存中。处于持久化状态的Java 对象被称为持久化对象。
3.游离状态(detached):已经被持久化,但不再处于Session 的缓存中。处于游离状态的Java 对象被称为游离对象。

hibernate配置

配置前先将hibernate的需求jar包下好,这里使用Maven+hibernate
在这里插入图片描述

hibernate核心配置文件
1 添加DTD支持
<!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>

如果添加完DTD约束后没有快捷提示,则是Eclipse软件没有添加约束支持。
在这里插入图片描述
添加支持如下:
1.在这里插入图片描述
2.
在这里插入图片描述
3.
在这里插入图片描述

2 添加Hibernate的配置 mysql的jar使用5.x即可

<?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>
	<!-- 数据库相关配置 -->
	<!--(connection.username|connection.password|connection.url|connection.driver_class|dialect) -->
	<!-- 链接账户名称 -->
		<property name="connection.username">root</property>
		<!-- 链接账户密码 -->
		<property name="connection.password">2945994949</property>
		<!-- 链接绝对路径 -->
		<property name="connection.url">
			jdbc:mysql://localhost:3306/t243?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>
		<!-- 调试相关配置 -->
		<!-- hibernate 运行过程是否展示自动生成的sql代码 -->
		<property name="show_sql">true</property>
		<!--是否规范输出sql代码  -->
		<property name="format_sql">true</property>
		<!-- 实体映射相关配置 -->
	<!-- 实体映射相关配置 -->
	<mapping resource="com/shegx/entity/User.hbm.xml"/>
	
	</session-factory>
	</hibernate-configuration>
	

实体映射文件

Hibernate的实体映射文件一般命名规则是name.hbm.xml。另外需要在文件中加入约束:

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

如果没有快捷提示方法同上,不过要把支持文件换成mapping支持
在这里插入图片描述

实体映射文件示例
<?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>
    <!-- 
   name:实体类的全路径名
   table:实体类对应的数据库表名 
     -->
    <class name="com.shegx.entity.User" table="t_user_hb ">
    <!-- id:用于设置数据库表结构中主键列的生成方式
    name:实体类中的属性名称
    type:Java中的数据类型
    column:数据库表字段名称
     -->
    <id name="id" type="java.lang.Integer" column="id">
    <!-- 
      Class:定义主键列生成的方式,hibernate管理,数据库管理。开发者管理
      increment,identity,sequcene,native,assgine
     -->
 <generator class="increment"></generator>
    </id>
    <property name="userName" type="java.lang.String" column="user_name"></property>
    <property name="userPwd" type="java.lang.String" column="user_pwd"></property>
    <property name="realName" type="java.lang.String" column="real_name"></property>
    <property name="sex" type="java.lang.String" column="sex"></property>
    <property name="birthday" type="java.util.Date" column="birthday"></property>
    <property name="create_datetime" insert="false" update="false" type="timestamp" column="create_datetime"></property>
    <property name="remark" type="java.lang.String" column="remark"></property>
    
    </class>
    </hibernate-mapping>
    
User实体类
/**
 * 
 */
package com.shegx.entity;

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

/**
 * @author SHEGX
 *2020年7月23日上午10:03:37
 */
public class User implements Serializable {

	private Integer id;
	private String userName;
	private String userPwd;
	private String realName;
	private String sex;
	private Date birthday;
	private Timestamp create_datetime;
	private String remark;
	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * @return the userPwd
	 */
	public String getUserPwd() {
		return userPwd;
	}
	/**
	 * @param userPwd the userPwd to set
	 */
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	/**
	 * @return the realName
	 */
	public String getRealName() {
		return realName;
	}
	/**
	 * @param realName the realName to set
	 */
	public void setRealName(String realName) {
		this.realName = realName;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}
	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	/**
	 * @return the create_datetime
	 */
	public Timestamp getCreate_datetime() {
		return create_datetime;
	}
	/**
	 * @param create_datetime the create_datetime to set
	 */
	public void setCreate_datetime(Timestamp create_datetime) {
		this.create_datetime = create_datetime;
	}
	/**
	 * @return the remark
	 */
	public String getRemark() {
		return remark;
	}
	/**
	 * @param remark the remark to set
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}
	/**
	 * 
	 */
	public User() {
		
	}
	/**
	 * @param id
	 * @param userName
	 * @param userPwd
	 * @param realName
	 * @param sex
	 * @param birthday
	 * @param create_datetime
	 * @param remark
	 */
	public User(Integer id, String userName, String userPwd, String realName, String sex, Date birthday,
			Timestamp create_datetime, String remark) {
		
		this.id = id;
		this.userName = userName;
		this.userPwd = userPwd;
		this.realName = realName;
		this.sex = sex;
		this.birthday = birthday;
		this.create_datetime = create_datetime;
		this.remark = remark;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
				+ ", sex=" + sex + ", birthday=" + birthday + ", create_datetime=" + create_datetime + ", remark="
				+ remark + "]";
	}
}

简单增删改
/**
 * 
 */
package com.shegx.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


import com.shegx.entity.User;

/**
 * @author SHEGX
 *2020年7月23日上午11:19:57
 */
public class Demo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Demo demo=new Demo();
		demo.users();
	}

	public void users() {
		//创建Hibernate核心类
		Configuration cfg=new Configuration();
		//读取核心配置文件
		cfg.configure("hibernate.cfg.xml");
		//创建session工厂
		SessionFactory sf = cfg.buildSessionFactory();
		//获取session
		Session session = sf.openSession();
		//开启事物
		Transaction ts = session.beginTransaction();
		
		//新增
		User user=new User();
    	user.setUserName("集合");
        user.setUserPwd("1111");
        user.setRealName("困扰我脑壳");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setRemark("热热热");
        //新增保存
        session.save(user);
        
		
		//修改
			/*	user.setId(2);
				User u = session.get(User.class,user.getId());
				if(null!=u) {
					u.setRemark("修改");
					session.update(u);
					
				}*/
		
		
				//删除
				/*user.setId(3);
				User u=(User)session.get(User.class,user.getId());
				if(null!=u) {
					session.delete(u);
				}
		*/
		
		
		 
        //提交
		ts.commit();
		//关闭session
		session.close();
		
		
		
	}
	
	
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值