第一个hibernate程序HelloWorldHibernate

时间:2023-03-09 22:07:23
第一个hibernate程序HelloWorldHibernate

HelloWorldHibernate步骤:

HelloWorld

1,新建java项目hibernate_0100_HelloWorld

2,学习User-library-hibernate,并加入相应的jar包

a)项目右键-build path-configure build path-add  library

b)选择User-library,在其中新建library,命名hibernate

c)在该library中加入hibernate所需的包

  1. /required必须的包
  1. Sl4j jar日志包
  1. bytecode生成二进制字节码需要的

3,引入mysql的JDBC驱动包

4,在mysql中建立对应的数据库及表

a)create  database  hibernate;

b)use  hibernate

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

5,建立hibernate配置文件hibernate.cfg.xnl

a),从参考文档中copy

b),修改对应的数据库连接

c),注释掉暂时用不上的内容

6,建立Student类

7,建立Student映射文件Student.hbm.xml

映射关系:

哪个类-----------------------哪个表

类属性----------------------表字段

a)参考文档

8,将映射文件加入到hibernate.cfg.xml

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

a)参考文档

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

项目架构:

第一个hibernate程序HelloWorldHibernate

package com.oracle.hibernate.model;

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;
}
}

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) -->
<!-- hibernate自带的连接池,实际开发一般用applicationserver本身用jndi注册在里面的连接池 -->
<!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect -->
<!-- 方言,hibernate已经统一了数据库的访问,他的HQL语句就是官方语言,但不过最终还是翻译成具体不同数据库的sql语句,(搜文档mysql)-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<!-- 3.2以后新加的内容,涉及到session。。。 -->
<!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache -->
<!-- 把二级缓存disable掉,优化hibernate -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<!-- 要不要打印生成的sql语句 -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<!-- hbm(hibernatemapping) ,ddl(建表语句。是否自动生成建表语句)-->
<!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/oracle/hibernate/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>

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"> <!-- 映射的是哪个package里的类 -->
<hibernate-mapping package="com.oracle.hibernate.model">
<!-- 类到表的映射 类名若和表名一致,table可以省略--> <class name="Student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class> </hibernate-mapping>

Test类:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.oracle.hibernate.model.Student; public class StudentTest { /**
* @param args
*/
public static void main(String[] args) {
Student s = new Student(); s.setId(3);
s.setName("student3");
s.setAge(3); /**
* Configuration.configure()读配置文件的。
*Document,File,Sting ,Url四种重载方法,默认是读取src下的hibernate.cfg.xml
*/
Configuration cfg = new Configuration(); /**
* cfg.configure()已经把hibernate.cfg.xml解析了,返回值还是Configuration,
* 是已经有了配置选项的Configuration对象。
* buildSessionFactory()暂时理解为产生Connection的一个工厂
*/
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();//打开新的Session /**
* 在hibernate里边的操作都应放在一个事务里
* session.beginTransaction()返回值Transaction,可以把他存在来。随便
*/
session.beginTransaction();
/**
* save时,先看s是Student类,然后去hibernate.cfg.xml配置文件找
* <mapping resource="XXX.hbm.xml"/>
* 看是否有这个配置文件,找到后,看他的映射关系,类和数据库表的映射关系,然后拼成sql语句
* 自动连数据库,自动生成preparedStatement,自动执行executeUpdate
*/
session.save(s); /**
* 拿到当前事务,提交
*/
session.getTransaction().commit(); session.close(); //关闭session
sf.close(); //关闭工厂
} }

注意:hibernate.cfg.xml名字不能改,约定俗成。Student.hbm.xml放在我们的Model类的包里。

session.save(s);方法执行图示:

第一个hibernate程序HelloWorldHibernate

save时,先看s是Student类,然后去hibernate.cfg.xml配置文件找 <mapping resource="com/oracle/hibernate/model/Student.hbm.xml"/>,

看是否有这个Student的类配置文件,找到后,看他的映射关系,类和数据库表的映射关系,类的属性和表字段的映射关系。然后拼成sql语句,自动连数据库,自动生成preparedStatement,自动执行executeUpdate,数据就保存到了数据库。

第一个hibernate程序HelloWorldHibernate

查找出单个对象,修改,删除代码如下:

//查询出id=1的Student
Student s = (Student)session.get(Student.class,1); System.out.println(s);
//修改信息
s.setAge(20);
session.update(s);
//删除一个对象
//session.delete(s); ts.commit(); if(session != null){
if(session.isOpen())
session.close();
}
sf.close();