一、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表多了一條記錄