• 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、执行结果

  • 相关阅读:
    数学工具WZgrapher
    零线和地线的区别,示波器如何测量市电?
    使用直流稳压电源时的注意事项!
    中文全角和半角输入有什么区别?
    ThinkingRock:使用方法
    2014记首
    如何使用Excel绘制甘特图
    AStyle代码格式工具在source insight中的使用
    STM32F103系列命名规则
    上市公司行情查询站点
  • 原文地址:https://www.cnblogs.com/super-admin/p/6431418.html
Copyright © 2020-2023  润新知