利用hibernate实现crud的操作

hibernate.cfg.xml主要用途
告诉Hibernate连接数据库信息,用的什么样的数据库(方言)根据持久化类和映射文件生成表的策略。

步骤:
1、创建工程
2、导入jar包
3、配置文件hibernate.cfg.xml

Paste_Image.png

说明:
javassist包是用来创建代理对象的,代理对象的三种创建方式:
1、jdkproxy
2、cglib
3、javassist

jta-(java Transaction API) java的事物API,JTA允许应用程序执行分布式事务处理。比如银行转账功能;(事务涉及到的数据来自不同的JVM->分布式),是sun公司给分布式事务处理出来的规范。


Paste_Image.png

配置文件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>
 <!-- 
  一个sessionFactory代表数据库的一个连接
 -->
 <session-factory>
  <!-- 链接数据库的用户名 -->
  <property name="connection.username">root</property>
  <!-- 链接数据库的密码 -->
  <property name="connection.password">root</property>
  <!-- 链接数据库的驱动 -->
  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
  <!-- 链接数据库的url -->
  <property name="connection.url">
   jdbc:mysql://localhost:3306/itheima12_hibernate
  </property>

  <!-- 
   方言
   告诉hibernate使用什么样的数据库,hibernate就会在底层拼接什么样的sql语句
  -->
  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>

  <!-- 
   根据持久化类生成表的策略
   validate   通过映射文件检查持久化类与表的匹配
   update   每次hibernate启动的时候,检查表是否存在,如果不存在,则创建,如果存在,则什么都不做了
   create   每一次hibernate启动的时候,根据持久化类和映射文件生成表
   create-drop
  -->
  <property name="hbm2ddl.auto">update</property>
  <property name="show_sql">true</property>
  <mapping
   resource="com/itheima12/hibernate/domain/Person.hbm.xml" />
  <mapping
   resource="com/itheima12/hibernate/utils/Person.hbm.xml" />
 </session-factory>
</hibernate-configuration>

---------------------持久化类-------------------------
映射文件*.hbm.xml:建立从表到类的映射,建立从表的字段的名称到类的属性的名称映射;建立从表的字段的类型到类的属性的类型的映射。

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容