首页 > 代码库 > Hibernate5-简单的增删改查
Hibernate5-简单的增删改查
1.创建项目,项目名称hibernatedemo1,目录结构如图所示
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(int fid, String name) { super(); this.fid = fid; 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; } }
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> <!-- 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.Query; 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 addForum(){ Configuration configuration = new Configuration(); SessionFactory sf = configuration.configure().buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); tx.begin(); Forum forum = new Forum(); forum.setName("forumdemo"); session.save(forum); tx.commit(); session.close(); } /* * 根据ID查询 */ @Test public void findForumById(){ Configuration configuration = new Configuration(); SessionFactory sf = configuration.configure().buildSessionFactory(); Session session = sf.openSession(); Query query = session.createQuery("from Forum where fid = 57");//Forum是类名不是表名 Forum forum = (Forum) query.uniqueResult(); session.close(); System.out.println(forum.getName()); } /* * 更新Forum信息 */ @Test public void modifyForum(){ Configuration configuration = new Configuration(); SessionFactory sf = configuration.configure().buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); tx.begin(); Query query = session.createQuery("from Forum where fid = 57"); Forum forum = (Forum) query.uniqueResult(); forum.setName("modify"); session.persist(forum); tx.commit(); session.close(); } /* * 删除 */ @Test public void removeForum(){ Configuration configuration = new Configuration(); SessionFactory sf = configuration.configure().buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); tx.begin(); Query query = session.createQuery("from Forum where fid = 57"); Forum forum = (Forum) query.uniqueResult(); session.delete(forum); tx.commit(); session.close(); } }
本文出自 “素颜” 博客,谢绝转载!
Hibernate5-简单的增删改查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。