eclipse下搭建hibernate5.0环境的步骤(图文)
程序员文章站
2023-12-18 17:34:52
本文介绍了eclipse下搭建hibernate5.0环境的步骤,分享给大家,具体如下:
hibernate引入的jar包:hibernate-release-5...
本文介绍了eclipse下搭建hibernate5.0环境的步骤,分享给大家,具体如下:
- hibernate引入的jar包:hibernate-release-5.0.12.final.zip
- 数据库驱动:mysql-connector-java-5.1.46
二.安装hibernate插件
打开eclipse,点击help-->eclipse marketplace,如图输入:hibernate tools,再点击goa按钮,找到jboss tools
点击install安装
如图选择hibernate tools,点击confrm安装。安装完成后重启eclipse。
三. 创建工程
1.创建新项目hibernatedemo,在工程下建立lib文件夹。打开jar包的目录,导入lib/required下的和数据库的jar包,add to build path
在src下新建文件
点击next,默认文件名,点击next,如图配置数据库信息
选择utf-8编码方式,点击finish,生成的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="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.password">a123</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <property name="hibernate.connection.username">sherman</property> <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> </session-factory> </hibernate-configuration>
注意,把 < session-factory name ="mysql" > 的name属性去掉,否则报org.hibernate.engine.jndi.jndiexception异常,在该文件中添加一些配置,如图:
<?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.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.password">a123</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <property name="hibernate.connection.username">sherman</property> <!-- 配置数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.mysql5dialect</property> <!-- 控制台打印sql语句 --> <property name="show_sql">true</property> <!-- 格式化sql --> <property name="format_sql">true</property> <!--在启动时根据配置更新数据库 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置连接池的连接数 --> <property name="connection.pool_size">20</property> <!-- 注册实体映射类 --> <mapping class="com.gdut.app.entity.news"/> </session-factory> </hibernate-configuration>
在src下新建一个包com.gdut.app.entity,存放持久化类news,news类代码如下
package com.gdut.app.entity; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.table; @entity @table(name="news_info") public class news { @id @generatedvalue(strategy=generationtype.identity) private integer id; private string title; private string content; public news() { } public news(integer id, string title, string content) { this.id = id; this.title = title; this.content = content; } 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 getcontent() { return content; } public void setcontent(string content) { this.content = content; } @override public string tostring() { return "news [id=" + id + ", title=" + title + ", content=" + content + "]"; } }
编写测试类:
package com.gdut.app.entity; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; import org.junit.test; public class beantest { @test public void beantest() { // final standardserviceregistry serviceregistry = new standardserviceregistrybuilder() // .configure("hibernate.cfg.xml").build(); // // sessionfactory sf = new metadatasources(serviceregistry).buildmetadata().buildsessionfactory(); //两种方式都可以获取sessionfactory configuration cfg = new configuration().configure(); sessionfactory sf = cfg.buildsessionfactory(); session sess =sf.opensession(); transaction transaction = sess.begintransaction(); news n = new news(); n.setcontent("在广工毕业"); n.settitle("毕业季"); sess.save(n); transaction.commit(); sess.close(); } }
经过测试成功
或者通过映射文件
在com.gdut.app.entity包下简历一个news.hbm.xml映射配置文件,修改genarator的class属性为active
<?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 2018-5-22 23:45:23 by hibernate tools 3.5.0.final --> <hibernate-mapping> <class name="com.gdut.app.entity.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="content" type="java.lang.string"> <column name="content" /> </property> </class> </hibernate-mapping>
在hibernate.cfg.xml中配置
<mapping resource="com/gdut/app/entity/news.hbm.xml"/>
测试验证成功。
整个工程架构如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
eclipse下搭建hibernate5.0环境的步骤(图文)
-
eclipse下搭建hibernate5.0环境的步骤(图文)
-
Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)
-
使用eclipse搭建c/c++开发环境的详解步骤
-
使用eclipse搭建c/c++开发环境的详解步骤
-
Linux环境下搭建php开发环境的操作步骤
-
Windows7下安装 Nodejs 并在 WebStorm 9.0.1 下搭建编译 LESS 环境的图文教程
-
标准版Eclipse搭建PHP环境的详细步骤
-
ubuntu下Android开发环境的搭建:eclipse+SDK详细安装教程+常见问题及其解决方案
-
MAC下Android的Eclipse开发环境的搭建