在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Hibernate环境搭建与配置方法(Hello world配置文件版)

2016-8-29 13:25| 发布者: zhangjf| 查看: 580| 评论: 0

摘要: 本文实例讲述了Hibernate环境搭建与配置方法。分享给大家供大家参考,具体如下: 1.下载hibernate jar包:hibernate-release-4.3.5.Final,导入必要的jar包,路径为:hibernate-release-4.3.5.Finallibrequired。 包 ...

本文实例讲述了Hibernate环境搭建与配置方法。分享给大家供大家参考,具体如下:

1.下载hibernate jar包:hibernate-release-4.3.5.Final,导入必要的jar包,路径为:hibernate-release-4.3.5.Finallibrequired。

包含的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.Finallibrequired),hello world就够了,其他的还要加。

4.引入数据库的jdbc驱动。我用的mysql:mysql-connector-java-5.1.7-bin.jar

(a)创建数据库:

  1. create database hibernate;
复制代码

(b)切换数据库:

  1. use hibernate;
复制代码

(c)创建Student表:

  1. create table Student(id int primary key,name varchar(20),age int);
复制代码

5.建立hibernate的配置文件hibernate.cfg.xml,强烈建议在hibernate-release-4.3.5.Finaldocumentationmanualen-UShtml_single路径下的帮助文档中copy。

地点:1.1.4. Hibernate configuration。 内容修改后:

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- Database connection settings -->
  8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
  10. <property name="connection.username">XXX</property>
  11. <property name="connection.password">XXXX</property>
  12. <!-- JDBC connection pool (use the built-in) -->
  13. <!--
  14. <property name="connection.pool_size">1</property>
  15. -->
  16. <!-- SQL dialect -->
  17. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  18. <!-- Enable Hibernate's automatic session context management -->
  19. <property name="current_session_context_class">thread</property>
  20. <!-- Disable the second-level cache -->
  21. <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  22. <!-- Echo all executed SQL to stdout -->
  23. <property name="show_sql">true</property>
  24. <!-- Drop and re-create the database schema on startup -->
  25. <!--
  26. <property name="hbm2ddl.auto">update</property>
  27. -->
  28. <mapping resource="com/huxing/hibernate/model/Student.hbm.xml"/>
  29. </session-factory>
  30. </hibernate-configuration>
复制代码

建立Student类:

  1. public class Student {
  2. private int id;
  3. private String name;
  4. private int age;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. }
复制代码

建立Student的映射文件:Student.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="com.huxing.hibernate.model">
  6. <class name="Student" table="student">
  7. <id name="id" column="id">
  8. </id>
  9. <property name="name" type="string" column="name"/>
  10. <property name="age" type="int" column="age"/>
  11. </class>
  12. </hibernate-mapping>
复制代码

最后测试:

  1. import org.hibernate.Session;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.Configuration;
  4. import com.huxing.hibernate.model.Student;
  5. public class StudentTest {
  6. public static void main(String[] args) {
  7. Student a = new Student();
  8. a.setId(123);
  9. a.setAge(32);
  10. a.setName("hello hibernate!");
  11. Configuration cfg = new Configuration();
  12. SessionFactory cf = cfg.configure().buildSessionFactory();
  13. Session session = cf.openSession();
  14. session.beginTransaction();
  15. session.save(a);
  16. session.getTransaction().commit();
  17. session.close();
  18. cf.close();
  19. }
  20. }
复制代码

希望本文所述对大家Hibernate框架程序设计有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-7 18:19

Copyright 2015-2025 djqfx

返回顶部