#log4j.properties
log4j.rootLogger=debug,info,warn,error,appender1,appender2
log4j.addivity.org.apache=true
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.FileAppender
log4j.appender.debug.File=log/crm_debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=debug
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
#log4j.appender.debug.layout.ConversionPattern=[ %p ] %-d{yyyy-MM-dd HH:mm:ss} [%F:%L] %m%n
log4j.appender.info.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.FileAppender
log4j.appender.info.File=log/crm_info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=info
log4j.appender.info.layout=org.apache.log4j.PatternLayout
#log4j.appender.info.layout.ConversionPattern=[ %p ] %-d{yyyy-MM-dd HH:mm:ss} [%F:%L] %m%n
log4j.appender.info.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.logger.warn=warn
log4j.appender.warn=org.apache.log4j.FileAppender
log4j.appender.warn.File=log/crm_warn.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=warn
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
#log4j.appender.warn.layout.ConversionPattern=[ %p ] %-d{yyyy-MM-dd HH:mm:ss} [%F:%L] %m%n
log4j.appender.info.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.logger.error=error
log4j.appender.error=org.apache.log4j.FileAppender
log4j.appender.error.File=log/crm_error.log
log4j.appender.error.Append=true
log4j.appender.error.Threshold=error
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L : %m%n
log4j.logger.appender1=appender1
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n
log4j.logger.appender2=appender2
log4j.appender.appender2.Threshold=error
log4j.appender.appender2=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.appender2.driver=com.mysql.jdbc.Driver
log4j.appender.appender2.URL=jdbc:mysql://localhost:3306/iwork?useUnicode=true&characterEncoding=UTF-8
log4j.appender.appender2.user=root
log4j.appender.appender2.password=yoplore
log4j.appender.appender2.sql=insert into logtable (cdate, message) VALUES ('%d{yyyy-MM-dd HH:mm:ss}', '%c %p %m %n')
log4j.appender.appender2.layout=org.apache.log4j.PatternLayout
/**
* code name:DaoSessionBuilder.java
* @author wacky
* @since 2016-9-28 9:24
* @version 0.0.1
*/
package com.wacky.dao.impl;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 静态化SessionFactory,从hibernate.cfg.xml中读取配置信息
*/
public class DaoSessionBuilder {
private static SessionFactory sessionFactory;
static {
// 步骤一:创建Configuration--准备读取hibernate.cfg.xml内容到内存
Configuration configuration = new Configuration();
// 步骤二:加载域对象--
// 读取hibernate.cfg.xml内容到内存
// 读取Xxx.hbm.xml内容到内存
configuration.configure("/hibernate.cfg.xml");
// 步骤三:创建SessionFactory实例
sessionFactory = configuration.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
/**
* code name: DaoLogImpl.java
* @author wacky
* @since 2016-9-28 9:28
* @version 0.0.1
*/
package com.wacky.dao.impl;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wacky.dao.DaoLog;
import com.wacky.entity.Log;
import com.wacky.model.PageModel;
import com.wacky.model.QueryCount;
/**
* 实现日志的Dao层,具体是实现Hibernate3.0接口及日志入数据库及控制台打印功能。
*/
public class DaoLogImpl extends HibernateDaoSupport implements DaoLog {
//此类的日志记录器
private static Logger logger = Logger.getLogger(DaoLogImpl.class);
//构造Dao的会话工厂
private static SessionFactory sfdao = DaoSessionBuilder.getSessionFactory();
public DaoLogImpl() {
super();
this.setSessionFactory(sfdao);
}
@Override
public void delete(Log log) {
this.getHibernateTemplate().delete(log);
}
@Override
public Log findLog(int id) {
// TODO Auto-generated method stub
return (Log) this.getHibernateTemplate().get(Log.class, id);
}
@Override
public void save(Log log) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(log);
}
@Override
public void update(Log log) {
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
//SessionFactory sf = DaoSessionBuilder.getSessionFactory();
//Session session = sf.openSession();
session.beginTransaction().begin();
session.update(log);
session.beginTransaction().commit();
session.close();
/*this.getHibernateTemplate().update(user);
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
session.beginTransaction().commit();
session.close();*/
}
@Override
public List<Log> findAllLog(String hql) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().find(hql);
}
@Override
public Log checkLog(String hql) {
// TODO Auto-generated method stub
return null;
}
@Override
public PageModel<Log> findByPageScroll(String hql, int firstPage, int maxPage) {
PageModel<Log> pm = new PageModel<Log>();
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
//SessionFactory sf = DaoSessionBuilder.getSessionFactory();
//Session session = sf.openSession();
Query query = session.createQuery(hql);
query.setFirstResult(firstPage); //设置起始行
query.setMaxResults(maxPage); //每页条数
List<Log> listLog = (List<Log>)query.list(); //得到每页的数据
pm.setRecordsDate(listLog);
pm.setTotal(((Long)QueryCount.getQueryCount(hql, session)).intValue());
pm.setCurrentPage(firstPage);
pm.setManPage(maxPage);
session.close();
return pm;
}
public static void main(String[] args) {
String user = "\'iWork" + "\\\\" + "管理员\'";
String hql = " from Log where subject = " + user + " order by created desc";
DaoLog daoLog = new DaoLogImpl();
PageModel<Log> pm = daoLog.findByPageScroll(hql, 5, 20);
//List<Log> listAllLogs = daoLog.findAllLog(hql);
List<Log> listLog = pm.getRecordsDate();
int iid = 0;
for(Log log : listLog) {
log.setId(iid++);
String str = log.getId() + "\t" + log.getUnid() + "\t" + log.getSubject() + "\t" + log.getOwner();
logger.info(str);
logger.warn(str);
logger.debug(str);
}
logger.error("ok, no error!");
}
}
进行Hibernate3.0自测类和功能收集
最新推荐文章于 2024-08-22 20:42:12 发布