Hibernate5-初始
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate5-初始相关的知识,希望对你有一定的参考价值。
1.创建项目,项目名称hibernatedemo2,目录结构如图所示
2.在项目中创建lib目录存储jar文件,目录结构如图所示
3.在src目录中创建实体Bean Forum,包名(com.mycompany.demo.bean),如图所示
4.实体Bean Forum的内容如下
package com.mycompany.demo.bean; public class Forum { private int fid; private String name; public Forum() { super(); } public Forum(String name) { super(); this.name = name; } public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + fid; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Forum other = (Forum) obj; if (fid != other.fid) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
5.在src目录中创建实体Bean Forum的映射文件Forum.hbm.xml,包名(com.mycompany.demo.bean),如图所示
6.映射文件Forum.hbm.xml的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.mycompany.demo.bean.Forum" table="hnsq_forum"> <meta attribute="class-description"> This class contains the forum detail. </meta> <id name="fid" type="int" column="fid"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> </class> </hibernate-mapping>
7.在src目录中创建Hibernate的配置文件hibernate.cfg.xml,如图所示
8.Hibernate的配置文件hibernate.cfg.xml的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.mysqlDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Assume test is the database name --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/b_shequ_two</property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"></property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 事务环境一个线程对一个事务 --> <property name="hibernate.current_session_context_class">thread</property> <!-- List of XML mapping files --> <mapping resource="com/mycompany/demo/bean/Forum.hbm.xml"/> </session-factory> </hibernate-configuration>
9.在项目中创建test目录存储测试文件,文件名称ManageForum,包名(com.mycompany.demo.bean),目录结构如图所示
10.ManageForum测试类的内容如下
package com.mycompany.demo.bean; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class ManageForum { @Test public void currentSession(){ //1.加载配置文件与映射文件 Configuration configure = new Configuration().configure(); //2.创建Session工厂对象 SessionFactory sessionFactory = configure.buildSessionFactory(); //3.生成Session对象 Session session = sessionFactory.getCurrentSession(); Transaction transaction = null; try { //4.开启事务 transaction = session.beginTransaction(); //5.执行操作 Forum forum = new Forum("getCurrentSession"); session.save(forum); //6.提交事务 transaction.commit(); } catch (Exception e) { //7.回滚事务 if(transaction != null){ transaction.rollback(); } e.printStackTrace(); } } }
本文出自 “素颜” 博客,谢绝转载!
以上是关于Hibernate5-初始的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 使用此代码片段为 iOS 应用程序初始化 SDK?
如何在使用cardview的片段中初始化gridlayoutmanager?
vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容
环境初始化 Build and Install the Apache Thrift IDL Compiler Install the Platform Development Tools(代码片段