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 [%t] %c{1}:%L - %m%n log4j.rootLogger=warn, stdout log4j.logger.cn.edu.hpu=debug
package cn.edu.hpu.tax.test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; public class TestLog { @Test public void test(){ Log log=LogFactory.getLog(getClass()); log.debug("debug 级别日志"); log.info("info 级别日志"); log.warn("warn 级别日志"); log.error("error 级别日志"); log.fatal("fatal 级别日志"); } }
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File=F:/tax/tax.log log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d [%t] %5p %c - %m%n
下面这个
package cn.edu.hpu.tax.test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; public class TestLog { @Test public void test(){ Log log=LogFactory.getLog(getClass()); try { int i=1/0; } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); } } }
我们如果做好了log日志记录,到时候出错的时候我们会在日志记录文件中看到下面的语句:
2015-10-20 20:03:33,431 [main] ERROR cn.edu.hpu.tax.test.TestLog - / by zero
package cn.edu.hpu.tax.core.dao; import java.io.Serializable; import java.util.List; public interface BaseDao<T> { //新增 public void save(T entity); //更新 public void update(T enetity); //根据id删除 public void delete(Serializable id); //根据id查找 public T findObjectById(Serializable id); //查找列表 public List<T> findObjects(); }
package cn.edu.hpu.tax.core.dao.impl; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; import org.hibernate.Query; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.edu.hpu.tax.core.dao.BaseDao; public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T>{ Class<T> clazz; //拿到泛型实体类T public BaseDaoImpl(){ //BaseDaoImpl<User>获取此类泛型框中的第一个泛型 ParameterizedType pt=(ParameterizedType)this.getClass().getGenericSuperclass(); clazz=(Class<T>)pt.getActualTypeArguments()[0]; } @Override public void save(Object entity) { getHibernateTemplate().save(entity); } @Override public void update(Object enetity) { getHibernateTemplate().update(enetity); } @Override public void delete(Serializable id) { getHibernateTemplate().delete(findObjectById(id)); } @Override public T findObjectById(Serializable id) { return getHibernateTemplate().get(clazz, id); } @Override public List<T> findObjects() { Query query=getSession() .createQuery("FROM "+clazz.getSimpleName()); return query.list(); } }
这就是我们的基础增删改查类。详细的功能开发下一篇继续。
转载请注明出处:http://blog.csdn.net/acmman/article/details/49312581