3.hello hibernate
生活随笔
收集整理的這篇文章主要介紹了
3.hello hibernate
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、Hibernate的開發(fā)步驟
1、引入jar文件 2、配置 3、api hibernate的映射文件的配置是不容易的,是重點學(xué)習(xí)的地方。二、Hello Hibernate
1、數(shù)據(jù)庫表準備
數(shù)據(jù)庫名 :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類型,必須設(shè)置 auto_increment 自增長。如果不設(shè)置,hibernate在操作mysql數(shù)據(jù)庫保存數(shù)據(jù)時會報錯誤: java.sql.SQLException: Field 'id' doesn't have a default value2、引入jar文件
*hibernate3.6hibernate3.jar核心 ?+ ?required文件夾里的jar+ ?jpa文件夾里的jar? + 數(shù)據(jù)庫驅(qū)動包。 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
數(shù)據(jù)庫驅(qū)動包:本例用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
3、寫對象以及對象的映射
User.java package hello.hibernate;import java.util.Date;public class User {private int userId;private String username;private Date birthday;//省略構(gòu)造,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、數(shù)據(jù)庫連接配置 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><!-- 數(shù)據(jù)庫連接配置 --><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你在用哪個數(shù)據(jù)庫 --><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();//創(chuàng)建session工廠對象SessionFactory sf=config.buildSessionFactory();//創(chuàng)建會話對象,代表與數(shù)據(jù)庫的一次會話Session session=sf.openSession();//開啟事務(wù)Transaction tx=session.beginTransaction();//保存數(shù)據(jù)session.save(user);//提交事務(wù)tx.commit();//關(guān)閉會話session.close();//關(guān)閉會話工廠sf.close();}效果: 控制臺打印出hibernate執(zhí)行了的sql語句 mysql數(shù)據(jù)庫test--users表多了一條記錄
總結(jié)
以上是生活随笔為你收集整理的3.hello hibernate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JobTracker和TaskTrack
- 下一篇: 深入理解Oracle的并行操作【好文认真