ssh第二讲--hibernate的单独使用

Hibernate的单独使用

一、创建配置hibernate项目

  1. 新建一个web project 项目
  2. 点击项目,然后点击左上角的project,再点击configure facets,然后点击install hibernate facet

在这里插入图片描述
在这里插入图片描述

然后finish,就可以了

3.配置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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

	<session-factory>
		<property name="myeclipse.connection.profile">
			MySQLConn
		</property>
        <!-- 方言 -->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.password">用户名</property>
		<property name="connection.username">密码</property>
		<property name="connection.url">
			jdbc:mysql://127.0.0.1:3306/数据库名?useSSL=false&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull
		</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<!--自动建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 打印hibernate创建的sql语句 -->
		<property name="show_sql">true</property>
		<!-- 打印hibernate创建的sql语句并进行格式化 -->
		<property name="format_sql">true</property>

		<!-- 打印hibernate创建的sql语句并进行格式化 -->
		<property name="connection.autocommit">true</property>

        <!-- 在配置映射表后添加以下配置 -->
		<!-- 配置IDcard的xml -->
		<mapping resource="com/lxf/entity/IDcard.hbm.xml" />
		<!-- 配置People的xml -->
		<mapping resource="com/lxf/entity/People.hbm.xml" />
	</session-factory>
</hibernate-configuration>

4.lib里面加上mysql-connector-java-5.1.48.jar(我是jdbc建立mysql连接)

5.建立数据库连接:

  • (1)将DB视图调出:Window–>show view–>other–>DB browser
  • (2)右键空白处new

  • (3)填写信息

6.创建表的映射

首先在src下建一个存实体类的文件夹:如com.lxf.entity,将自己的实体类建好

然后右键表点击Hibernate Reverse Engineering,构建数据库对应的表的映射

没有的话自己新建一个xx.hbm.xml文件,例如People.hbm.xml:

<?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">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<!--映射的文件夹 -->
<hibernate-mapping package="com.lxf.entity">
    <!-- name:映射的实体类名字,table:数据库中的表格名字,catalog:数据库名字,lazy:延迟加载-->
	<class name="People" table="tab_people" catalog="xxxx"
		lazy="true">
        <!--实体类对应表->
		<id name="id" type="java.lang.Integer" column="id" length="11">
			<!-- 外键生成 -->
			<generator class="identity">
				<param name="identity">id</param>
			</generator>
		</id>
		<!-- property标签就是字段的对应 -->
		<property name="name" type="java.lang.String" column="name"
			length="255">
		</property>
		<property name="sex" type="java.lang.String" column="sex"
			length="255">
		</property>
		<property name="age" type="java.lang.Integer" column="age"
			length="11">
		</property>
		<one-to-one name="idcard" class="com.lxf.entity.IDcard"
			cascade="all"></one-to-one>
	</class>
</hibernate-mapping>

二、测试代码

package com.lxf.test;


import org.hibernate.Session;

import com.lxf.entity.IDcard;
import com.lxf.entity.People;
import com.lxf.sessionFactoryUtil.HibernateSessionFactory;

public class test {
	public static void main(String[] args) {
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			//开启事务
			session.beginTransaction();
			//创建一个厂商实例
			IDcard iDcard = new IDcard();
			iDcard.setIdcard_code("123456");
			People people = new People();
			people.setName("lxf");
			people.setSex("男");
			people.setAge(1);
			people.setIdcard(iDcard);
			
			iDcard.setPeople(people);
			
			//保存商品
			session.save(people);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("更新错误,事务回滚!");
			//回滚事务
			session.getTransaction().rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
}

注意:HibernateSessionFactory.closeSession()要自己在系统提供给我们的方法类加上:

 /**
     *  Close the sessionFactory.
     *
     *  @throws HibernateException
     */
    public static void closeSessionFactory() throws HibernateException {
        if (sessionFactory != null&&!sessionFactory.isClosed()) {
        	sessionFactory.close();
        }
    }

1.Hibernate实例状态:Hibernate的实例状态分为三种,分别为瞬时状态(Transient)、持久化状态(Persistent)、脱管状态(Detached)。 关于hibernate的三种状态

2.hibernate的一级缓存自带,当两次使用同一个session查询同一个内容时,第二次查询不会打印sql语句,也就是直接从缓存中读取内容。

3.hibernate的二级缓存要配置才能使用,实现不同session查询内容实现缓存:

hibernate二级缓存

4.hibernate一对多、多对一、一对一(主键)、一对一(外键):hiberate关联关系

三、hibernate的高阶使用

1.Hibernate查询语言

2.Hibernate标准查询(Criteria)

3.hibernate原生SQL

4.hibernate注解(javax.persistence 包)

猜你喜欢

转载自blog.csdn.net/Inmaturity_7/article/details/106501455
今日推荐