slf日志接口, 实现有slf4j nodep, log4j

hibernate里我们一般用 log4j,所以删除之前创建的hibernate 包里的 slf4j-nop包, 加入log4j-1.2.17.jar

现在hibernate的包里有slf的api 的jar, 不能和log4j 自动匹配, 所以中间需要一个接口, 再加入 slf4j-log4j 的包 (1.5.8)

这样在运行界面就可以看到有建表等语句.

ddl语句就出来了:

14:00:20,587  INFO SchemaExport:179 - exporting generated schema to database
14:00:20,588 DEBUG SchemaExport:303 - drop table if exists Teacher
14:00:20,890 DEBUG SchemaExport:303 - drop table if exists student
14:00:21,022 DEBUG SchemaExport:303 - create table Teacher (id integer not null, name varchar(255), title varchar(255), primary key (id))
14:00:21,309 DEBUG SchemaExport:303 - create table student (id integer not null, name varchar(255), age integer, primary key (id))
14:00:21,530 INFO SchemaExport:196 - schema export complete

 

因为debug信息太多, 只保留数据库信息可以将log4j.properties里的内容只保留下面2句, 其余都屏蔽:

log4j.rootLogger=warn, stdout
log4j.logger.org.hibernate.tool.hbm2ddl=debug

最后ddl日志信息如下:

14:13:24,066  INFO SchemaExport:154 - Running hbm2ddl schema export
14:13:24,068 DEBUG SchemaExport:170 - import file not found: /import.sql
14:13:24,068 INFO SchemaExport:179 - exporting generated schema to database
14:13:24,069 DEBUG SchemaExport:303 - drop table if exists Teacher
14:13:24,193 DEBUG SchemaExport:303 - drop table if exists student
14:13:24,279 DEBUG SchemaExport:303 - create table Teacher (id integer not null, name varchar(255), title varchar(255), primary key (id))
14:13:24,372 DEBUG SchemaExport:303 - create table student (id integer not null, name varchar(255), age integer, primary key (id))
14:13:24,552 INFO SchemaExport:196 - schema export complete
Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)

  

搭建junit日志环境

preferrence里新建MyJUnit包, 加入junit包, 然后再项目右键add libary把自己的MyJUnit导入进项目.

1. 创建测试代码包: 项目右键 new --- source folder---test, 里面装测试代码, src里面都是项目代码  

2. 给test创建一个和上面model一样的package, 然后创建测试类 TeacherTest.java的unit test, 代码如下:

package com.bjsxt.hibernate.model;

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.Test; public class TeacherTest { @Test
public void testTeacherSave() {
Teacher t =new Teacher();
t.setId(4);
t.setName("wdfd");
t.setTitle("high"); Configuration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory(); //默认找hibernate.cfg.xml,然后产生一个connection工厂
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
} }

上面代码中每次创建sessionFactory比较费时, 所以考虑用beforeclass和afterclass:

package com.bjsxt.hibernate.model;

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 {
private static SessionFactory sf=null;
@BeforeClass
public static void beforeClass(){
sf=new AnnotationConfiguration().configure().buildSessionFactory();
}
@Test
public void testTeacherSave() {
Teacher t =new Teacher();
t.setId(4);
t.setName("wdfd");
t.setTitle("high"); Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
@AfterClass
public static void afterClass(){
sf.close();
}
}

  

有时test文件会出错, 无法追踪, 需要在 方法加try catch:

	@BeforeClass
public static void beforeClass(){
try {
sf=new AnnotationConfiguration().configure().buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

或者加一个main方法, 调用beforeClass();  

Hibernate---基础配置之日志信息slf 及搭建日志环境的更多相关文章

  1. Hibernate学习笔记2.1(Hibernate基础配置)

    Hibernate基础配置 1.<property name="hbm2ddl.auto">update</property> 在SessionFactor ...

  2. 3.Hibernate基础配置

    1.Hibernate.cfg.xml:hbm2ddl.auto 在SessionFactory创建时,自动检查数据库结构,或者将数据库schema的DDL导出到数据库 <property na ...

  3. Hibernate 基础配置及常用功能(三)

    本章重点讲述Hibernate对象的三种状态以及如何配置二级缓存 有关Hibernate的三种状态如何相互转换网上都能查到,官方文档描述的也比较详细.这里主要是针对几个重点方法做代码演示. 一.状态转 ...

  4. Hibernate 基础配置及常用功能(一)

    本来是想等全部框架测试完以后再统一发布的,但是随着测试的一点点增加感觉把需要叙述的东西放在一起终将会是一场灾难.所以还是打算分成几章来描述,其中还包括一些有待解决的问题.短期很难腾出时间来仔细阅读Hi ...

  5. 使用log4j使某些java类的日志信息输出到指定日志文件中

    Log4j 是 Apache 的一个开放源代码项目,通过使用 Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI 组件.甚至是套接口服务器.NT 的事件记录器.UNIX Syslog ...

  6. 【ELK】5.spring boot日志集成ELK,搭建日志系统

    阅读前必看: ELK在docker下搭建步骤 spring boot集成es,CRUD操作完整版 ============================================== 本章集成 ...

  7. Hibernate 基础配置及常用功能(二)

    本章主要是描述几种经典映射关系,顺带比较Hibernate4.x和Hibernate5.x之间的区别. 一.建立测试工程目录 有关实体类之间的相互映射关系,Hibernate官方文档其实描述的非常详细 ...

  8. hibernate基础配置

    数据库表名和类名 一致 注解:可写可不写: XML:可写可不写: <class name="Student"> 不一致 注解:  public class Teache ...

  9. Hibernate学习笔记2.3(Hibernate基础配置)

    映射,注释可以放在成员变量上面,也可以放在get方法上面 写在成员变量的话 破坏了java的面向对象思维 直接让hibernate访问内部的私有元素 要是能直接设指不合适哈哈 所以主张写在get方法上 ...

随机推荐

  1. Qt之Windows开发移植问题汇总

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101auw6.html 在用Qt开发完成项目后,就需要将其打包并且移植在其他机器上,能在其他PC机上正常跑起来才 ...

  2. 扫描局域网内的ip和主机名

    1. 目的 今天发现我配置的一台电脑ip被人占用了,所以准备写个程序扫描一下局域网内所有正在使用的ip和主机名 2. 实现--直接上代码 import time import threading im ...

  3. Gym 100917L Liesbeth and the String 规律&&胡搞

    题目: Description standard input/outputStatements Little Liesbeth likes to play with strings. Initiall ...

  4. php如何获取本地手机号

    <?php function inquiry_number_infor($phonenumber) /* *传入手机号码,通过API的到xml格式数据,对xml进一步解析,最后返回相应的号码信息 ...

  5. Python -- Web -- 使用框架

    Python的web框架有很多: Flask,Django,Zope2,Web.py,Web2py,Pyramid,Bottle, Tornado... Flask 轻量级,比较简单 from fla ...

  6. posix信号量(sem_t)

    引言 信号量分为三种:posix有名信号量(使用Posix IPC名字标识,至少具有随内核的持续性)/posix基于内存的信号量(共享内存,随进程的持续性)/System V 信号量(内核) 有了互斥 ...

  7. dage手法之 头部和banner ad tpl_header

    <div class="top2"> <?php if ($current_page_base == 'index' || $current_page_base ...

  8. Chapter 1 First Sight——12

    Breakfast with Charlie was a quiet event. 和查理斯吃早饭时一件安静的事情 He wished me good luck at school. I thanke ...

  9. L7,too late

    words: parcel,包裹 detective,侦探 expect,期待 airfield,飞机起落的场地 guard,警戒,守卫,n precious,adj,珍贵的 stone,石头 exp ...

  10. Django之路:QuerySet API,后台和表单

    一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...