• hibernate的helloworld实现


    首先要新建一个 web project,然后新建一个lib的文件夹并导入相应的jar包

    hibernate开发步骤:

    1.创建hibernate配置文件

    2.创建持久化类

    3.创建对象关系映射文件

    如下:

    相应的jar包如下:

    (jar包去 hibernate文件中找)

    hibernate.cfg.xml代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7      <!-- 配置hibernate的基本信息 -->
     8      <property name="connection.username">root</property>
     9      <property name="connection.password">root</property>
    10      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    11      <property name="connection.url">jdbc:mysql:///hibernate1</property>
    12      
    13      <!-- hibernate的基本配置 -->
    14      <!-- 数据库方言 -->
    15      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    16 
    17      <!-- 是否打印sql -->
    18      <property name="show_sql">true</property>
    19      <!-- 是否格式化sql -->
    20      <property name="format_sql">true</property>
    21      <!-- 生成表的策略 -->
    22      <property name="hbm2ddl.auto">update</property>
    23      
    24      
    25      
    26      <!--需要关联的映射文件-->
    27      <mapping  resource="com/nzx/hibernate/News.hbm.xml"/>
    28      
    29     </session-factory>
    30 </hibernate-configuration>

    持久化类News代码如下:

     1 package com.nzx.hibernate;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * @author zxN
     7  * @version 创建时间:2016年1月3日 下午2:13:57
     8  * 类说明
     9  */
    10 class News {
    11     private Integer id;
    12     private String title;
    13     private String author;
    14     private Date date;
    15     public News() {
    16         super();
    17         // TODO Auto-generated constructor stub
    18     }
    19     public News( String title, String author, Date date) {
    20         super();
    21     
    22         this.title = title;
    23         this.author = author;
    24         this.date = date;
    25     }
    26     public Integer getId() {
    27         return id;
    28     }
    29     public void setId(Integer id) {
    30         this.id = id;
    31     }
    32     public String getTitle() {
    33         return title;
    34     }
    35     public void setTitle(String title) {
    36         this.title = title;
    37     }
    38     public String getAuthor() {
    39         return author;
    40     }
    41     public void setAuthor(String author) {
    42         this.author = author;
    43     }
    44     public Date getDate() {
    45         return date;
    46     }
    47     public void setDate(Date date) {
    48         this.date = date;
    49     }
    50     @Override
    51     public String toString() {
    52         return "News [id=" + id + ", title=" + title + ", author=" + author
    53                 + ", date=" + date + "]";
    54     }
    55     
    56     
    57 }

    对象关系映射文件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 <!-- Generated 2016-2-29 14:33:38 by Hibernate Tools 3.4.0.CR1 -->
     5 <hibernate-mapping>
     6     <class name="com.nzx.hibernate.News" table="NEWS">
     7         <id name="id" type="java.lang.Integer">
     8             <column name="ID" />
     9             <generator class="native" />
    10         </id>
    11         <property name="title" type="java.lang.String">
    12             <column name="TITLE" />
    13         </property>
    14         <property name="author" type="java.lang.String">
    15             <column name="AUTHOR" />
    16         </property>
    17         <property name="date" type="java.util.Date">
    18             <column name="DATE" />
    19         </property>
    20     </class>
    21 </hibernate-mapping>

    junit测试类HibernateTest代码如下:

     1 package com.nzx.hibernate;
     2 
     3 import java.util.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 
    14 public class HibernateTest {
    15 
    16     @Test
    17     public void test() {
    18         //1.创建一个SessionFactory对象
    19         SessionFactory sessionFactory = null;
    20         
    21         //1).创建Configuration对象:对应hibernate的基本配置信息和对象关系映射信息
    22         Configuration configuration = new Configuration().configure();
    23         //2).创建一个ServiceRigistry对象  hibernate的任何配置和服务都需要在该对象中注册才能生效
    24         
    25         ServiceRegistry serviceRegistry=
    26                  new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
    27                         
    28         sessionFactory = configuration.buildSessionFactory(serviceRegistry);                
    29         //2.创建一个Session对象
    30         Session session = sessionFactory.openSession();
    31         //3.开启事务
    32         Transaction transaction = session.beginTransaction();
    33         //4.执行保存操作
    34         News news = new News("java","JAVAEE详解",new Date(new java.util.Date().getTime()));
    35         session.save(news);
    36         //5.提交事务
    37         transaction.commit();
    38         
    39         //6.关闭 session
    40         session.close();
    41         //7.关闭SessionFactory对象
    42         sessionFactory.close();
    43         
    44         
    45     }
    46 
    47 }

    执行test方法,会新建表并出现 插入进的语句

    Hibernate:
    insert
    into
    NEWS
    (TITLE, AUTHOR, DATE)
    values
    (?, ?, ?)

  • 相关阅读:
    [PHP] yield沟通函数循环内外
    [Linux] scp本地服务器和远程服务器拷贝文件
    [Linux] 大数据库导出大文件统计并去重
    [Go] golang连接查询mysql
    [日常] 解决mysql不允许外部访问
    [Go] golang创建目录写文件判断文件
    [日常] imap协议读取邮件
    [Go] golang使用github里的imap类库
    [Go] golang无缓冲通道实现工作池控制并发
    [Go] golang的range循环遍历通道
  • 原文地址:https://www.cnblogs.com/zx-n/p/5228303.html
Copyright © 2020-2023  润新知