国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Hibernate環境搭建與配置方法(Hello world配置文件版)

2019-11-26 14:29:31
字體:
來源:轉載
供稿:網友

本文實例講述了Hibernate環境搭建與配置方法。分享給大家供大家參考,具體如下:

1.下載hibernate jar包:hibernate-release-4.3.5.Final,導入必要的jar包,路徑為:hibernate-release-4.3.5.Final/lib/required。

包含的jar包有10個。

2.建立新的java項目。

3.學習自己建立User Library:

(a)項目右鍵――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.引入數據庫的jdbc驅動。我用的mysql:mysql-connector-java-5.1.7-bin.jar

(a)創建數據庫:

create database hibernate;

(b)切換數據庫:

use hibernate;

(c)創建Student表:

create table Student(id int primary key,name varchar(20),age int);

5.建立hibernate的配置文件hibernate.cfg.xml,強烈建議在hibernate-release-4.3.5.Final/documentation/manual/en-US/html_single路徑下的幫助文檔中copy。

地點:1.1.4. Hibernate configuration。 內容修改后:

<?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>

最后測試:

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(); }}

希望本文所述對大家Hibernate框架程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 视频| 文安县| 西青区| 文昌市| 涿鹿县| 定州市| 敦化市| 金川县| 竹山县| 靖州| 武清区| 泗洪县| 百色市| 蓬溪县| 桐柏县| 通榆县| 壶关县| 宁国市| 离岛区| 梁山县| 永年县| 加查县| 武宁县| 崇信县| 西丰县| 兰西县| 进贤县| 丹江口市| 静宁县| 腾冲县| 彰化县| 绥江县| 徐汇区| 酒泉市| 淄博市| 丘北县| 陆河县| 桂林市| 贡觉县| 清镇市| 龙里县|