一、Hibernate的开发步骤
1、引入jar文件
2、配置
3、api
hibernate的映射文件的配置是不容易的,是重点学习的地方。
二、Hello Hibernate
1、数据库表准备
数据库名 :test
表:
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (`id` int(11) NOT NULL auto_increment,`name` varchar(255),`birthday` date,PRIMARY KEY (`id`)
);
ps:
主键id,int类型,必须设置 auto_increment 自增长。如果不设置,hibernate在操作mysql数据库保存数据时会报错误:
java.sql.SQLException: Field 'id' doesn't have a default value
2、引入jar文件
*hibernate3.6
hibernate3.jar核心 + required文件夹里的jar+ jpa文件夹里的jar + 数据库驱动包。
hibernate3.jar核心:hibernate-distribution-3.6.0.Final \ hibernate3.jar
required文件夹里的jar:hibernate-distribution-3.6.0.Final \ lib \ required
jpa文件夹里的jar:hibernate-distribution-3.6.0.Final \ lib \ jpa
数据库驱动包:本例用mysql。
--------------------必须的包如下9个:---------------------------
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.8-bin.jar
slf4j-api-1.6.1.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.8-bin.jar
slf4j-api-1.6.1.jar
3、写对象以及对象的映射
User.java
package hello.hibernate;import java.util.Date;public class User {private int userId;private String username;private Date birthday;//省略构造,get,set方法
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hello.hibernate"><class name="User" table="users"><!-- 主键映射 --><id name="userId" column="id"><generator class="native" /></id><!-- 非主键映射 --><property name="username" column="name"></property><property name="birthday" column="birthday"></property></class>
</hibernate-mapping>
PS:<hibernate-mapping package="hello.hibernate"> 的 package必须写,如果不写会报:
Could not parse mapping document from resource hello/hibernate/User.hbm.xml
4、src/hibernate.cfg.xml 主配置文件
1、数据库连接配置
2、加载所用的映射(*.hbm.xml)
<!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><!-- 数据库连接配置 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///test</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><!-- sql方言:告诉hibernate你在用哪个数据库 --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="show_sql">true</property><!-- 加载所用的映射(*.hbm.xml) --><mapping resource="hello/hibernate/User.hbm.xml"/></session-factory>
</hibernate-configuration>
5、测试类编写,使用api
@Testpublic void test(){User user=new User();user.setUsername("hello");user.setBirthday(new Date());//获取加载配置文件的管理类对象Configuration config=new Configuration();//读取配置文件,默认读取加载src/hibernate.cfg.xmlconfig.configure();//创建session工厂对象SessionFactory sf=config.buildSessionFactory();//创建会话对象,代表与数据库的一次会话Session session=sf.openSession();//开启事务Transaction tx=session.beginTransaction();//保存数据session.save(user);//提交事务tx.commit();//关闭会话session.close();//关闭会话工厂sf.close();}
效果:
控制台打印出hibernate执行了的sql语句
mysql数据库test--users表多了一条记录