Hibernate的CRUD配置及简单使用

参考博客:https://blog.csdn.net/qq_38977097/article/details/81326503

1.首先是jar包,可以在官网下载。

或者点击下面链接下载

链接:https://pan.baidu.com/s/1pW3LHu18nK0E6TzGxHBafg
提取码:cf2o
百度网盘下载的文件中,log文件放在src目录下

2.配置数据库连接的文件,放在src下面hibernate.cfg.xml,我使用的为mysql,如果是sqlserver修改对应的url和driver class即可。

<?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>
        <!-- 配置关于数据库连接的四个项:driverClass  url username password -->
<!--         <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> -->
<!--         <property name="hibernate.connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Javaweb</property> -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/javaweb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>

        <!-- 可以将向数据库发送的SQL语句显示出来 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 格式化SQL语句 -->
        <property name="hibernate.format_sql">true</property>

        <!-- hibernate的方言 -->    
<!--         <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>  -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property> 
        <!-- 配置hibernate的映射文件所在的位置 -->
        <mapping resource="Model/Student.hbm.xml" />
    </session-factory>
</hibernate-configuration>

3.配置数据库对应表格的属性,其中需要一个Student.java,对应一个mapping,路径在hibernate.cfg.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="Model">
    <!-- 
        name:即实体类的全名
        table:表名
     -->
    <class name="Student" table="t_student" >
        <id name="id" column="id"> 
            <generator class="native"></generator>
        </id>
    
<property name="name" column="name" length="50"></property> <property name="xuehao" column="xuehao" length="50"></property> <property name="sex" column="sex" length="50"></property> <property name="shengri" column="shengri" length="50"></property> <property name="zhuzhi" column="zhuzhi" length="50"></property> </class> </hibernate-mapping>

需要在property中配置实体类对应的属性,即列名

4.下一步就是数据库操作,首先创建一个工具类

HibernateUtils.java
package Util;

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

public class HibernateUtils {
    private static SessionFactory sessionFactory;

    static {
        sessionFactory = new Configuration() 
                .configure() 
                .buildSessionFactory(); 
    }


    //获取全局唯一的SessionFactory
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    /从全局唯一的SessionFactory中打开一个Session
    public static Session openSession() {
        return sessionFactory.openSession();
    }

}

5.对数据库的增删改查

StudentDao.java

package Dao;

import java.util.List;

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

import Model.Student;
import Util.HibernateUtils;

public class StudentDao {
    public void add(Student student) {
        // 使用Hibernate的API来完成将Customer信息保存到mysql数据库中的操作
        Session session = HibernateUtils.openSession();
        try {
            Transaction tx = session.beginTransaction(); // 开启事务
            session.save(student);
            tx.commit(); // 提交事务
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); // 回滚事务
            throw e;
        } finally {
            session.close(); // 关闭session
        }
    }
    
    public void delete(int id) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Object student = session.get(Student.class, id); // 要先获取到这个对象
            session.delete(student); // 删除的是实体对象

            tx.commit();
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
    
    public void update(Student student) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.update(student);// 操作
            tx.commit();
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
    public Student load(int id) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Student student = (Student) session.get(Student.class, id);// 操作
            tx.commit();
            return student;
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
    
    public List<Student> load() {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            // 方式一:使用HQL语句
            List<Student> list = session.createQuery("FROM Student").list(); // 使用HQL查询

            tx.commit();
            return list;
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
    

}

这样基本的配置及方法已经完成,只要调用对应的方法即可完成crud

 

转载于:https://www.cnblogs.com/wys-373/p/10891214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值