Hibernate快速入门
下載hibernate
documentation目錄:存放hibernate的相關(guān)文件與API
lib目錄:存放hibernate編譯和運(yùn)行所依賴的jar包,其中required子目錄下包含了運(yùn)行hibernate項(xiàng)目必須的jar包
project目錄:存放hibernate各種相關(guān)的源代碼與資源.
在lib/required目錄中,包含的jar包
創(chuàng)建數(shù)據(jù)庫(kù)與表
創(chuàng)建實(shí)體類
public class Customer {private int id;private String name;private String address;private String sex;public Customer() {super();}public Customer(String name, String address) {super();this.name = name;this.address = address;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";}}導(dǎo)入hibernate框架相關(guān)依賴jar包
導(dǎo)入lib/required下所有的jar
導(dǎo)入數(shù)據(jù)庫(kù)的驅(qū)動(dòng)jar包
日志相關(guān)jar包
將hibernate/project/etc/log4j.properties文件導(dǎo)入到工程src下.
# # Hibernate, Relational Persistence for Idiomatic Java # # License: GNU Lesser General Public License (LGPL), version 2.1 or later. # See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. #### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file hibernate.log ### #log4j.appender.file=org.apache.log4j.FileAppender #log4j.appender.file.File=hibernate.log #log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=warn, stdout#log4j.logger.org.hibernate=info log4j.logger.org.hibernate=debug### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug### log just the SQL #log4j.logger.org.hibernate.SQL=debug### log JDBC bind parameters ### log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=debug### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug### log HQL parse trees #log4j.logger.org.hibernate.hql=debug### log cache activity ### #log4j.logger.org.hibernate.cache=debug### log transaction activity #log4j.logger.org.hibernate.transaction=debug### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=traceHibernate的相關(guān)配置文件
有兩種:
1.xxx.hbm.xml 它主要是用于描述類與數(shù)據(jù)庫(kù)中的表的映射關(guān)系.
2.hibernate.cfg.xml 它是hibernate框架核心配置文件。
映射配置文件
位置:它要與實(shí)體類在同一個(gè)包下.
名稱 :類名.hbm.xml
約束:
約束文件可以在hibernate的核心的jar包下的org.hibernate包下查找到
Customer.hbm.xml配置文件的內(nèi)容
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.nwtxxb.domain"><!-- name屬性它是實(shí)體類的全名 table 表的名稱 catalog 數(shù)據(jù)庫(kù)名稱 --><class name="Customer" table="t_customer"catalog="hibernateTest"><!-- id它是用于描述主鍵 --><id name="id" column="id" type="int"> <!-- java數(shù)據(jù)類型 --><!-- 主鍵生成策略 --><generator class="native"></generator></id><!-- 使用property來(lái)描述屬性與字段的對(duì)應(yīng)關(guān)系 --><property name="name" column="name" length="20" type="string"></property> <!-- hibernate數(shù)據(jù)類型 --><property name="address"><column name="address" length="50" sql-type="varchar(50)"></column> <!-- sql數(shù)據(jù)類型 --></property><property name="sex" column="sex" length="20"></property></class> </hibernate-mapping>核心配置文件
它主要是hibernate框架所使用的,它主要包含了連接數(shù)據(jù)庫(kù)相關(guān)信息,hibernate相關(guān)配置等。
位置:在src下創(chuàng)建一個(gè)hibernate.cfg.xml
約束:
約束文件所在位置:hiberante核心jar包下的org.hibernate包下
在這個(gè)文件中如何配置?
可以參考 hibernate-release-5.0.7.Final\project\etc\hibernate.properties文件
測(cè)試
public class HibernateTest1 {// 保存一個(gè)Customer@Testpublic void saveCustomerTest() {// 創(chuàng)建一個(gè)CustomerCustomer c = new Customer();c.setName("張三");c.setAddress("北京");c.setSex("男");// 使用hibernate的api來(lái)完成將customer信息保存到mysql中操作Configuration config = new Configuration().configure(); // 加載hibernate.cfg.xmlSessionFactory sessionFactory = config.buildSessionFactory();Session session = sessionFactory.openSession(); // 相當(dāng)于得到一個(gè)Connection。// 開啟事務(wù)Transaction transaction = session.beginTransaction();// 操作session.save(c);// 事務(wù)提交transaction.commit();session.close();sessionFactory.close();} }總結(jié)
以上是生活随笔為你收集整理的Hibernate快速入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring声明式事务管理
- 下一篇: Hibernate执行原理总结