码迷,mamicode.com
首页 > 数据库 > 详细

myeclipse数据库逆向hibernate教程

时间:2016-06-30 16:25:23      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

.首先我们的准备

1.项目

    技术分享

2.数据库

技术分享数据库执行命令.sql
 1 /*
 2 Navicat MySQL Data Transfer
 3 
 4 Source Server         : mysql123
 5 Source Server Version : 50624
 6 Source Host           : localhost:3306
 7 Source Database       : inverse
 8 
 9 Target Server Type    : MYSQL
10 Target Server Version : 50624
11 File Encoding         : 65001
12 
13 Date: 2016-06-30 15:46:41
14 */
15 
16 SET FOREIGN_KEY_CHECKS=0;
17 
18 -- ----------------------------
19 -- Table structure for users
20 -- ----------------------------
21 DROP TABLE IF EXISTS `users`;
22 CREATE TABLE `users` (
23   `id` int(4) NOT NULL AUTO_INCREMENT,
24   `name` varchar(20) DEFAULT NULL,
25   `content` varchar(100) DEFAULT NULL,
26   PRIMARY KEY (`id`)
27 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

技术分享

技术分享

 

现在准备好了以上空项目和表后,我们开始逆向工程第一步

1.文字不重要,看图

技术分享

技术分享

技术分享

技术分享

 

 

2.接下来我们来配置我们的hibernate核心jar包

  右键我们的项目选择myeclipse--》 project facets-->install hibernate facet  

技术分享技术分享

技术分享技术分享

 3.现在我们回到从自带的DB开始

技术分享

技术分享

技术分享

技术分享

4.然后就这样就可以了,让我们来看看我们反向的实体类

技术分享

5.这样就好了,谢谢您的观看,大家一起学习

附上配置一份

  hibernate.cfg.xml

技术分享
<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/test
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">test</property>
        <mapping resource="com/hp/entity/Users.hbm.xml" />

    </session-factory>

</hibernate-configuration>
View Code

HibernateSessionFactory(工具类)

技术分享
package com.hp.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static org.hibernate.SessionFactory sessionFactory;
    
    private static Configuration configuration = new Configuration();
    private static ServiceRegistry serviceRegistry; 

    static {
        try {
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }
    private HibernateSessionFactory() {
    }
    
    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession()
                    : null;
            threadLocal.set(session);
        }

        return session;
    }

    /**
     *  Rebuild hibernate session factory
     *
     */
    public static void rebuildSessionFactory() {
        try {
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }

    /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

    /**
     *  return session factory
     *
     */
    public static org.hibernate.SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    /**
     *  return hibernate configuration
     *
     */
    public static Configuration getConfiguration() {
        return configuration;
    }

}
View Code

 

myeclipse数据库逆向hibernate教程

标签:

原文地址:http://www.cnblogs.com/thehugo/p/5630241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!