第一个Hibernate项目
一、構(gòu)建Hibernate項(xiàng)目
????1.新建Java項(xiàng)目HibernateDemo1
????2.導(dǎo)入Hibernate下的jar包(lib->required下的所有jar包)+jdbc驅(qū)動(dòng)包
????3.導(dǎo)入hibernate.cfg.xml文件到src目錄下(在Hibernate文件目錄中搜索*.cfg.xml)
???? 配置該文件如下: ?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ????????<!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.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> ????????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ????????????<property?name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property> ????????????<property?name="hibernate.connection.username">root</property> ????????<property?name="hibernate.connection.password">root</property> ????????<property?name="hibernate.hbm2ddl.auto">update</property> ????????<property?name="hibernate.show_sql">true</property> ????????<property?name="hibernate.format_sql">true</property> ????????<mapping?resource="com\eduask\pojo\Person.hbm.xml"?/> ????????</session-factory> ????????</hibernate-configuration> |
? ? ?4.建立mysql數(shù)據(jù)庫(kù)hibernate_db
????5.在src目錄下建兩個(gè)包c(diǎn)om.eduask.pojo、com.eduask.test
???? ?pojo包下建一個(gè)Person類(Person.java),內(nèi)容如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package?com.eduask.pojo; import?java.util.Date; public?class?Person?{ ????private?Integer?id; ????private?String?name; ????private?int?password; ????private?Date?birthday; ????? ????public?Person()?{} ????public?Person(String?name,?int?password,?Date?birthday)?{ ????????super(); ????????this.name?=?name; ????????this.password?=?password; ????????this.birthday?=?birthday; ????} ????@Override ????public?String?toString()?{ ????????return?"Person?[id="?+?id?+?",?name="?+?name?+?",?password="?+?password?+?",?birthday="?+?birthday?+?"]"; ????} ????public?Integer?getId()?{ ????????return?id; ????} ????public?void?setId(Integer?id)?{ ????????this.id?=?id; ????} ????public?String?getName()?{ ????????return?name; ????} ????public?void?setName(String?name)?{ ????????this.name?=?name; ????} ????public?int?getPassword()?{ ????????return?password; ????} ????public?void?setPassword(int?password)?{ ????????this.password?=?password; ????} ????public?Date?getBirthday()?{ ????????return?birthday; ????} ????public?void?setBirthday(Date?birthday)?{ ????????this.birthday?=?birthday; ????} ????? ????? } |
????pojo包下引入xml文件Person.hbm.xml,(Hibernate包中搜索),修改內(nèi)容如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 | <?xml?version="1.0"?> <!DOCTYPE?hibernate-mapping?SYSTEM?"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"?> <hibernate-mapping> ????<class?name="com.eduask.pojo.Person"?table="t_person"> ????????<id?name="id"> ????????????<generator?class="native"/> ????????</id> ????????<property?name="name"?column="t_name"></property> ????????<property?name="password"?length="6"></property> ????????<property?name="birthday"></property> ????</class> </hibernate-mapping> |
????test包中新建測(cè)試類HibernateTest.java,內(nèi)容如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package?com.eduask.test; import?java.util.List; import?org.hibernate.Criteria; import?org.hibernate.Session; import?org.hibernate.SessionFactory; import?org.hibernate.Transaction; import?org.hibernate.cfg.Configuration; import?com.eduask.pojo.Person; public?class?HibernateTest?{ ????public?static?void?main(String[]?args)?{ ????????Configuration?config?=?new?Configuration().configure(); ????????SessionFactory?factory?=?config.buildSessionFactory(); ????????Session?session?=?factory.openSession(); ????????Transaction?tx?=?session.beginTransaction(); ????????Person?p?=?new?Person("admin",123456,new?java.util.Date());? ????????Criteria?c?=?session.createCriteria(Person.class); ????????List<Person>?lists?=?c.list(); ????????System.out.println(lists.get(0).getName()); ????????tx.commit(); ????????session.close(); ????????factory.close(); ????} } |
????運(yùn)行測(cè)試類,結(jié)果如下:
Hibernate:?
? ? insert?
? ? into
? ? ? ? t_person
? ? ? ? (t_name, password, birthday)?
? ? values
? ? ? ? (?, ?, ?)
八月 11, 2016 5:17:14 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql:///hibernate_db]
同時(shí)在數(shù)據(jù)庫(kù)中可以看到t_person表已被創(chuàng)建以及插入的響應(yīng)數(shù)據(jù)。
本文轉(zhuǎn)自yeleven 51CTO博客,原文鏈接:http://blog.51cto.com/11317783/1836998
總結(jié)
以上是生活随笔為你收集整理的第一个Hibernate项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到母牛下崽是什么寓意
- 下一篇: 创建邮箱过程中的问题及解决办法