• 复习Hibernate(1)


    1.首先Hibernate是对数据库进行持久化操作的框剪

     通过简单的代码来复习Hibernate

    第一步要搭建环境,此前有一片初识Hibernate的文章,欢迎大家参考,今天,将对数据库持久化的操作进行具体的介绍。

     由于是对数据库进行操作,因此,建一个 JavaProject 测试即可。

    ①首先建一个 Java 工程

    1.建一个单元测试类(HibernateTest.java)

     1 package com.atguigu.hibernate.helloworld;
     2 
     3 import java.sql.Date;
     4 
     5 import org.hibernate.Session;
     6 import org.hibernate.SessionFactory;
     7 import org.hibernate.Transaction;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.service.ServiceRegistry;
    10 import org.hibernate.service.ServiceRegistryBuilder;
    11 import org.junit.Test;
    12 
    13 public class HibernateTest {
    14 
    15     @Test
    16     public void test() {
    17         System.out.println("test...");
    18         // 1. 创建一个 SessionFactory 对象
    19         SessionFactory sessionFactory = null;
    20         // 1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
    21         Configuration configuration = new Configuration().configure();
    22         // 4.0 之前这样创建
    23         // sessionFactory = configuration.buildSessionFactory();
    24 
    25         // 2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
    26         // hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
    27         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
    28                 .applySettings(configuration.getProperties()).build();
    29         // 3).
    30         sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    31 
    32         // 2. 创建一个 Session 对象
    33         Session session = sessionFactory.openSession();
    34 
    35         // 3. 开启事务
    36         Transaction transaction = session.beginTransaction();
    37 
    38         // 4. 执行保存操作
    39         News news = new News("Java123450", "ATGUIGU0", new Date(
    40                 new java.util.Date().getTime()));
    41         session.save(news);
    42 
    43         // 5. 提交事务
    44         transaction.commit();
    45 
    46         // 6. 关闭 Session
    47         session.close();
    48 
    49         // 7. 关闭 SessionFactory 对象
    50         sessionFactory.close();
    51     }
    52 
    53 }
    View Code

    2.建一个JavaBean (News.java) 用于和数据库关联,即所谓的持久化类

      1 package com.atguigu.hibernate.helloworld;
      2 
      3 import java.sql.Blob;
      4 import java.util.Date;
      5 
      6 public class News {
      7     
      8     private Integer id; //field
      9     private String title;
     10     private String author;
     11     
     12     private String desc;
     13     
     14     //使用 title + "," + content 可以来描述当前的 News 记录. 
     15     //即 title + "," + content 可以作为 News 的 desc 属性值
     16     
     17     private String content;
     18     
     19     private Blob picture;
     20     
     21     public Blob getPicture() {
     22         return picture;
     23     }
     24 
     25     public void setPicture(Blob picture) {
     26         this.picture = picture;
     27     }
     28 
     29     public String getContent() {
     30         return content;
     31     }
     32 
     33     public void setContent(String content) {
     34         this.content = content;
     35     }
     36     
     37     public String getDesc() {
     38         return desc;
     39     }
     40 
     41     public void setDesc(String desc) {
     42         this.desc = desc;
     43     }
     44 
     45 
     46 
     47     private Date date;
     48 
     49     public Integer getId() { //property
     50         return id;
     51     }
     52 
     53     public void setId(Integer id) {
     54         this.id = id;
     55     }
     56 
     57     public String getTitle() {
     58         return title;
     59     }
     60 
     61     public void setTitle(String title) {
     62         this.title = title;
     63     }
     64 
     65     public String getAuthor() {
     66         return author;
     67     }
     68 
     69     public void setAuthor(String author) {
     70         this.author = author;
     71     }
     72 
     73     public Date getDate() {
     74         return date;
     75     }
     76 
     77     public void setDate(Date date) {
     78         this.date = date;
     79     }
     80 
     81     public News(String title, String author, Date date) {
     82         super();
     83         this.title = title;
     84         this.author = author;
     85         this.date = date;
     86     }
     87     
     88     public News() {
     89         // TODO Auto-generated constructor stub
     90     }
     91 
     92     @Override
     93     public String toString() {
     94         return "News [id=" + id + ", title=" + title + ", author=" + author
     95                 + ", date=" + date + "]";
     96     }
     97     
     98     
     99     
    100 }
    View Code

    3.借助此前搭建的eclipse插件生成  (News.hbm.xml)

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 
     5 <hibernate-mapping package="com.atguigu.hibernate.helloworld">
     6 
     7     <class name="News" table="NEWS" dynamic-insert="true">
     8         
     9         <id name="id" type="java.lang.Integer">
    10             <column name="ID" />
    11             <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
    12             <generator class="native" />
    13         </id>
    14     
    15         <property name="title" not-null="true" unique="true"
    16             index="news_index" length="50"
    17             type="java.lang.String" column="TITLE" >
    18         </property>
    19         
    20         <property name="author" type="java.lang.String"
    21             index="news_index">
    22             <column name="AUTHOR" />
    23         </property>
    24         
    25         <property name="date" type="date">
    26             <column name="DATE" />
    27         </property>
    28         
    29         <property name="desc" 
    30             formula="(SELECT concat(title, ',', author) FROM NEWS n WHERE n.id = id)"></property>
    31         
    32         <property name="content">
    33             <column name="CONTENT" sql-type="text"></column>
    34         </property>
    35         
    36         <property name="picture" column="PICTURE" type="blob"></property>
    37         
    38     </class>
    39     
    40 </hibernate-mapping>
    View Code

    4.Hibernate的配置文件 (hibernate.cfg.xml)

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 
     5 <hibernate-mapping package="com.atguigu.hibernate.helloworld">
     6 
     7     <class name="News" table="NEWS" dynamic-insert="true">
     8         
     9         <id name="id" type="java.lang.Integer">
    10             <column name="ID" />
    11             <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
    12             <generator class="native" />
    13         </id>
    14     
    15         <property name="title" not-null="true" unique="true"
    16             index="news_index" length="50"
    17             type="java.lang.String" column="TITLE" >
    18         </property>
    19         
    20         <property name="author" type="java.lang.String"
    21             index="news_index">
    22             <column name="AUTHOR" />
    23         </property>
    24         
    25         <property name="date" type="date">
    26             <column name="DATE" />
    27         </property>
    28         
    29         <property name="desc" 
    30             formula="(SELECT concat(title, ',', author) FROM NEWS n WHERE n.id = id)"></property>
    31         
    32         <property name="content">
    33             <column name="CONTENT" sql-type="text"></column>
    34         </property>
    35         
    36         <property name="picture" column="PICTURE" type="blob"></property>
    37         
    38     </class>
    39     
    40 </hibernate-mapping>
    View Code

    参考目录结构 :

                                  Hibernate 简单案例(1)完了    ^_^

                                                     更新中。。。

     

       

  • 相关阅读:
    clion打断点不生效
    PHP加密解密
    细说MySQL表操作
    细说MySQL数据库操作
    终端(命令行)连接MySQL
    MySQL结构
    求1!+(1!+3!)+(1!+3!+5!)+...+(1!+3!+5!+7!+9!)的值
    react 生命周期
    React TS 组件 Demo
    react-redux 实现原理
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/3650689.html
Copyright © 2020-2023  润新知