hibernate入门程序

2024-10-15 07:33:23

1、hibernate整个开发步骤如图所示:

hibernate入门程序

2、新建一个hibernate的maven项目,并增加hibernate与mysql驱动依赖包:<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.4.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.2</version> </dependency>

hibernate入门程序hibernate入门程序

3、配置hibernate.cfg.xml中数据库基本信息和hibernate基本属性信息:<?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.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- show_sql: 操作数据库时,会 向控制台打印sql语句 --> <property name="show_sql">true</property> <!-- format_sql: 打印sql语句前,会将sql语句先格式化 --> <property name="format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 数据库方言配置 org.hibernate.dialect.MySQLDialect (选择最短的) --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> </session-factory></hibernate-configuration>

hibernate入门程序

4、创建持久化类:package com.gwolf.domain;import java.util.Date;public class News { private Integer id; private String title; private String author; private Date date; public News() { } @Override public String toString() { return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; } public News(String title, String author, Date date) { this.title = title; this.author = author; this.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; } }

hibernate入门程序

5、使用hibernatetools创建对象关系映射文件.并且把映射文件加入到hibernate.cfg.xml文件中。<mapping resource="com/gwolf/domain/News.hbm.xml"/>

hibernate入门程序hibernate入门程序hibernate入门程序

6、创建hibernate单元测试类:package com.gwolf;import ja箪滹埘麽va.util.Date;import org.hib髫潋啜缅ernate.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.gwolf.domain.News;public class HibernateTest { @Test public void test() { //创建Configuration对象 Configuration configuration = new Configuration().configure(); //创建一个ServiceRegistry对象,hibernate的任何配置和服务都需要在该对象中注册后才有效 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder(). applySettings(configuration.getProperties()).buildServiceRegistry(); //创建一个SessionFactory对象 SessionFactory sessionFactory = configuration. buildSessionFactory(serviceRegistry); //创建一个Session对象 Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); //执行保存操作 News news = new News("java","gowlf",new Date()); session.save(news); //提交事务 transaction.commit(); session.close(); sessionFactory.close(); } }

hibernate入门程序

7、查看程序的执行结果,数据已经成功插入了。

hibernate入门程序hibernate入门程序
猜你喜欢