浅谈hibernate

浅谈hibernate

个人理解

关于hibernate,就是一个全自动的orm框架,没有多说的。不过在怎么全自动也不是一个人工智能,在当前计算机基础下,不可能出现人工智能,再怎么智能也需要人给出指令或者命令,才能给你执行。话不多说,进入正题

配置

首先就是建立工程,然后去pom导包
,导包的话百度上多了去了,自己找。我就随便放
(idea----idea----idea----idea----)

      <!-- hibernate5 -->
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.2.5.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>



        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-java8</artifactId>
            <version>5.2.12.Final</version>
        </dependency>

然后再把你需要的其他的资源放进去,下载好就行了。

配置文件

配置文件获得数据源和配置SQL方言。生成SQL语句和映射实体
hibernate官网上多的是,我也随便放一个

<?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>

<session-factory>
    <!-- 数据库连接配置 -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&amp;characterEncoding=UTF-8</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>
    <!-- SQL 方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- 在控制台输出sql语句 -->
    <property name="show_sql">true</property>
    <!-- 在启动时根据配置更新数据库 -->
    <property name="hbm2ddl.auto">update</property>

    <!-- hibernate加载映射的实体 -->
    <mapping class="com.Alibaba.hibernate.entity.StudentEntity"></mapping>
</session-factory>

</hibernate-configuration>

得到session

写一个得到session的类,代码如下

package com.lovo.hibernate.db;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class GetSessionDb {


    public static Session sessionOpen()  {
        SessionFactory  sessionFactory=null;
        // A SessionFactory is set up once for an application!
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("hibernate.cfg2.xml") // configures settings from hibernate.cfg.xml
                .build();
        try {
            sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
        }
        catch (Exception e) {
            // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
            // so destroy it manually.
            StandardServiceRegistryBuilder.destroy( registry );
        }
        return sessionFactory.openSession();
    }
}

实体

写实体,注意映射关系,具体看代码,我注解给上

package com.lovo.hibernate.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity  //可以被hibernate管理的实体
@Table(name = "h_student") //映射表  name=表名
public class StudentEntity {
    @Id   //所有实体必须有@ID注解,这里使用UUID的话需要长度32位,不然报错,不要问我为什么,规定就这样
    @Column(length = 32,name = "stu_id")  //映射数据库的列
    //自定义ID生成策略UUID
    @GenericGenerator(name="stuuuid",strategy = "uuid")
//    这里的UUID是上面的
    @GeneratedValue(generator = "stuuuid")
    private  String studentId;
//    unique,主键唯一约束
    @Column(length = 48,name = "user_name",unique = true)
    private  String userName;
//    nullable是非空约束,默认true
    @Column(length = 48,nullable = false)
    private  String pwd;
    private float score;

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }
}

最后

去你的test测试就好了。这个东西没有什么难的,注意一下就好了。
加油,程序员

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值