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


好久没有碰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最基础的应用就结束了,本实例源码点击下载


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值