Hibernate开发备用

来源:互联网 发布:矩阵内积 编辑:程序博客网 时间:2024/06/04 18:52
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>
 
 
 

本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/35211

原创粉丝点击