zoukankan      html  css  js  c++  java
  • 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、执行结果

  • 相关阅读:
    Java泛型 T.class的获取
    Android ant自动打包脚本:自动替换友盟渠道、版本号、包名
    验证:mysql AUTO_INCREMENT 默认值是1
    双重OAuth 2.0架构
    使用coding、daocloud和docker打造markdown纯静态博客
    创业小坑:内网域名 在windows下能nslookup,但ping不通,也无法访问。而在linux下正常。
    freeradius 安装出错的解决办法
    与锤子手机HR的对话——创业没有联合创始人,CTO 等高管会把它当做自己的事业吗?
    LBS数据分析:使用地图展示统计数据——麻点图与麻数图
    PHP极客水平测试——给创业公司用的远程面试题
  • 原文地址:https://www.cnblogs.com/super-admin/p/6431418.html
Copyright © 2011-2022 走看看