ORM框架

持久化框架

一、持久化:IO、数据库

对象的瞬时状态和持久状态相互转换的过程,称之为持久化。

CRUD :

瞬时状态–>--持久状态 : insert update delete

持久状态 -->–瞬时状态:select

二、ORM

ORM : Object Relational Mapping

ORM 即 Object Relation Mapping 对象关系映射。是一种程序技术,解决一类问题的思路方法,不局限于应用在 Java 语言。用于面向对象语言中完成类和表,属性和表字段转换的映射机制。

类名和表名、属性和列名、Java类型和表类型等 之间的映射

三、序列化

把对象状态转为可以可存储或可传输的状态 称为序列化

把可存储或可传输状态转为对象瞬时状态 称为反序列化

四、Hibernate使用步骤

1. 下载 jar ,并在工程中引入

如果使用的 idea 在创建工程时,注意勾选 Hibernate 即可(自动下载),点击右下角 config 也可以更换版本。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jN76yOmm-1594821154563)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1594782440594.png)]

2. 创建配置文件 hibernate.cfg.xml

hibernate框架就是对 JDBC 封装,需要 url 、driver 、 username、 password 四个参数,另外还需要 “方言” 。

hibernate.cfg.xml中需要配置五个基本参数,用来连接数据库

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT</property>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
		<property name="hibernate.show_sql">true</property>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

3. 创建实体类及对应的映射文件(可以注解代替)

创建实体类:

package com.wdzl.entity;

public class Student {
    private int sid;
    private String sname;
    private int age;
    private String phone;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

创建映射文件

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.wdzl.entity.Student" table="student">
        <!--主键-->
        <id name="sid" column="sid"></id>
        <property name="age" column="sage"></property>
        <property name="sname" column="name"></property>
        <property name="phone" column="phone"></property>
    </class>
</hibernate-mapping>

把映射文件添加到配置文件中

<mapping resource="com/wdzl/entity/Student.hbm.xml"></mapping>

4. 编写代码测试

package com.wdzl;

import com.wdzl.entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * 1.jar
 * 2.配置文件: dirver url username password dialect
 * 3.实体类  映射文件  引入配置文件中
 * 4.测试
 */
public class TestHibernate {
    public static void main(String[] args) {
        //加载配置文件
        Configuration cfg = new Configuration().configure("/hibernate.cfg.xml");
        //创建session工厂 sessionFactory
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        //创建session
        Session session = sessionFactory.openSession();
        
        Student student = new Student();
        student.setAge(23);
        student.setSname("曹操");
        student.setPhone("133455555");
        student.setSid(1);
        //开启事务
        Transaction transaction = session.beginTransaction();
        //持久化操作
        session.save(student);
        //提交事务
        transaction.commit();
        //关闭
        session.close();
    }
}

ginTransaction();
//持久化操作
session.save(student);
//提交事务
transaction.commit();
//关闭
session.close();
}
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值