Hibernate day1

框架是什么?

1.提高开发效率,只要调用即可,不需要手动实现,相当于半成品项目

2.以面向对象的方式完成数据库操作

3.orm元数据配置(object-relationship mapping)

orm分为4级

hibernate 4级

mybatis 2级

dbutils 1级

搭建步骤:

1.导包

  导入require下面的包

2.创建数据库准备表

  新建一个Student的Class

3.书写orm元数据(配置文件为Student.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">

<hibernate-mapping package="com.littlepage.domain">
    <class name="Student" table="t_student">
        <!--
            id元素为主键元素
        -->
        
        <id name="id" column="id">
        </id>
        <!--property元素
        name:属性名
        column:列名
        type:3个可选
        sql-type:数据类型(varchar等)
        not-null:配置属性是否不能为空,默认值:false
        length:配置长度,默认是数据库类型最大长度
        -->
        <property name="name" column="name" ></property>
        <property name="age" column="age"></property>
        <property name="grade" column="grade"></property>
    </class>
</hibernate-mapping>

4.书写主配置文件(配置文件为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>
        <!-- 必须的属性 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        
        <!-- 显示和格式化输出语句 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--auto属性
         create 表数据会丢失
         create-drop 自动创建,每次创建运行结束,都会把所有表删除
         update 不丢失
         validate 校验实体和数据库是否一致,不自动生成表
         -->
         <!-- 引入orm元数据 -->
         <mapping resource="com/littlepage/domain/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
    

5.书写代码测试

package com.littlepage.test;

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

import com.littlepage.domain.Student;

public class OperatorTest {
    public static void main(String[] args) {
        //1.配置加载类,.configure是进行读取默认配置文件
        Configuration conf=new Configuration().configure();
        //2.根据配置信息,配置SessionFactory对象,sessionfactory消耗内存特大,属于线程安全,保证sessionFactory只有一个在内存
        SessionFactory sf=conf.buildSessionFactory();
        //3.打开新session,获得已经绑定的Session,getCurrentSession
        //session在已经学过中出现3次,与web服务器的session,与邮件服务器的session,与数据库的session
        Session session=sf.openSession();
        //4.获得事务对象,建立并开启事务,getTransaction只获取,不进行开启事务
        Transaction tx=session.beginTransaction();
        
        Student s=new Student();
        s.setId("20202223");
        s.setName("littlepage");
        s.setAge("15");
        s.setGrade("100");
        session.save(s);
        
        tx.commit();//提交
        //tx.rollback();//回滚
            session.close();//关闭
            sf.close();
    }
}

封装HibernateUtils类:

因为比较完备,所以只有2个方法

package com.littlepage.test;

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

public class HibernateUtils {
    /**
     * get open session
     * @return the thread session
     */
    public static Session openSession(){
        Configuration conf=new Configuration().configure();
        SessionFactory sf=conf.buildSessionFactory();
        Session session=sf.openSession();
        return session;
    }
    /**
     * get current session
     * @return the current session
     */
    public static Session getCurrentSession(){
        Configuration conf=new Configuration().configure();
        SessionFactory sf=conf.buildSessionFactory();
        Session session=sf.getCurrentSession();
        return session;
    }
}

猜你喜欢

转载自www.cnblogs.com/littlepage/p/10625067.html