Hibernate入门之helloworld

2025-11-24 21:04:24

1、hibernate.cfg.xml

<?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.username">root</property>

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

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql:///hibernate5</property>

<!-- 配置 hibernate 的基本信息 -->

<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

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

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

<property name="hbm2ddl.auto">update</property>

<!-- 指定关联的 .hbm.xml 文件 -->

<mapping resource="com/atguigu/hibernate/helloworld/News.hbm.xml"/>

</session-factory>

</hibernate-configuration>

2、News.java

package com.atguigu.hibernate.helloworld;

import java.sql.Date;

public class News {

private Integer id;

private String title;

private String author;

private Date date;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public Date getDate() {

return date;

}

public void setDate(Date date) {

this.date = date;

}

public News(String title, String author, Date date) {

super();

this.title = title;

this.author = author;

this.date = date;

}

public News() {

// TODO Auto-generated constructor stub

}

@Override

public String toString() {

return "News [id=" + id + ", title=" + title + ", author=" + author

+ ", date=" + date + "]";

}

}

3、News.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

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

<hibernate-mapping>

    <class name="com.atguigu.hibernate.helloworld.News" table="NEWS">

    

        <id name="id" type="java.lang.Integer">

            <column name="ID" />

            <!-- 指定主键的生成方式 -->

            <generator class="native" />

        </id>

        

        <property name="title" type="java.lang.String">

            <column name="TITLE" />

        </property>

        

        <property name="author" type="java.lang.String">

            <column name="AUTHOR" />

        </property>

        

        <property name="date" type="java.sql.Date">

            <column name="DATE" />

        </property>

        

    </class>

</hibernate-mapping>

4、HibernateTest

package com.atguigu.hibernate.helloworld;

import java.sql.Date;

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;

public class HibernateTest {

@Test

public void test() {

// System.out.println("test...");

//1. 创建一个 SessionFactory 对象

SessionFactory sessionFactory = null;

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

ServiceRegistry serviceRegistry = 

new ServiceRegistryBuilder().applySettings(configuration.getProperties())

                           .buildServiceRegistry();

sessionFactory = configuration.buildSessionFactory(serviceRegistry);

//2. 创建一个 Session 对象

Session session = sessionFactory.openSession();

//3. 开启事务

Transaction transaction = session.beginTransaction();

//4. 执行保存操作

News news = new News("Java", "ATGUIGU", new Date(new java.util.Date().getTime()));

session.save(news);

//5. 提交事务 

transaction.commit();

//6. 关闭 Session

session.close();

//7. 关闭 SessionFactory 对象

sessionFactory.close();

}

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢