《struts2 hibernate分页.doc》由会员分享,可在线阅读,更多相关《struts2 hibernate分页.doc(22页珍藏版)》请在金锄头文库上搜索。
1、1). 针对网上一些 struts2分页的例子 不完整 ,代码混乱繁琐 ,我特意写了一个分页的小例子,供大家参考,如果程序有什么纰漏, 请指出。开发环境:eclipse3.2 + myeclipse5.5mysql5.0tomcat5.5Dreamweaver20042)建库,并导入脚本这里给大家介绍一个mysql客户端工具 - SQLyog ,非常好用。-admin.sql-/*SQLyog Community Edition- MySQL GUI v6.12MySQL - 5.0.37-community-nt : Database - test把这段脚本导入到mysql的test库里面(
2、你可以先建一个test库 ,这样会省去很多麻烦)*/*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=*/;create database if not exists test;USE test;/*!40014 SET OLD_FOREIGN_KEY_CHECKS=FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET OLD_SQL_MODE=SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;/*Table structure for table a
3、dmin */DROP TABLE IF EXISTS admin;CREATE TABLE admin (id int(11) NOT NULL auto_increment,user char(20) default NULL,pass char(20) default NULL,PRIMARY KEY (id) ENGINE=InnoDB DEFAULT CHARSET=gb2312;/*Data for the table admin */insert into admin(id,user,pass) values (786432,天使,#),(786433,天使,#),(786434
4、,天使,#),(786435,天使,#),(786436,天使,#),(819200,天使,#),(819201,天使,#),(819202,天使,#),(819203,天使,#),(819204,天使,#),(819205,wangbacheng,#),(851968,天使,#),(851969,天使,#),(851970,天使,#),(851971,天使,#),(851972,天使,#);/*!40101 SETSQL_MODE=OLD_SQL_MO*/;/*!40014 SETFOREIGN_KEY_CHECKS=OLD_FOREIGN_KEY_CHECKS*/;DE3)建立数据库连接程
5、序 ,也就是hibernate相关程序,都可以用eclipse自动生成,这个不用多说在eclipse 中只要引入 hibernate核心包和struts2 包 就可以了-HibernateSessionFactory.java-package action;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;/* Configures and provides access to Hibernate sessions, tied t
6、o the* current thread of execution. Follows the Thread Local Session* pattern, seelinkhttp:/hibernate.org/42.html.*/public class HibernateSessionFactory /* * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configura
7、tion file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ private static String CONFIG_FILE_LOCATION = /hibernate.cfg.xml;private static final ThreadLocal t
8、hreadLocal = new ThreadLocal(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION;static try configuration.configure(configFile); sessionFactory = configuration.buildSe
9、ssionFactory(); catch (Exception e) System.err .println(% Error Creating SessionFactory %); e.printStackTrace(); private HibernateSessionFactory() /* * Returns the ThreadLocal Session instance. Lazy initialize * the SessionFactory if needed. * * return Session * throws HibernateException */ public s
10、tatic Session getSession() throws HibernateException Session session = (Session) threadLocal.get(); if (session = null | !session.isOpen() if (sessionFactory = null) rebuildSessionFactory(); session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); return se
11、ssion; /* * Rebuild hibernate session factory * */public static void rebuildSessionFactory() try configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); catch (Exception e) System.err .println(% Error Creating SessionFactory %); e.printStackTrace(); /* * Close the
12、single hibernate session instance. * * throws HibernateException */ public static void closeSession() throws HibernateException Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) session.close(); /* * return session factory * */public static org.hibernate.SessionFactory getSessionFactory() return sessionFactory;/* * return session factory * * session factory will be rebuilded in the next call */public static void setConfigFile(String configFile) HibernateSessionFactory.configFile = configFile; sessionFactory = null;/* * return hibernate