02、Hibernate开发步骤
1、创建hibernate配置文件(hibernate.cfg.xml)
<?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="connection.username">root</property> <property name="connection.password">root123</property> <property name="connection.driver_class">com.mysql.jdbc.driver</property> <property name="connection.url">jdbc:mysql:///hibernate5</property>
<!-- 配置 hibernate 的基本信息 --> <!-- hibernate 指定数据库所使用的 sql 方言 --> <property name="dialect">org.hibernate.dialect.mysqlinnodbdialect</property>
<!-- 指定程序运行时是否在控制台输出 sql 语句 --> <property name="show_sql">true</property>
<!-- 指定是否对输出 sql 语句进行格式化 --> <property name="format_sql">true</property>
<!-- 指定程序运行时是否在数据库自动生成数据表 --> <property name="hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml 文件 --> <mapping resource="csah/com/cnblogs/www/news.hbm.xml" />
</session-factory> </hibernate-configuration> |
1)问题:生成cfg.xml时候弹出右下角内容,然后按finish一直无反应怎么办?? 答:hibernate版本问题,就是第三行hibernate version那个选择低一点的版本 我看jar包是4.3.x的,我选了4.3的就ok了 |
2、创建持久化类
package csah.com.cnblogs.www;
import java.util.date;
public class news { private integer id; private string title; private string author; private date date; public date getdate() { return date; } public news(string title, string author, date date) { super(); this.title = title; this.author = author; this.date = date; } public void setdate(date date) { 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 news() { } @override public string tostring() { return "news [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; } } |
3、创建对象-关系映射文件(*.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"> <!-- generated 2020-4-25 2:28:16 by hibernate tools 3.5.0.final --> <hibernate-mapping package="csah.com.cnblogs.www"> <class name="news" table="news"> <id name="id" type="java.lang.integer"> <column name="id" /> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <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.util.date"> <column name="date" /> </property> </class> </hibernate-mapping> |
1)问题:ids for this class must be manually assigned before calling save(): csah.com.cnblogs.www.news 答:我们只需要将<generator class="assigned " />设置为<generator class="native" /> |
4、通过hibernate api编写访问数据库代码
package csah.com.cnblogs.www;
import static org.junit.jupiter.api.assertions.*;
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.jupiter.api.test;
import csah.com.cnblogs.www.*;
class hibernatetest {
@test public void test() {
system.out.println("test1...");
//1. 创建一个 sessionfactory 对象 sessionfactory sessionfactory = null; system.out.println("test2...");
//1). 创建 configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息 configuration configuration = new configuration().configure(); system.out.println("test3...");
//4.0 之前这样创建 // sessionfactory = configuration.buildsessionfactory();
//2). 创建一个 serviceregistry 对象: hibernate 4.x 新添加的对象 //hibernate 的任何配置和服务都需要在该对象中注册后才能有效. serviceregistry serviceregistry = new serviceregistrybuilder().applysettings(configuration.getproperties()) .buildserviceregistry(); system.out.println("test4...");
//3). sessionfactory = configuration.buildsessionfactory(serviceregistry); system.out.println("test5...");
//2. 创建一个 session 对象 session session = sessionfactory.opensession(); system.out.println("test6...");
//3. 开启事务 transaction transaction = session.begintransaction(); system.out.println("test7...");
//4. 执行保存操作 news news = new news("java", "atguigu", new date(new java.util.date().gettime())); system.out.println("test8...");
session.save(news); system.out.println("test9...");
//5. 提交事务 transaction.commit(); system.out.println("test10...");
//6. 关闭 session session.close(); system.out.println("test11...");
//7. 关闭 sessionfactory 对象 sessionfactory.close(); system.out.println("test12..."); }
} |
注意:上面的多个system.out.println()可以测试代码运行到哪一部分中断,如如果system.out.println("test9...");没有输出,那么我们只要找session.save(news)的问题即可。
1)问题:org.hibernate.invalidmappingexception: could not parse mapping document from resource com/cnblogs/com/csah/news.hbm.xml java.lang.classnotfoundexception: com.nblogs.com.csah.news 答:在news.hbm.xml中 class中的路径出现错误 没有找到'com.nblogs.com.csah.news'
2)问题:org.hibernate.mappingnotfoundexception: resource: com/cnblogs/com/csah/news.hbm.xml not found 答:指定关联的 .hbm.xml 文件路径没有找到
3)问题:duplicate entry 'java' for key 2 答:插入的数据已经存在 |