• 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate文件配置


    <?xml version="1.0" encoding="GBK"?>
    <project name="hibernate" basedir="." default="">
        <property name="src" value="src"/>
        <property name="dest" value="classes"/>
    
        <path id="classpath">
            <fileset dir="../../lib">
                <include name="**/*.jar"/>
            </fileset>
            <pathelement path="${dest}"/>
        </path>
    
        <target name="compile" description="Compile all source code">
            <delete dir="${dest}"/>
            <mkdir dir="${dest}"/>
            <copy todir="${dest}">
                <fileset dir="${src}">
                    <exclude name="**/*.java"/>
                </fileset>        
            </copy>
            <javac destdir="${dest}" debug="true" includeantruntime="yes"
                deprecation="false" optimize="false" failonerror="true">
                <src path="${src}"/>
                <classpath refid="classpath"/>
                <compilerarg value="-Xlint:deprecation"/>
            </javac>
        </target>
    
        <target name="run" description="Run the main class" depends="compile">
            <java classname="lee.NewsManager" fork="yes" failonerror="true">
                <classpath refid="classpath"/>
            </java>
        </target>
    
    </project>
    package lee;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import org.hibernate.service.*;
    import org.hibernate.boot.registry.*;
    import org.crazyit.app.domain.*;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class NewsManager
    {
        public static void main(String[] args) throws Exception
        {
            // 实例化Configuration,不加载任何配置文件
            Configuration conf = new Configuration()
                // 通过addAnnotatedClass()方法添加持久化类
                .addAnnotatedClass(org.crazyit.app.domain.News.class)
                // 通过setProperty设置Hibernate的连接属性。
                .setProperty("hibernate.connection.driver_class"
                    , "com.mysql.jdbc.Driver")
                .setProperty("hibernate.connection.url"
                    , "jdbc:mysql://localhost/hibernate")
                .setProperty("hibernate.connection.username" , "root")
                .setProperty("hibernate.connection.password" , "32147")
                .setProperty("hibernate.c3p0.max_size" , "20")
                .setProperty("hibernate.c3p0.min_size" , "1")
                .setProperty("hibernate.c3p0.timeout" , "5000")
                .setProperty("hibernate.c3p0.max_statements" , "100")
                .setProperty("hibernate.c3p0.idle_test_period" , "3000")
                .setProperty("hibernate.c3p0.acquire_increment" , "2")
                .setProperty("hibernate.c3p0.validate" , "true")
                .setProperty("hibernate.dialect"
                    , "org.hibernate.dialect.MySQL5InnoDBDialect")
                .setProperty("hibernate.hbm2ddl.auto" , "update");
            // 以Configuration实例创建SessionFactory实例
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties()).build();
            SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
            // 实例化Session
            Session sess = sf.openSession();
            // 开始事务
            Transaction tx = sess.beginTransaction();
            // 创建消息实例
            News n = new News();
            // 设置消息标题和消息内容
            n.setTitle("疯狂Java联盟成立了");
            n.setContent("疯狂Java联盟成立了,"
                + "网站地址http://www.crazyit.org");
            // 保存消息
            sess.save(n);
            // 提交事务
            tx.commit();
            // 关闭Session
            sess.close();
        }
    }
    package org.crazyit.app.domain;
    
    import javax.persistence.*;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    @Entity
    @Table(name="news_inf")
    public class News
    {
        // 消息类的标识属性
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        // 消息标题
        private String title;
        // 消息内容
        private String content;
    
        // id的setter和getter方法
        public void setId(Integer id)
        {
            this.id = id;
        }
        public Integer getId()
        {
            return this.id;
        }
    
        // title的setter和getter方法
        public void setTitle(String title)
        {
            this.title = title;
        }
        public String getTitle()
        {
            return this.title;
        }
    
        // content的setter和getter方法
        public void setContent(String content)
        {
            this.content = content;
        }
        public String getContent()
        {
            return this.content;
        }
    }
  • 相关阅读:
    Java核心技术点之动态代理
    一起写一个Android图片加载框架
    Java核心技术点之注解
    Android中的Intent Filter匹配规则介绍
    阿里客户端工程师试题简析——Android应用的闪退(crash)分析
    Android开发之高效加载Bitmap
    Java NIO:浅析I/O模型
    深入探索Android中的Handler
    git常用命令
    SM2
  • 原文地址:https://www.cnblogs.com/tszr/p/12367134.html
Copyright © 2020-2023  润新知