Hibernate开发备用

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
Hibernate开发备用
 
平时做小测试用的,稍微修改一下就可以用到正式开发环境中。
 
 
一、配置文件
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        " [url]http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd[/url]">
<hibernate-configuration>
    <session-factory>
        <!--显示执行的SQL语句-->
        <property name="show_sql">true</property>
        <!--连接字符串-->
        <property name="connection.url">
            jdbc:mysql://localhost:3306/testdb
        </property>
        <!--连接数据库的用户名-->
        <property name="connection.username">root</property>
        <!--数据库用户密码-->
        <property name="connection.password">leizhimin</property>
        <!--数据库驱动-->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <!--选择使用的方言-->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <!--设置Hibernate自动管理上下文的策略-->
        <property name="current_session_context_class">thread</property>
        <!--JDBC连接池(使用内置的连接池)-->
        <property name="connection.pool_size">1</property>
        <!--是否使用数据库外连接-->
        <!--<property name="hibernate.use_outer_join">true</property>-->
        <!--事务管理类型,这里使用JDBC Transaction-->
        <property name="hiberante.transaction.factory_class">
            org.hibernate.transaction.JDBCTransactionFactory
        </property>
        <!--在启动时删除并重新创建数据库-->
        <!--<property name="hbm2ddl.auto">create</property>-->

        <mapping resource="org/lavasoft/domain/zvfims/reg/user/entity/TUser.hbm.xml"/>
        <mapping resource="org/lavasoft/domain/zvfims/reg/user/entity/TTest.hbm.xml"/>

    </session-factory>
</hibernate-configuration>
 
 
二、HibernateUtil.java
package org.lavasoft.domain.common;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-7-18
 * Time: 10:18:44
 * Hibernate工具类
 */
public class HibernateUtil {
    private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static Configuration configuration = new Configuration();
    private static SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;
    static {
        try {
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }
    private HibernateUtil() {
    }
    /**
     * 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;
    }
    /**
     * 重新构建SessionFactory
     */
    public static void rebuildSessionFactory() {
        try {
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        } 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 session factory
     * <p/>
     * session factory will be rebuilded in the next call
     */
    public static void setConfigFile(String configFile) {
        HibernateUtil.configFile = configFile;
        sessionFactory = null;
    }
    /**
     * return hibernate configuration
     */
    public static Configuration getConfiguration() {
        return configuration;
    }
}
 
 
三、xdoclet-build.xml
<?xml version="1.0" encoding="gb2312"?>
<project name="xdoclet-build" default="xdoclet" basedir=".">
    <property name="project.lib.dir" value="${basedir}/lib"/>
    <property name="xdoclet.lib.dir" value="${project.lib.dir}/xdoclet"/>
    <property name="project.src.dir" value="${basedir}/src"/>
    <property name="project.sql.dir" value="${basedir}/doc/dbscript"/>
    <property name="project.resources.dir" value="${basedir}/doc/res"/>
    <property name="hibernate.cfg.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    <property name="hibernate.cfg.driver" value="com.mysql.jdbc.Driver"/>
    <property name="hibernate.cfg.username" value="root"/>
    <property name="hibernate.cfg.password" value="leizhimin"/>
    <property name="hibernate.cfg.jdbcurl" value="jdbc:mysql://localhost:3306/testdb"/>
    <property name="hibernate.cfg.showsql" value="true"/>
    <property name="module.name" value="doc"/>

    <target name="xdoclet">
        <path id="xdoclet.task.classpath">
            <fileset dir="${xdoclet.lib.dir}">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${project.lib.dir}">
                <include name="**/*.jar"/>
            </fileset>
        </path>
        <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.task.classpath"/>
    </target>
    <target name="generate-mapping" depends="xdoclet">
        <hibernatedoclet destdir="${project.src.dir}" verbose="true" force="false">
            <fileset dir="${project.src.dir}">
                <include name="**/domain/**/*.java"/>
            </fileset>
            <hibernate version="3.0" xmlencoding="gb2312"/>
        </hibernatedoclet>
    </target>
    <target name="generate-configuration" depends="xdoclet">
        <hibernatedoclet destdir="${project.resources.dir}" verbose="true"
            force="true">
            <fileset dir="${project.src.dir}">
                <include name="**/entity/*.java"/>
            </fileset>
            <hibernatecfg destinationFile="${project.resources.dir}/hibernate.cfg.xml"
                dialect="${hibernate.cfg.dialect}"
                driver="${hibernate.cfg.driver}"
                username="${hibernate.cfg.username}"
                password="${hibernate.cfg.password}"
                jdbcurl="${hibernate.cfg.jdbcurl}"
                showsql="${hibernate.cfg.showsql}"
                destdir="${project.resources.dir}" xmlencoding="gb2312"/>
        </hibernatedoclet>
    </target>
    <target name="generate-schema" depends="xdoclet">
        <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
            classpathref="xdoclet.task.classpath"/>
        <property name="hibernate.dialect" value="${hibernate.cfg.dialect}"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments true" value="true"/>
        <schemaexport quiet="no" text="yes" drop="no" delimiter=";"
            output="${project.sql.dir}/hb_test1.sql">
            <fileset dir="${project.src.dir}">
                <include name="**/domain/**/*.hbm.xml"/>
            </fileset>
        </schemaexport>
    </target>
    <target name="generate-schema-all" depends="xdoclet">
        <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
            classpathref="xdoclet.task.classpath"/>
        <property name="hibernate.dialect" value="${hibernate.cfg.dialect}"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments true" value="true"/>
        <schemaexport quiet="no" text="yes" drop="no" delimiter=";"
            output="${project.sql.dir}/hb_test2.sql">
            <fileset dir="${project.src.dir}">
                <include name="**/domain/**/*.hbm.xml"/>
            </fileset>
        </schemaexport>
    </target>
</project>
 
 

本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/35211,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
11天前
|
Java 关系型数据库 MySQL
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
|
16天前
|
Java 关系型数据库 MySQL
基于SpringBoot后端实现连接MySQL数据库并存贮数据
基于SpringBoot后端实现连接MySQL数据库并存贮数据
|
Java 数据库
SSM 最简单的实现操作 多数据源&动态切换
SSM 最简单的实现操作 多数据源&动态切换
159 0
SSM 最简单的实现操作 多数据源&动态切换
|
Java 数据库连接 数据库
SpringBoot2学习(四):数据库场景的自动配置与整合
SpringBoot2学习(四):数据库场景的自动配置与整合
SpringBoot2学习(四):数据库场景的自动配置与整合
|
SQL Java 数据库连接
Spring学习(九):JdbcTemplate操作数据库实现增加功能
Spring学习(九):JdbcTemplate操作数据库实现增加功能
Spring学习(九):JdbcTemplate操作数据库实现增加功能
|
Java 数据库 Spring
spring中bean配置8.0版本数据库,时区解决问题!
spring中bean配置8.0版本数据库,时区解决问题!
|
SQL druid Java
基于springboot+jpa 实现多租户动态切换多数据源 - 使用Flyway实现多数据源数据库脚本管理和迭代更新
基于springboot+jpa 实现多租户动态切换多数据源 - 使用Flyway实现多数据源数据库脚本管理和迭代更新
1325 0
基于springboot+jpa 实现多租户动态切换多数据源 - 使用Flyway实现多数据源数据库脚本管理和迭代更新
|
前端开发 Java 开发者
扩展与全面接管 SpringMVC|学习笔记
快速学习扩展与全面接管 SpringMVC
64 0
|
SQL druid Java
Springboot 从数据库读取数据库配置信息,动态切换多数据源 最详细实战教程
Springboot 从数据库读取数据库配置信息,动态切换多数据源 最详细实战教程
3283 1
Springboot 从数据库读取数据库配置信息,动态切换多数据源 最详细实战教程
|
SQL XML 存储
工具人不好当,至少也要会如何配置MyBatis!
本文主要介绍 MyBatis中的配置详解
97 0