1.导包
使用注解时,通用baseDao别忘了获得sessionFactory对象工厂,使用注解
2.导入约束https://pan.baidu.com/s/1pKHc6ojhttps://pan.baidu.com/s/1pKHc6oj
3.创建数据库
本例使用Navicat Mysql数据库进行测试
(1)cmd建库create database+库名/或者使用数据库操作建表
4.书写orm元数据(对象与表的映射配置文件
(1)创建一个实体类名+.hbm+.xml 建议这个文件的目录和实体类同一目录下
<hibernate-mapping>
<class name="cn.hd.bean.User" table="t_user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="sex" column="sex"></property>
<property name="age" column="age"></property>
<property name="balance" column="balance"></property>
</class>
</hibernate-mapping>
5.书写核心配置文件
名字必须为Hibernate.cfg.xml
文件的位置必须放在src目录下
(1)导入约束
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
(2)配置文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.isolation">4</property>
<mapping resource="cn/hd/bean/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
6.书写设置代码
public class Demo {
public static void main(String[] args) {
//读取配置文件
Configuration cfg = new Configuration();
cfg.configure();
//创建sessionFactory
SessionFactory sessionFactory = cfg.buildSessionFactory();
//获得一个session
Session session = sessionFactory.openSession();
//开启事务
Transaction transaction = session.beginTransaction();
User user = new User();
user.setName("小明");
user.setAge(20);
user.setBalance(1000);
user.setSex("男");
session.save(user);
//提交事务
transaction.commit();
//释放资源
session.close();
sessionFactory.close();
}
配置文件详解
1.映射文件
<!--下面class中的类的路径,下面的class name属性可以简写-->
<hibernate-mapping>
<!--映射类和数据库表之间的关系-->
<!--name属性是实体类名 写完整路径名-->
<!--table属性 数据库表名-->
<class name="cn.hd.bean.User" table="t_user">
<!--映射文件中必须拥有 主键-->
<id name="id" column="id">
<!--主键生成策略
identity:mysql自动递增
increment:
sequence:Oracle数据库中的自动递增
native:自动递增(3和1)
assigned
uuid
-->
<generator class="native"></generator>
</id>
<!--基本属性 在这里可以设置数据表属性-->
<property name="name" column="name"></property>
<property name="sex" column="sex"></property>
<property name="age" column="age"></property>
<property name="balance" column="balance"></property>
</class>
</hibernate-mapping>
2.核心配置文件详解
名字和位置都不能更改
<hibernate-configuration>
<session-factory>
<!-- 数据库的驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--数据库的url-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate01</property>
<!-- 数据库的账号-->
<property name="hibernate.connection.username">root</property>
<!--数据库的密码-->
<property name="hibernate.connection.password">root</property>
<!--方言 必须设置-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--在控制台显示sql语句-->
<property name="hibernate.show_sql">true</property>
<!--格式化sql语句输出-->
<property name="hibernate.format_sql">true</property>
<!--
1.update 如果映射文件和数据表保持一致就不修改,如果没有这个表 自动创建表
2.create 每次执行都会重新创建一个表
3.create-drop 先将原来表删除 然后重新创建
4.validate 只做校验 不修改表
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--事务的隔离级别
脏读
幻读
不可重复读
串行化
-->
<property name="hibernate.connection.isolation">4</property>
<!--扫描映射文件
class 必须 映射文件和配置文件名字和路径
package 扫描该包下的所有配置文件
resource 指定某个确定xml配置文件
-->
<mapping resource="cn/hd/bean/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>