码迷,mamicode.com
首页 > Web开发 > 详细

hibernate详细教程

时间:2019-06-17 13:03:38      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:数据库表   action   arc   nic   on()   行业   创建   mic   重要   

一、下载hibernate开发环境

1.https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/

2.创建一个项目,首先引入jar包。

 技术图片

 

3. 创建数据库表

CREATE TABLE `customer` (

  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘,

  `cust_name` varchar(32) COLLATE utf8_unicode_ci NOT NULL COMMENT ‘客户名称(公司名称)‘,

  `cust_source` varchar(32) COLLATE utf8_unicode_ci DEFAULT ‘null‘ COMMENT ‘客户信息来源‘,

  `cust_industry` varchar(32) COLLATE utf8_unicode_ci DEFAULT ‘null‘ COMMENT ‘客户所属行业‘,

  `cust_level` varchar(32) COLLATE utf8_unicode_ci DEFAULT ‘null‘ COMMENT ‘客户级别‘,

  `cust_phone` varchar(64) COLLATE utf8_unicode_ci DEFAULT ‘null‘ COMMENT ‘固定电话‘,

  `cust_mobile` varchar(16) COLLATE utf8_unicode_ci DEFAULT ‘null‘ COMMENT ‘移动电话‘,

  PRIMARY KEY (`cust_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 

4. 创建实体类Customer

 

private int cust_Id;

private String cust_name;

private String cust_source;

private String cust_industry;

private String cust_level;

private String cust_phone;

private String cust_mobile;

 

5. 创建映射文件

<?xml version="1.0" encoding="UTF-8"?>

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

<hibernate-mapping package="com.itheima.bean">

<class name="Customer" table="customer">

<id name="cust_id">

<!-- generator:主键生成策略 -->

<generator class="native"></generator>

</id>

 

<property name="cust_name" column="cust_name" ></property>

        <property name="cust_source" column="cust_source" ></property>

        <property name="cust_industry" column="cust_industry" ></property>

        <property name="cust_level" column="cust_level" ></property>

        <property name="cust_phone" column="cust_phone" ></property>

        <property name="cust_mobile" column="cust_mobile" ></property>

</class>

</hibernate-mapping>

 

6.创建一个Hibernate的核心配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- 配置数据库驱动 -->

<property name="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<!-- 配置数据库连接URL -->

<property name="connection.url">

jdbc:mysql://localhost:3306/cst_cuntmoer

</property>

<!-- 数据库user -->

<property name="connection.username">root</property>

<!-- 数据库user密码 -->

<property name="connection.password">admine</property>

<!-- 配置JDBC内置连接池 -->

<property name="connection.pool_size">1</property>

<!-- 配置数据库方言 -->

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<!-- 输出运行时生成的SQL语句 -->

<property name="show_sql">true</property>

<!-- 配置对输出的SQL语句进行格式化 -->

<property name="format_sql">true</property>

 

<!-- 配置映射文件 -->

<mapping resource="com/itheima/bean/Customer.hbm.xml" />

</session-factory>

</hibernate-configuration>

6. 映射的配置(重要)

class标签的配置】

 标签用来建立类与表的映射关系

 属性:

 name :类的全路径

 table :表名(类名与表名一致,table可以省略)

 catalog :数据库名

 id标签的配置】

 标签用来建立类中的属性与表中的主键的对应关系

 属性:

 name :类中的属性名

 column :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)

 length :长度

 type :类型

 property标签的配置】

 标签用来建立类中的普通属性与表的字段的对应关系

 属性:

 name :类中的属性名

 column :表中的字段名

 length :长度

 type :类型

 not-null :设置非空

 unique :设置唯一

7. 核心配置文件(了解,自行百度即可,hibernate.cfg.xml

必须的配置

 连接数据库的基本的参数

 驱动类

 url路径

 用户名

 密码

 方言

 可选的配置

 显示SQL hibernate.show_sql

 格式化SQL hibernate.format_sql

 自动建表 hibernate.hbm2ddl.auto

 none :不使用hibernate的自动建表

 create :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(测试)

 create-drop :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(测试)

 update:如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)

 validate:如果没有表,不会创建表。只会使用数据库中原有的表。(校验映射和表结构)。

 映射文件的引入

 引入映射文件的位置

 

 

8. 编写测试类

package com.itheima.hibernate.demo1;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.junit.Test;

 

import com.itheima.bean.Customer;

import com.itheima.utils.HibernateUtils;

 

/**

 * 入门案例

 *

 * @author csh

 *

 */

public class HibernateDemo1 {

 

/**

 * 增加操作

 */

@Test

public void demo1() {

// 加载核心配置文件

Configuration configuration = new Configuration().configure();

// 创建一个session工厂,类似于jdbc连接池

SessionFactory sessionFactory = configuration.buildSessionFactory();

// 获取session对象

Session session = sessionFactory.openSession();

// 手动开启事务

Transaction tranction = session.beginTransaction();

// 编写代码

Customer customer = new Customer();

customer.setCust_name("杨玉环");

session.save(customer);

 

tranction.commit();

session.close();

}

 

/**

 * 修改操作

 */

@Test

public void demo2() {

// 加载核心配置文件

Session session = HibernateUtils.OpenSession();

Transaction tx = session.beginTransaction();

Customer customer = session.get(Customer.class, 1);

customer.setCust_name("暗杀回电话");

session.update(customer);

tx.commit();

session.close();

}

 

/**

 * 删除操作

 */

@Test

public void demo3() {

// 加载核心配置文件

Session session = HibernateUtils.OpenSession();

Transaction tx = session.beginTransaction();

Customer customer = session.get(Customer.class, 1);

session.delete(customer);

tx.commit();

session.close();

}

 

/**

 * 保存或更新操作

 */

@Test

public void demo4() {

// 加载核心配置文件

Session session = HibernateUtils.OpenSession();

Transaction tx = session.beginTransaction();

Customer customer = new Customer();

customer.setCust_id(1);

customer.setCust_name("刘鹗恶化");

session.saveOrUpdate(customer);

tx.commit();

session.close();

}

}

/**

 * 查询所有

 */

@Test

public void demo5() {

// 加载核心配置文件

Session session = HibernateUtils.OpenSession();

Transaction tx = session.beginTransaction();

//接受HQL语句(面向对象查询语言)

/* Query query2 = session.createQuery("from Custonmer");

List<Customer> list2 = query2.list();

for (Customer customer : list2) {

System.out.println(customer);

}*/

 

//接受Sql语句

SQLQuery query = session.createSQLQuery("select * from customer");

List<Object[]> list = query.list();

for (Object[] object : list) {

System.out.println(Arrays.toString(object));

}

tx.commit();

session.close();

}

 

 

 

HibernateUtils工具类:

package com.itheima.utils;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

/**

 * hibernate抽取工具类

 * @author csh

 *

 */

public class HibernateUtils {

 

private static final Configuration cfg;

private static final SessionFactory sf;

static {

cfg= new Configuration().configure();

sf = cfg.buildSessionFactory();

}

 

public static Session OpenSession() {

return sf.openSession();

}

}

9. Transaction:事务对象

Hibernate中管理事务的对象。

l commit();

rollback();一般在出错后面

hibernate详细教程

标签:数据库表   action   arc   nic   on()   行业   创建   mic   重要   

原文地址:https://www.cnblogs.com/csh520mjy/p/11038987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!