开始学hibernate学习了,要赶紧些:
1.第一个Hibernate程序:
建数据库:
create database Hiberinate;
建表:
create table (id int primary key,name varchar(10),age int);
建model类:
public class Student {
private int id;
private String name;
private int age;
/*
setters...getters...constructors...
*/
}
引入jar包:需要下载版本相符的core和annotation包,我的是hibernate-distribution-3.3.2.GA-dist.zip和hibernate-annotations-3.4.0.GA.zip
另外Hibernate用到slf4j作为记录日志的工具,所以还需要下载跟core包对应的slf4j包,这个版本需要的slf4j的版本是slf4j-1.5.8.zip
在不用到annotation的情况下,所需要引入的包有:
创建配置文件: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>
<!-- 数据库连接的配置,指定数据库驱动-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3309/Hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">java</property>
<!-- 默认指定的数据库连接池,暂时先不用-->
<!--<property name="connection.pool_size">1</property>-->
<!-- 指定sql方言,指使用的是哪个数据库下SQL语言,不同的数据库sql语句有不同,这里指定mysql -->
<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显示hibernate自动生成的sql语句 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--<property name="hbm2ddl.auto">update</property> -->
<!--下面指定model的映射文件,通过映射文件,才能将实体类映射到数据库中对应的表 -->
<mapping resource="com/zxf/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建model的映射文件: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 package="com.zxf.model"> 实体所在的包
<class name="Student" table="Student"> 实体的类名 和 对应的表名
<id name="id"></id> 指定主键 name是指实体类中的属性名
<property name="age"/> age和下面的name都对应实体类中的属性名,如果
<property name="name"/> 属性名和数据表中的列名相同则不用指定column,还可以指定type,指定数据库中的数据类型,
</class> 上面的table也可以省略,因为数据库中表名和类名一样
</hibernate-mapping>
注意!:hibernate.cfg.xml放在src根目录下,实体的映射文件放在跟实体类同一个包中
接下来创建junit单元测试:
package junit;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.zxf.model.Student;
public class Test01 {
@Test
public void hibernateTest01() {
Student s1 = new Student(1,20,"Jaon");//创建实体对象
Configuration cfg = new Configuration();//有默认的hibernate.cfg.xml创建配置文件对象
SessionFactory sf = cfg.configure().buildSessionFactory();//创建session的工厂类
Session session = sf.openSession();//得到一个session
session.beginTransaction();//开始事务
session.save(s1);//model的持久化,存到数据库
session.getTransaction().commit();//事务提交
}
}
如果不出意外,结果如下:
Hibernate: insert into Student (age, name, id) values (?, ?, ?)
这是控制台的信息,显示hibernate自动生成的sql语句