zoukankan      html  css  js  c++  java
  • Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等

        <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>

    有update create validate create-drop四种属性

    update 表示如果映射的类中有加的类 会自动改表

    create是指如果数据库没有创表可以自动帮创

    validate会在执行操作前可以先验证有没有表

    create-drop是指关闭sessionfactory之后自动drop掉表

    一般实际开发先建表

     搭建Log4j日志环境

    加入jar包

    引入配置文件

    Log4j.properties

    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n
    
    ### direct messages to file hibernate.log ###
    #log4j.appender.file=org.apache.log4j.FileAppender
    #log4j.appender.file.File=hibernate.log
    #log4j.appender.file.layout=org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    
    log4j.rootLogger=warn, stdout
    
    #log4j.logger.org.hibernate=info
    #log4j.logger.org.hibernate=debug
    
    ### log HQL query parser activity
    #log4j.logger.org.hibernate.hql.ast.AST=debug
    
    ### log just the SQL
    #log4j.logger.org.hibernate.SQL=debug
    
    ### log JDBC bind parameters ###
    #log4j.logger.org.hibernate.type=info
    #log4j.logger.org.hibernate.type=debug
    
    ### log schema export/update ###
    log4j.logger.org.hibernate.tool.hbm2ddl=debug
    
    ### log HQL parse trees
    #log4j.logger.org.hibernate.hql=debug
    
    ### log cache activity ###
    #log4j.logger.org.hibernate.cache=debug
    
    ### log transaction activity
    #log4j.logger.org.hibernate.transaction=debug
    
    ### log JDBC resource acquisition
    #log4j.logger.org.hibernate.jdbc=debug
    
    ### enable the following line if you want to track down connection ###
    ### leakages when using DriverManagerConnectionProvider ###
    #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

    可以注释掉不想要的内容

    搭建Junit环境

    学习maven 加入Junit类库

    新建一个test

    新建Junite test case

    加入注解@Test

    package com.easy;
    
    import static org.junit.Assert.*;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class Teachertest {
        public static  SessionFactory sf = null;
        @BeforeClass
        public static void beforeclass(){
                sf = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        @Test
        public void test() {
            student  su = new student();
              su.setId(13);
              su.setName("haha功");
              su.setAge(22);
              
              Session session = sf.openSession();
              session.beginTransaction();
              session.save(su);
              session.getTransaction().commit();
              session.close();
             
        }
       @AfterClass
       public static void afterclass(){
           sf.close();
       }
    }

    按照junite测试

  • 相关阅读:
    非域,非匿名用户访问远程企业服务的详细步骤
    调用远程的企业服务的安全问题
    未能加载文件或程序集“****”或它的某一个依赖项的一种情况
    XAMPP使用非80端口的安装配置修改
    Lucene 的存储结构概述
    .NET Framework 4.0 SDK的安装
    lucene 文件存储相关的几个类
    ASP.NET 状态服务 及 session丢失问题解决方案总结
    不安装.net framework框架运行.Net 程序的方法<收藏>
    net面试题集及答案
  • 原文地址:https://www.cnblogs.com/frankzone/p/9582671.html
Copyright © 2011-2022 走看看