现在的位置: 首页 > 综合 > 正文

第一个hibernate例子的实现

2014年02月06日 ⁄ 综合 ⁄ 共 4551字 ⁄ 字号 评论关闭

记第一个Hibernate的实现

废话不多说,直接上代码!

1、新建java project导入所需jar包,

(注:文中可以使用自己建的hibernate library,也可以直接导入jar包)

2、以student类来演示不使用annotation的方式,以文件目录为顺序的代码:

StudentTest 类:

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.bjsxt.hibernate.Student;

public class StudentTest {

public static void main(String[] args) {

Student student = new Student();

student.setId(1);

student.setName("yuyu");

student.setAge(23);

Configuration cfg = new Configuration();

SessionFactory sf = cfg.configure().buildSessionFactory();

Session session =  sf.openSession();

session.beginTransaction();

session.save(student);

session.getTransaction().commit();

session.close();

sf.close();

}

}

Student 类:

package com.bjsxt.hibernate;

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.hbm.xml的配置:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.bjsxt.hibernate.Student">

<id name="id" />

<property name="name" />

<property name="age" />

    </class>

</hibernate-mapping>

Hibernate.cfg.xml的配置:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://hibernate.sourceforge.net/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">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.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/bjsxt/hibernate/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

3、以teacher类来演示使用annotation的方式,以文件目录为顺序的代码:

(注意:在导入jar包的时候比上个工程多导入了一个jar包,如图所示最下面的jar包)

TeacherTest 类:

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.cfg.Configuration;

import com.bjsxt.hibernate.Teacher;

public class TeacherTest {

public static void main(String[] args) {

Teacher teacher = new Teacher();

teacher.setId(1);

teacher.setName("yaya");

teacher.setAge(23);

teacher.setTitle("高级教师");

Configuration cfg = new AnnotationConfiguration();

SessionFactory sf = cfg.configure().buildSessionFactory();

Session session =  sf.openSession();

session.beginTransaction();

session.save(teacher);

session.getTransaction().commit();

session.close();

sf.close();

}

}

Teacher 类:

package com.bjsxt.hibernate;

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity

public class Teacher {

private int id;

private String name;

private int age;

private String title;

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

@Id

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;

}

}

Hibernate.cfg.xml的配置:

仅仅将上个工程的

<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>

改为:

<mapping class="com.bjsxt.hibernate.Teacher"/>

4、工程中可能用到的sql语句,在mysql建表:

create database hibernate;

drop table student;

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

select * from student;

drop table teacher;

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

select * from teacher;

OK,收工!

【上篇】
【下篇】

抱歉!评论已关闭.