1. Download the hibernate
hibernate official website.
2. Import all the jar packages under required and add a package to connect to the database (my database is mysql8.0.13), and the package is finished!
3. To configure, the first step is to create a hibernate.cfg.xml file under src, the name can not be wrong. Then introduce the constraint dtd to this xml file. (This dtd is in the package imported before)
Open:
add this paragraph to your hibernate.cfg.xml file
<?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>
<session-factory>
<!-- property 元素用于配置Hibernate中的属性
键:值
-->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">321123ww</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/RUNOOB?serverTimezone=UTC</property>
<!--表示那个数据库的方言org.hibernate.dialect.MySQL8Dialect这一块因为我的mysql是8.0.13所以这块是8-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- format_sql: 打印sql语句前,会将sql语句先格式化 -->
<property name="format_sql">true</property>
<!-- #4如何创建表(不重要)
create:每一次加载cfg.xml文件都将创建表,程序关闭时,表不进行删除 [初始化,测试时使用]
如果表存在则先删除后创建
create-drop:每一次加载cfg.xml文件都将创建表,程序关闭时,表进行删除
必须执行factory.close()才能删除
update:如果表不存在则创建,如果表存在,先回检查*.hbm.xml文件是否和表匹配,
如果不匹配将更新表结构(只添加,不删除)
validate:加载cfg.xml进效验,映射文件和数据表是否匹配,如果匹配正常操作,如果不匹配则抛出异常
### 显示的开发中先有的表,再有的映射文件
* 表 由DBA创建
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--将线程配置成Thread级别的 -->
<property name="hibernate.current_session_context_class">thread</property>
<!--绑定orm配置 -->
<mapping resource="hiubernate/domain/product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
After adding constraints
, create entities:
package hiubernate.domain;
public class Product {
private String pid;
private String pname;
private double p_price;
private int cid;
private String eve;
private String unit;
Create product.hbm.xml at the same level of this class
<?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="hiubernate.domain">
<class name="Product" table="product_copy2">
<id name="pid" column="pid" >
<!--数据库id和表的id -->
<generator class="uuid" /> <!--uuid是会自动给你一个uid,但是你的id必须为String类型 -->
</id>
<!--数据库和domain的对应 -->
<property name="pname" column="pname"></property>
<property name="p_price" column="p_price"></property>
<property name="cid" column="cid"></property>
<property name="eve" column="eve"></property>
<property name="unit" column="unit"></property>
</class>
</hibernate-mapping>
This one should also add a dependency, the same as the previous one! ! !
4. Test
Goodbye in advance and fill in the code afterwards! ! ! !