• zbb20170218_hibernate


    1、结构图

    2、class文件

    Event.java

    package com.zbb.entity;
    
    import java.util.Date;
    
    public class Event {
        private Long id;
    
        private String title;
        private Date date;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public Date getDate() {
            return date;
        }
        public void setDate(Date date) {
            this.date = date;
        }
    
    }

    Test.java

    package com.zbb.test;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    public class Test {
        public static void main(String[] args) {
            creteTable();
        }
        public static void creteTable(){
            new SchemaExport(getConfiguration()).create(true, true);
        }
        public static Session getSession(){
            return getSessionFactory().openSession();
        }
        public static SessionFactory getSessionFactory(){
            return getConfiguration().buildSessionFactory();
        }
        public static Configuration getConfiguration(){
            return new Configuration().configure();
        }
    }

    3、配置文件

    Event.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
        <class name="com.zbb.entity.Event">
            <id name="id" column="e_id"></id>
            <property name="title" column="e_titme"></property>
            <property name="date" type="timestamp" column="e_date"></property>
        </class>
    </hibernate-mapping>

    hibernate.cfg.xml

    <?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">
    
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    
    <session-factory>
    
        <!-- Database connection settings -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://127.0.0.1:3306/test
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
    
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
    
        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
    
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
    
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>
    
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="com/zbb/entity/Event.hbm.xml" />
    
    </session-factory>
    
    </hibernate-configuration>

    4、执行结果

  • 相关阅读:
    jenkins插件开发
    常用模块-------hashlib (加密模块)
    树莓派在任意无线网下连接笔记本(借用笔记本屏幕)
    常用模块-------时间模块(time/datetime),随机数模块(random)
    pycharm常用的快捷方式及设置
    迭代器
    生成器
    获取行业和概念列表
    钉钉页面扫码登录中hmac加密签名
    获取钉钉开发access_token
  • 原文地址:https://www.cnblogs.com/super-admin/p/6431418.html
Copyright © 2020-2023  润新知