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. perl的USE和require

    来源: http://www.cnblogs.com/itech/archive/2010/11/22/1884345.html 相同: 都可以用来引用module(.PM). 不同: 1) 区别在于 ...

  2. SmtpDlg 调用SMTP

    // SmtpDlg.h : 头文件 // #pragma once #include "afxwin.h" #include "string" using n ...

  3. overlay

    http://dockone.io/article/237 http://blog.cloud66.com/docker-with-overlayfs-first-impression/ http:/ ...

  4. OpenLayer 3 鼠标位置坐标显示控件

    <body> <div id="map"> <div id="mouse-position"></div> &l ...

  5. 1988: Sn 爆long long 的处理方法

    题目描述 给你两个数 n, p(0 < n,p <= 10^15); a1 = 1;  a2 = 1+2;  a3 = 1+2+3;  ... an = 1+2+3+...+n    Sn ...

  6. CentOS下载及版本选择-CentOS LiveCD、LiveDVD和BinDVD区别

    1.CentOS系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.x-i386-bin-DVD1.iso(32位)或者CentOS-6.x-x86_64-bin-DVD1.iso(64位), ...

  7. mysql问题总结,远程登录

    http://blog.sina.com.cn/s/blog_4550f3ca0101axzd.html 更改mysql数据库的数据库名 http://tech.sina.com.cn/s/s/200 ...

  8. ubuntu环境下安装Tomcat

    tomcat 是javaweb开发的本地服务器,tomcat是目前比较流行的一款. 1.下载Tomcat:http://tomcat.apache.org 2.进入下载文件夹解压Tomcat:sudo ...

  9. 2015年4月29日 dayofweek

    #include <stdio.h>#include <stdlib.h>int DayofYear(int year, int month, int day);#define ...

  10. cocos2d-x 3.3 显示中文

    Resources文件夹下的strings.xml: <dict> <key>targetScore</key> <string>目标分数</st ...