美文网首页
Idea Hibernate

Idea Hibernate

作者: dragon海鸟 | 来源:发表于2021-01-08 15:07 被阅读0次

1、生成hibernate.cfg.xml文件


image.png

2、添加数据库


image.png

3、创建数据
4、生成映射文件

a. image.png
b. image.png

5、数据库配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
      <!-- 数据库方言 -->
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <!-- 驱动 -->
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <!-- URL -->
      <property name="connection.url">jdbc:mysql://localhost:3306/Hibernate01</property>
      <!-- 用户名 -->
      <property name="connection.username">root</property>
      <!-- 密码 -->
      <property name="connection.password">12345678</property>
      <!-- 在控制台显示SQL语句 -->
      <property name="show_sql">true</property>
      <!--注册映射表-->
      <mapping class="com.hib.Userentity"/>
      <mapping resource="Userentity.hbm.xml"/>

  </session-factory>
</hibernate-configuration>

6、插入数据测试

package com.hib;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.jupiter.api.Test;

public class Demo {

    @Test
    public void instrt() {
        Userentity userentity = new Userentity("我的1",15);

        //2.创建SessionFactory工厂
        SessionFactory factory = new Configuration().configure().buildSessionFactory();

        //3.创建Session对象
        Session session = factory.openSession();

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

        //5.执行添加操作
        session.save(userentity);

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

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

相关文章

网友评论

      本文标题:Idea Hibernate

      本文链接:https://www.haomeiwen.com/subject/qcvioktx.html