Hibernate搭建教程以及基础信息介绍

这个应该是刚学的时候写,本人有严重的拖拉癌,已到晚期,本着生无所恋,还是念念叨叨滴写下,以此为纪。

搭建的简单步骤:数据库-->表-->实体java类-->类对应的hbm.xml或annotation--->hibernate.cfg.xml配置文件--->junit test case(测试)

1.先说一哈子自己对hibernate的拙见

  • hibernate自动连接数据库(只需要在hibernate.cgf.xml中做简单配置);
  • 对于model做了映射,hibernate通过session对数据库中的表进行操作(crub);
  • 相比较于手动连接数据库写jdbc,进行增删改查的操作,简单得多。


2.hibernate搭建以及学习需要准备

  • hibernate官方api ,hibernate annotation api,j2ee的javax.persistence
  • hibernate运行需要的核心的jar (hibernate/lib/require),数据库驱动包(mysql-connector-java-5.0.8-bin.jar)
  • junit 测试使用jar  :junit-4.8.2.jar
  • log4j :slf4j-api-1.7.13.jar |slf4j-log4j12-1.7.13.jar  (sfl类似jdbc是规则,log4j)
  • 在hibernate文档下拷贝hibernate.cfg.xml到src的根目录
  • 在log4j文档下拷贝log4j.properties到src的根目录


3.hibernate.cfg.xml 内容

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>        
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernatetest</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
		
        <!--   
        <mapping resource="com/hibernate/Student.hbm.xml"/>
        <mapping resource="com/hibernate/StuIdCard.hbm.xml"/>-->
		<mapping class="com.hibernate.Husband"/>
		<mapping class="com.hibernate.Wife"/>

    </session-factory>

</hibernate-configuration>

  • dialect:不同数据库对应的方言不一样。根据实际情况查找hibernate api(中文api说明详细)
  • current_session_context_class:thread 
  • show_sql:输出控制语句到console
  • format_sql:格式化输出的sql
  • hbmsddl.auto:控制数据库表的创建


4.log4j.properties内容

### 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:%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=trace

log4j.logger.org.hibernate.tool.hbm2ddl=debug  控制台显示ddl语句



5.创建实体类,实现映射关系

  • 映射关系:xml实现或annotation


注意:

  1. 本文中没有具体的实例,只记载搭建过程中需要加载的主要内容以及注意事项,能做到提醒自己。
  2. JPA(Java Persistence API)Java持久性api,这是一个规范 hibernate实现这个规范hibernate-annotation也实现jpa这个规范
  3. |slf4j nodep  |  log4j |jdk logging api | apache commons-log  |  都实现sfl 规范
  4. 常见的O-R  mapping 框架  :hibernate ,ibatis ,openJPA
  5. junit 测试不报异常,添加 try catch 或者写main方法调用beforclass()
  6. hibernate.cfg.xml数据库的配置信息,在hibernate官方文档project-->etc->hibernate.properties



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值