Hibernate環(huán)境搭建與配置方法(Hello world配置文件版)
本文實(shí)例講述了Hibernate環(huán)境搭建與配置方法。分享給大家供大家參考,具體如下:
1.下載hibernate jar包:hibernate-release-4.3.5.Final,導(dǎo)入必要的jar包,路徑為:hibernate-release-4.3.5.Final\lib\required。
包含的jar包有10個(gè)。
2.建立新的java項(xiàng)目。
3.學(xué)習(xí)自己建立User Library:
(a)項(xiàng)目右鍵——build path——configure build path——add library.
(b)選擇User-library,在其中新建library,命名為hibernate。
(c)在library中加入hibernate所需要的jar包(路徑為:hibernate-release-4.3.5.Final\lib\required),hello world就夠了,其他的還要加。
4.引入數(shù)據(jù)庫(kù)的jdbc驅(qū)動(dòng)。我用的mysql:mysql-connector-java-5.1.7-bin.jar
(a)創(chuàng)建數(shù)據(jù)庫(kù):
create database hibernate;
(b)切換數(shù)據(jù)庫(kù):
use hibernate;
(c)創(chuàng)建Student表:
create table Student(id int primary key,name varchar(20),age int);
5.建立hibernate的配置文件hibernate.cfg.xml,強(qiáng)烈建議在hibernate-release-4.3.5.Final\documentation\manual\en-US\html_single路徑下的幫助文檔中copy。
地點(diǎn):1.1.4. Hibernate configuration。 內(nèi)容修改后:
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">XXX</property> <property name="connection.password">XXXX</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/huxing/hibernate/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
建立Student類:
public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
建立Student的映射文件:Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.huxing.hibernate.model"> <class name="Student" table="student"> <id name="id" column="id"> </id> <property name="name" type="string" column="name"/> <property name="age" type="int" column="age"/> </class> </hibernate-mapping>
最后測(cè)試:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.huxing.hibernate.model.Student; public class StudentTest { public static void main(String[] args) { Student a = new Student(); a.setId(123); a.setAge(32); a.setName("hello hibernate!"); Configuration cfg = new Configuration(); SessionFactory cf = cfg.configure().buildSessionFactory(); Session session = cf.openSession(); session.beginTransaction(); session.save(a); session.getTransaction().commit(); session.close(); cf.close(); } }
希望本文所述對(duì)大家Hibernate框架程序設(shè)計(jì)有所幫助。
- Eclipse添加xml文件提示及Hibernate配置學(xué)習(xí)
- Hibernate 的原理與配置
- springmvc4+hibernate4分頁(yè)查詢功能實(shí)現(xiàn)
- spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開(kāi)發(fā)環(huán)境搭建圖文教程
- struts2.3.24+spring4.1.6+hibernate4.3.11+mysql5.5.25開(kāi)發(fā)環(huán)境搭建圖文教程
- SSH框架網(wǎng)上商城項(xiàng)目第1戰(zhàn)之整合Struts2、Hibernate4.3和Spring4.2
- hibernate4基本配置方式詳解
相關(guān)文章
詳細(xì)總結(jié)Java for循環(huán)的那些坑
在平常寫(xiě)代碼的過(guò)程中循環(huán)是不可避免的,雖然for的語(yǔ)法并不復(fù)雜,但是在開(kāi)發(fā)中還是會(huì)遇到一些坑,雖然大部分的坑都是自己的騷操作導(dǎo)致的.今天來(lái)總結(jié)一下for循環(huán)在開(kāi)發(fā)中可能遇到的坑,不要在同樣的問(wèn)題上再次犯錯(cuò).需要的朋友可以參考下2021-05-05java 利用HttpClient PostMethod提交json數(shù)據(jù)操作
這篇文章主要介紹了java 利用HttpClient PostMethod提交json數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Spring Cloud中FeignClient實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Spring Cloud中FeignClient實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Java通過(guò)HttpClient進(jìn)行HTTP請(qǐng)求的代碼詳解
Apache?HttpClient是一個(gè)功能強(qiáng)大且廣泛使用的Java庫(kù),它提供了方便的方法來(lái)執(zhí)行HTTP請(qǐng)求并處理響應(yīng)。本文將介紹如何使用HttpClient庫(kù)進(jìn)行HTTP請(qǐng)求,包括GET請(qǐng)求、POST請(qǐng)求、添加參數(shù)和請(qǐng)求體、設(shè)置請(qǐng)求頭等操作,需要的朋友可以參考下2023-05-05Java實(shí)題演練二叉搜索樹(shù)與雙向鏈表分析
這篇文章主要介紹了Java二叉搜索樹(shù)與雙向鏈表,總的來(lái)說(shuō)這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過(guò)程。希望通過(guò)這道題能給你帶來(lái)一種解題優(yōu)化的思路2022-12-12