您的位置:首页 > 其它

Hibernate最基础的项目搭建与配置

2016-10-18 17:08 351 查看
好久没有碰SSH相关的代码了,许多都生疏了,重新温习一下并记录过程。

  本实例中所用hibernate版本为4.3.5,可以直接下载hibernate-release-4.3.5.Final.zip解压出来,里面包含hibernate所必须的jar以及相关的文档,选择hibernate-release-4.3.5.Final\lib\required下的jar;需要的jar还包括mysql的mysql-connector-java-5.1.21.jar。

1.hibernate-release-4.3.5.Final.zip 可以从官网或者csdn里下载,也可以点击下载

2.创建实例所需的数据表

1)创建数据库

create database hibernate_01;


2)切换进入数据库

use hibernate_01;


3)创建数据表

create table student(id int primary key, name varchar(50), age int, sex varchar(2));
 //主键暂时设置成自己手动输入

3.创建java project,,项目名为Hibernate_01,引入所需jar,hibernate-release-4.3.5.Final\lib\required下的所有jar以及mysql-connector-java-5.1.21.jar。

4.创建hibernate.cfg.xml配置文件

最好是直接从hibernate-release-4.3.5.Final/documentation/manual/en-US/html_single/index.html中拷贝,在网页中输入hibernate.cfg.xml查找复制粘贴即可,熟练了也可以自己手写,修改成mysql对应的信息(数据库方言,用户名,密码,数据库名等),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>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_01</property>
<property name="connection.username">root</property>
<property name="connection.password">root</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/bobo/app/model/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>


5.创建实体类Student.java

package com.bobo.app.model;

public class Student {
private int id;
private String name;
private int age;
private String sex;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

}


6.创建实体映射文件Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.bobo.app.model">
<class name="Student" table="student">
<id name="id" column="id"></id>
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="int"></property>
<property name="sex" column="sex" type="string"></property>
</class>
</hibernate-mapping>


7.创建测试文件StudentTest.java

package com.bobo.app;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.bobo.app.model.Student;

public class StudentTest {
public static void main(String[] args) {
Student obj = new Student();
obj.setId(1);
obj.setName("jack");
obj.setAge(25);
obj.setSex("男");

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
session.close();
sf.close();

}

}


8.运行程序,sql查询测试证明插入数据成功。

select *  from student;


9.改进代码

上面的代码有警告提示:The method buildSessionFactory() from the type Configuration is deprecated;hibernate3以前用的方法buildSessionFactory在4以后的不同版本有所不同,本版本下修改为:

package com.bobo.app;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.bobo.app.model.Student;

public class StudentTest {
public static void main(String[] args) {
Student obj = new Student();
obj.setId(2);
obj.setName("jack");
obj.setAge(25);
obj.setSex("男");

Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session session = sf.openSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
session.close();
sf.close();

}

}



至此,hibernate最基础的应用就结束了,本实例源码点击下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate