Java进阶学习笔记(二)Hibernate框架

Hibernate框架

一、概述

二、入门案例——使用Hibernate

1.安装Hibernate插件到eclipse环境

1)下载hibernate tools插件:http://hibernate.org/
2)安装
Java进阶学习笔记(二)Hibernate框架_第1张图片
注意取消勾选 Contact all update sites
Java进阶学习笔记(二)Hibernate框架_第2张图片

2.导入Hibernate的jar包

Java进阶学习笔记(二)Hibernate框架_第3张图片

3.创建持久化Java类

这里创建的持久化Java类的属性要和数据库表中的字段所对应

public class User {
    private Integer id;
    private String name;
    private String password;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

4.在持久化Java类的同目录下创建“对象-关系”映射文件(hbm.xml文件)

Java进阶学习笔记(二)Hibernate框架_第4张图片
自动生成如下xml文件




<hibernate-mapping>
    <class name="com.hibernate.domain.User" table="USER">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" /> 
        id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        property>
    class>
hibernate-mapping>

5.配置Hibernate配置文件



<hibernate-configuration>
    <session-factory>
        
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
        
        <property name="hibernate.connection.url">jdbc:mysql:///testhibernateproperty>
        
        <property name="hibernate.connection.username">rootproperty>
        
        <property name="hibernate.connection.password">123456property>        
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
        
        <property name="hibernate.show_sql">trueproperty>
        
        <property name="format_sql">trueproperty>
        
        <property name="hibernate.hbm2ddl.auto">updateproperty>
        
        <mapping resource="com/hibernate/domain/User.hbm.xml"/>
    session-factory>
hibernate-configuration>

6.编写测试类

package com.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
import com.hibernate.domain.User;
public class TestHibernate {

    @Test
    public void insertTest(){
        //1、加载hibernate.cfg.xml配置
        Configuration config = new Configuration().configure("hibernate.cfg.xml");
        //2、获取sessionFactory实例

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()  
                                          .applySettings(config.getProperties())  
                                     .buildServiceRegistry();  
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); 
        //3、获得session对象
        Session session = sessionFactory.openSession();

        //4、开启事务
        Transaction transaction = session.beginTransaction();

        //5、操作
        User us = new User();
        us.setName("cz");
        us.setPassword("1234");
        session.save(us);

        //6、提交事务
        transaction.commit();

        //7、关闭资源
        session.close();
        sessionFactory.close();

    }

    @Test
    public void selectTest(){
        // 1、加载hibernate.cfg.xml配置
        Configuration config = new Configuration().configure("hibernate.cfg.xml");
        // 2、获取sessionFactory实例

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
                .buildServiceRegistry();
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 3、获得session对象
        Session session = sessionFactory.openSession();

        // 4、开启事务
        Transaction transaction = session.beginTransaction();

        String hql = "from User where id = ?";
        Query query = session.createQuery(hql);
        query.setParameter(0, 1);//设置参数,参数位置从0开始
        List list = query.list();

        // 6、提交事务
        transaction.commit();
        // 7、关闭资源
        session.close();
        sessionFactory.close();

    }

    @Test
    public void updateTest(){
        // 1、加载hibernate.cfg.xml配置
        Configuration config = new Configuration().configure("hibernate.cfg.xml");
        // 2、获取sessionFactory实例

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
                .buildServiceRegistry();
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 3、获得session对象
        Session session = sessionFactory.openSession();

        // 4、开启事务
        Transaction transaction = session.beginTransaction();



        // 6、提交事务
        transaction.commit();
        // 7、关闭资源
        session.close();
        sessionFactory.close();

    }
}

使用单元测试,成功后可以在控制台打印执行的SQL语句
Java进阶学习笔记(二)Hibernate框架_第5张图片
数据库中的表也被修改
Java进阶学习笔记(二)Hibernate框架_第6张图片

你可能感兴趣的:(学习之路)