Hibernate 入门

1. 什么是hibernate
hibernate 是一个ORM框架/持久层框架,全称Object_Relative DateBase-Mapping;
优势:跨数据库的无缝移植

2. 如何在项目中添加hibernate支持(手动添加)

  1. 添加hibernate相关依赖2.1 添加hibernate相关依赖
  2. 在resource目录下添加hibernate.cfg.xml(核心配置文件)
  3. 添加DTD支持
  4. 添加Hibernate的配置
    4.1 数据库相关url|connection.d(connection.username|connection.password|connection.river_class|dialect)
    4.2 调试相关(show_sql|format_sql)

hibernate.cfg.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>
		<!-- 1. 数据库相关 (数据库连接信息)-->
		<property name="connection.username">root</property>
		<property name="connection.password">123</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 配置本地事务(No CurrentSessionContext configured!) -->
		<property name="hibernate.current_session_context_class">thread</property>
		
		<!-- 2. 调试相关 (项目开发调试所用)-->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		
		<!-- 3. 添加实体映射文件 (hibernate所管理的实体类映射文件)-->
		<mapping resource="com/hibernate/entity/User.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>

	
  1. 在开发阶段再创建实体类和实体映射文件(*.hbm.xml)
    实体必须实现Serializable接口

实体映射文件:


<?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>
	<!-- 
		table对应着实体类所对应的表
		name指全类名
	 -->
	<class table="t_hibernate_user" name="com.zking.one.entity.User">
		<!-- 
			name指类属性
			type指类属性类型
			column指数据库表字段
		 -->
		<id name="id" type="java.lang.Integer" column="id">
			<!-- class指表的主键对应生成的类,class="increment"设置主键自增 -->
			<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>
	</class>
</hibernate-mapping>

  1. 使用hibernate实现 CRUD 操作
    1.读取配置
    2.创建SessionFactory(建模获取sessionfactory)
    3.打开Session
    4.开启事务
    5.执行数据库操作
    6.提交事务/回滚事务
    7.关闭Session

实例:

package com.hibernate.test;

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

import com.hibernate.entity.User;

public class Date1 {
	public static void main(String[] args) {
// 1.读取配置
	   Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");
//  2.创建SessionFactory(建模获取sessionfactory)	   
       SessionFactory sessionFactory = cfg.buildSessionFactory();
//   3.打开Session	
       Session session = sessionFactory.openSession();
// 4.开启事务       
       Transaction transaction = session.beginTransaction();
       
       User user=new User();
//  5.执行数据库操作       
       //增加
//       user.setUserName("中意");
//       user.setUserPwd("454");
//       session.save(user);
       
       //修改
//         user.setId(1);
//         user.setUserName("效果");
//         user.setUserPwd("124");
//         session.update(user);
//         
         
         //查询
         user.setId(3);
         User user2 = session.get(User.class, user.getId());
         user2.setUserName("Uzi");
         
         System.out.println(user2);
       
         //删除
//          user.setId(2);
//          session.delete(user);
 //  6.提交事务         
           transaction.commit();
  //7.关闭Session
           session.close();
       
	}

}

猜你喜欢

转载自blog.csdn.net/zimuliusu/article/details/83144520