<host id='yourweb:8080' app-dir='e:\blog\' class-update-interval='2'> <web-app id='/'> <directory-servlet id='false'/> </web-app> <error-log id='log/web-error.log'/> <host> |
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- The Usual Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-nested</taglib-uri> <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-tiles</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib> </web-app> |
hibernate2.jar commons-beanutils.jar commons-collections.jar commons-dbcp.jar commons-lang.jar commons-logging.jar commons-pool.jar dom4j.jar cglib-asm.jar connector.jar |
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/yourdatabase </property> <property name="hibernate.connection.driver_class"> org.gjt.mm.mysql.Driver </property> <property name="hibernate.connection.username"> yourname </property> <property name="hibernate.connection.password"> youyrpassword </property> <!?- 是否将运行期生成的SQL输出到日志以供调试--> <property name="show_sql">false</property> <!--dialect ,每个数据库都有其对应的Dialet以匹配其平台特性--> <propertyname="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <!?- 事务管理类型,这里我们使用JDBC Transaction --> <property name="transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory</property> <property name="jdbc.batch_size">10</property> <property name="jdbc.use_scrollable_resultset">true</property> <!?映射文件配置,注意配置文件名必须包含其相对于根的全路径--> <mapping resource="net/seerlog/hbm/Cat.hbm.xml"/> </session-factory> </hibernate-configuration> |
<!DOCTYPE project [ <!ENTITY database SYSTEM "file:./config/database/hsqldb.xml"> ]> |
<property name="name" value="airline"/> |
<property name="build.gen-src.dir" value="${build.dir}/gen-src"/> |
<hibernate destination="${build.gen-src.dir}" package="${name}.hibernate" genXDocletTags="false" genIntergratedCompositeKeys="false" javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper" /> |
### 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 %c{1}:%L - %m%n #log4j.appender.stdout.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %l%n%m%n log4j.logger.seerlog=DEBUG,A2 log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender log4j.appender.A2.file=d:\\log\\applog.txt log4j.appender.A2.DatePattern='.'yyyy-MM-dd log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %m%n #log4j.logger.ALL=DEBUG,A1 log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.file=d:\\log\\all.log log4j.appender.A1.DatePattern='.'yyyy-MM-dd log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %l%n%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.rootLogger=info, A1 #以下是hibernate API需要的配置 log4j.logger.net.sf.hibernate=info ### log just the SQL #log4j.logger.net.sf.hibernate.SQL=debug ### log JDBC bind parameters ### log4j.logger.net.sf.hibernate.type=info ### log schema export/update ### log4j.logger.net.sf.hibernate.tool.hbm2ddl=debug ### log cache activity ### #log4j.logger.net.sf.hibernate.cache=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.net.sf.hibernate.connection.DriverManagerConnectionProvider=trac |
String prefix = context.getRealPath("/"); PropertyConfigurator.configure(prefix+"/WEB-INF/classes/log4j.properties"); |
static Logger log=Logger.getLogger("seerlog"); log.warn("it's log4j warn"); log.info("it's log4j info"); |
public class Hi{ private static final SessionFactory sessionFactory; private static Connection conn; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (HibernateException ex) { throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex); } } public static final ThreadLocal session = new ThreadLocal(); public static Session getSession() throws HibernateException { Session s = (Session) session.get(); // 假如没有一个可用的线程,开启一个新 Session, if (s == null) { conn=DBConnectionManager.getConnection(); s = sessionFactory.openSession(conn); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); session.set(null); if (s != null) s.close(); if(conn!=null) DBConnectionManager.returnConnection(conn); } } |
try{ //开启一个会话,并启动事务回滚能力 Session session = Hi.getSession(); Transaction tx= session.beginTransaction(); //查询数据库,使用HQL Query q = session.createQuery("from Topic as topic order by topic.id desc"); q.setFirstResult(0); q.setMaxResults(5); //得到一个List类型的数据 List newTopicList = q.list(); // 业务处理 NewTopicList ntlBean=new NewTopicList(); ntlBean.setList(newTopicList); req.setAttribute("newtopiclist",ntlBean); //事务提交,记得关闭会话! tx.commit(); Hi.closeSession(); }//因为整个会话会抛出异常,所以需要用try--catch来包裹 catch(HibernateException e){ //日志记录 Log.error(e.toString()); } |
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="net.seerlog.vo.LoginForm"/> <form-bean name="replyForm" type="net.seerlog.vo.ReplyForm"/> </form-beans> <global-forwards> <forward name="index" path="/first.jsp"/> </global-forwards> <action-mappings> <action path="/login" type="net.seerlog.action.LoginAction" scope="request" name="loginForm" input="/login.jsp"> <forward name="success" path="/success.jsp"/> </action> <action path="/blog" type="net.seerlog.action.BlogAction" scope="request"> <forward name="success" path="/sublog.jsp"/> </action> <action path="/topic" type="net.seerlog.action.TopicAction" scope="request"> <forward name="success" path="/sutopic.jsp"/> </action> <action path="/posttopic" type="net.seerlog.action.PostTopicAction" scope="request" > </action> <action path="/postreply" type="net.seerlog.action.PostReplyAction" scope="request" name="replyForm" input="/sublog.jsp"> </action> <action path="/index" type="net.seerlog.action.IndexAction" scope="request"> </action> </action-mappings> <message-resources parameter="resouse.application"> </message-resources> </struts-config> |
//导入所有用的到的包 import java.util.List; import net.seerlog.po.*; import net.seerlog.vo.*; import net.seerlog.util.*; import net.sf.hibernate.*; import org.apache.struts.action.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import javax.servlet.ServletException; public class IndexAction extends Action{ //action的主体方法 execute ,注意要抛出异常 public ActionForward execute (ActionMapping mapping,ActionForm form,HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ try{ //开启会话 Session session = Hi.getSession(); Transaction tx= session.beginTransaction(); //查询 Query q = session.createQuery("from Topic as topic order by topic.id desc"); q.setFirstResult(0); q.setMaxResults(5); List newTopicList = q.list(); NewTopicList ntlBean=new NewTopicList(); ntlBean.setList(newTopicList); req.setAttribute("newtopiclist",ntlBean); //事务提交,关闭session tx.commit(); Hi.closeSession(); //捕获异常 }catch(HibernateException e){ Log.error(e.toString()); //日志记录 } //返回一个 actionForward 对象 return mapping.findForward("index"); } } |