初始hibernate 和环境搭建

时间:2023-03-08 16:50:56
初始hibernate 和环境搭建

hibernate是一个开源的数据持久化框架。

hibernate的优点:

  hibernate进行操作数据库提高了代码的开发速度,大大降低了代码量,降低了维护成本。

  hibernate支持许多面向对象的特性。使开发人员不必在面向业务的对象模型和面向数据库的关系模型之间来回切换。

  可移植性好 。系统不会绑定在某个特定的数据库上,想要更换只需更换hibernate的配置文件即可。

hibernate的缺点:

  不适合以数据为中心大量使用存储过程的应用。

  不适合大量的删除,修改和添加。

hibernate环境搭建:

1.引入jar包

初始hibernate 和环境搭建

Oraclejar需要配置本地仓库,因为maven没有Oracle的jar包,需要自己手动配置。

初始hibernate 和环境搭建

2.配置大配置hibernate.cfg.xml(第一次配置尽量名字写成 hibernate.cfg.xml)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!--大配置的根节点是 SessionFactory--> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@//192.168.19.129/orcl</property> <property name="connection.username">y2169</property> <property name="connection.password">y2169</property> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="show_sql">true</property> <!--format sql if or not--> <property name="hibernate.format_sql">true</property> <!--ddl:data definationLanguage 数据定义语言 自动生成CreateTable语句--> <!--从映射文件自动--> <property name="hbm2ddl.auto">update</property> <mapping resource="cn/happy/day01/entity/DeptMapper.xml"/> </session-factory> </hibernate-configuration>

 3.配置小配置文件(名字不是固定的)

初始hibernate 和环境搭建

<?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="cn.happy.day01.entity">
<class name="Dept" table="Dept" schema="y2169">
<!--和DB中主键列对应的类的映射配置-->
<id name="deptNo" column="deptNo">
<!--主键生成策略
native:由底层数据决定主键值
Mysql:自增 auto_increment
Oracle: 序列
hibernate_sequence 序列
-->
<generator class="native"/>
</id>
<!--普通的列-->
<property name="deptName" type="string" column="deptName"/>
</class> </hibernate-mapping>