1.先建议一个数据表student
2.将hibernate架包导进项目
3.配置hibernate.cfg.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<!-- 该头文件和hibernate-configuration-3.0.dtd的一样 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 规定用哪一种数据库 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 数据库连接URL -->
<property name="connection.url">
jdbc:mysql://localhost:3306/student
</property>
<!-- 数据库的连接名和密码,没有密码不配置 -->
<property name="connection.username">root</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<!-- 格式化数据库语句 -->
<property name="format_sql">true</property>
<!-- 是否生成sql语句 -->
<property name="show_sql">true</property>
<!-- 映射资源 类与数据库相互映射 -->
<mapping resource="com/Student.hbm.xml" />
<mapping resource="com/StudentType.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<!-- 该头文件和hibernate-configuration-3.0.dtd的一样 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 规定用哪一种数据库 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 数据库连接URL -->
<property name="connection.url">
jdbc:mysql://localhost:3306/student
</property>
<!-- 数据库的连接名和密码,没有密码不配置 -->
<property name="connection.username">root</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<!-- 格式化数据库语句 -->
<property name="format_sql">true</property>
<!-- 是否生成sql语句 -->
<property name="show_sql">true</property>
<!-- 映射资源 类与数据库相互映射 -->
<mapping resource="com/Student.hbm.xml" />
<mapping resource="com/StudentType.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.写一个pojo javabean类
package com;
/**
* 简单的pojo类型javabean
* @author xieyongxue
*
*/
public class Student {
private String stuId;
private String stuName;
private String stuGener;
private String stuAge;
private StudentType studenttype;
public StudentType getStudenttype() {
return studenttype;
}
public void setStudenttype(StudentType studenttype) {
this.studenttype = studenttype;
}
public Student() {
}
public Student(String stuId) {
this.stuId = stuId;
}
public Student(String stuId, String stuName, String stuGener, String stuAge) {
this.stuId = stuId;
this.stuName = stuName;
this.stuGener = stuGener;
this.stuAge = stuAge;
}
public String getStuId() {
return this.stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getStuName() {
return this.stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuGener() {
return this.stuGener;
}
public void setStuGener(String stuGener) {
this.stuGener = stuGener;
}
public String getStuAge() {
return this.stuAge;
}
public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}
}
/**
* 简单的pojo类型javabean
* @author xieyongxue
*
*/
public class Student {
private String stuId;
private String stuName;
private String stuGener;
private String stuAge;
private StudentType studenttype;
public StudentType getStudenttype() {
return studenttype;
}
public void setStudenttype(StudentType studenttype) {
this.studenttype = studenttype;
}
public Student() {
}
public Student(String stuId) {
this.stuId = stuId;
}
public Student(String stuId, String stuName, String stuGener, String stuAge) {
this.stuId = stuId;
this.stuName = stuName;
this.stuGener = stuGener;
this.stuAge = stuAge;
}
public String getStuId() {
return this.stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getStuName() {
return this.stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuGener() {
return this.stuGener;
}
public void setStuGener(String stuGener) {
this.stuGener = stuGener;
}
public String getStuAge() {
return this.stuAge;
}
public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}
}
5.配置Student.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 此头文件与hibernate-mapping-3.0.dtd相同-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com">
<!-- 熟悉名与表的字段名 -->
<!-- catalog="student"可以理解为数据库 -->
<class name="Student" table="student" catalog="student">
<id name="stuId" type="java.lang.String">
<column name="STU_ID" length="50" />
<!-- generator class="incremet"ID生成器 -->
<generator class="assigned" />
</id>
<property name="stuName" type="java.lang.String">
<column name="STU_NAME" length="50" />
</property>
<property name="stuGener" type="java.lang.String">
<column name="STU_GENER" length="50" />
</property>
<property name="stuAge" type="java.lang.String">
<column name="STU_AGE" length="50" />
</property>
<many-to-one name="studenttype" column="stutypeid" class="StudentType" cascade="all" fetch="join"></many-to-one>
</class>
</hibernate-mapping>
<!-- 此头文件与hibernate-mapping-3.0.dtd相同-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com">
<!-- 熟悉名与表的字段名 -->
<!-- catalog="student"可以理解为数据库 -->
<class name="Student" table="student" catalog="student">
<id name="stuId" type="java.lang.String">
<column name="STU_ID" length="50" />
<!-- generator class="incremet"ID生成器 -->
<generator class="assigned" />
</id>
<property name="stuName" type="java.lang.String">
<column name="STU_NAME" length="50" />
</property>
<property name="stuGener" type="java.lang.String">
<column name="STU_GENER" length="50" />
</property>
<property name="stuAge" type="java.lang.String">
<column name="STU_AGE" length="50" />
</property>
<many-to-one name="studenttype" column="stutypeid" class="StudentType" cascade="all" fetch="join"></many-to-one>
</class>
</hibernate-mapping>
6.写一个简单的插入和查询测试类
package com;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateDemo {
public static void main(String[] args) {
//创建hibenate配置对象
Configuration conf=new Configuration();
//加载处理配置文件的hibenate.cfg.xml
conf.configure();
//生成session工厂对象
SessionFactory sf=conf.buildSessionFactory();
//session是持久化最关键的一个类
Session s=sf.openSession();
//开启事务
Transaction tx=s.beginTransaction();
//创建一个对象
Student student=new Student();
student.setStuId("12345522333");
student.setStuName("xieyongxue");
//执行插入
s.save(student);
//执行查询
Query q=s.createQuery("from Student");
ArrayList<Student> alg=(ArrayList<Student>)q.list();
for(Student stu:alg){
System.out.println(stu.getStuName());
}
tx.commit();
s.close();
sf.close();
}
}
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateDemo {
public static void main(String[] args) {
//创建hibenate配置对象
Configuration conf=new Configuration();
//加载处理配置文件的hibenate.cfg.xml
conf.configure();
//生成session工厂对象
SessionFactory sf=conf.buildSessionFactory();
//session是持久化最关键的一个类
Session s=sf.openSession();
//开启事务
Transaction tx=s.beginTransaction();
//创建一个对象
Student student=new Student();
student.setStuId("12345522333");
student.setStuName("xieyongxue");
//执行插入
s.save(student);
//执行查询
Query q=s.createQuery("from Student");
ArrayList<Student> alg=(ArrayList<Student>)q.list();
for(Student stu:alg){
System.out.println(stu.getStuName());
}
tx.commit();
s.close();
sf.close();
}
}