struts20 + hibernate + oracle 分页问题Word文件下载.docx
《struts20 + hibernate + oracle 分页问题Word文件下载.docx》由会员分享,可在线阅读,更多相关《struts20 + hibernate + oracle 分页问题Word文件下载.docx(12页珍藏版)》请在冰豆网上搜索。
![struts20 + hibernate + oracle 分页问题Word文件下载.docx](https://file1.bdocx.com/fileroot1/2022-12/7/926016f8-f61b-4d97-abc3-c87a280ff286/926016f8-f61b-4d97-abc3-c87a280ff2861.gif)
PCTFREE10PCTUSED40INITRANS1MAXTRANS255NOCOMPRESSLOGGINGSTORAGE
INITIAL65536NEXT1048576MINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0FREELISTS1FREELISTGROUPS1BUFFER_POOLDEFAULT
TABLESPACESYSTEM;
CREATESEQUENCE"
MINVALUE1MAXVALUE9999INCREMENTBY1STARTWITH121CACHE20NOORDERNOCYCLE;
InsertintoT_USER(ID,USERNAME,PASSWORD)values(Q_USER.nextval,'
222'
'
);
HibernateUtil.java
Java代码
packagehibernate;
importorg.hibernate.HibernateException;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.cfg.Configuration;
/**
*Hibernate的工具类.
*@authorAmigoXie
*/
publicclassHibernateUtil{
privatestaticSessionFactorysessionFactory;
/**持有一个单态的Session实例.
privatestaticfinalThreadLocalthreadLocal=newThreadLocal();
/**持有一个单态的configuration实例.
privatestaticfinalConfigurationcfg=newConfiguration();
privatestaticStringCONFIG_FILE_LOCATION="
hibernate/hibernate.cfg.xml"
static{
try{
sessionFactory=newConfiguration().configure(CONFIG_FILE_LOCATION)
.buildSessionFactory();
}catch(Throwableex){
//Makesureyoulogtheexception,asitmightbeswallowed
System.err.println("
InitialSessionFactorycreationfailed."
+ex);
thrownewExceptionInInitializerError(ex);
}
}
publicstaticSessionFactorygetSessionFactory(){
returnsessionFactory;
/**
*获得当前的Session实例.
*@returnSession
*@throwsHibernateException
publicstaticSessioncurrentSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
//if(session==null){
if(session==null||session.isOpen()==false){
if(sessionFactory==null){
try{
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory=cfg.buildSessionFactory();
catch(Exceptione){
%%%%ErrorCreatingSessionFactory%%%%"
+e.getMessage());
session=sessionFactory.openSession();
threadLocal.set(session);
returnsession;
User.java
publicclassUser{
privateStringid;
privateStringuserName;
privateStringpassword;
publicStringgetId(){
returnid;
publicvoidsetId(Stringid){
this.id=id;
publicStringgetUserName(){
returnuserName;
publicvoidsetUserName(StringuserName){
this.userName=userName;
publicStringgetPassword(){
returnpassword;
publicvoidsetPassword(Stringpassword){
this.password=password;
hibernate.cfg.xml
Xml代码
<
?
xmlversion='
1.0'
encoding='
utf-8'
>
!
DOCTYPEhibernate-configurationPUBLIC
"
-//Hibernate/HibernateConfigurationDTD3.0//EN"
hibernate-configuration>
<
session-factory>
--properties-->
propertyname="
connection.url"
jdbc:
oracle:
thin:
@127.0.0.1:
1521:
JIM<
/property>
connection.username"
scott<
connection.password"
tiger<
dialect"
org.hibernate.dialect.OracleDialect<
connection.driver_class"
oracle.jdbc.driver.OracleDriver<
show_sql"
true<
format_sql"
false<
--mappingfiles-->
mappingresource="
hibernate/user.hbm.xml"
/>
/session-factory>
/hibernate-configuration>
user.hbm.xml
xmlversion="
1.0"
encoding="
UTF-8"
DOCTYPEhibernate-mappingPUBLIC
-//Hibernate/HibernateMappingDTD3.0//EN"
hibernate-mappingpackage="
hibernate"
classname="
User"
table="
idname="
id"
column="
ID"
type="
string"
generatorclass="
assigned"
/id>
userName"
column="
USERNAME"
type="
not-null="
true"
/>
password"
PASSWORD"
/class>
/hibernate-mapping>
IuserDao.java接口
packagepage;
importhibernate.User;
importjava.util.List;
publicinterfaceIuserDao{
publicList<
User>
displayUser(intoffset,intmax);
publicintcount();
UserDao.java
importhibernate.HibernateUtil;
importorg.hibernate.Query;
importorg.hibernate.Transaction;
publicclassUserDaoimplementsIuserDao{
@Override
displayUser(intoffset,intmax){
//TODOAuto-generatedmethodstub
//PagerModel<
pm=null;
List<
list=null;
Stringhsql="
fromUser"
Sessionsession=HibernateUtil.currentSession();
Transactionts=null;
ts=session.beginTransaction();
Queryquery=session.createQuery(hsql);
query.setFirstResult(offset);
query.setMaxResults(max);
list
=query.list();
}catch(HibernateExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
if(ts!
=null){
ts.rollback();
}finally{
session.close();
returnlist;
publicintcount(){
inti=0;
Queryquery=session.createQuery("
selectcount(*)fromUser"
i=(Integer)query.uniqueResult();
returni;
pageAction.java
importcom.opensymphony.xwork2.ActionSupport;
publicclassPageActionextendsActionSupport{
inti=1;
//中间变量
privateintk;
//储存最大页面数
privateintpageNow=1;
//页码数,初始为1
privateintpageSize=5;
//页面行数
privateintintRowCount=0;
//总行数
privateintintPageCount=1;
//总页数
//
privateAdminadmin;
privateList<
Admin>
Adminss;
privateUseruser;
users;
privateintid;
privateintaid;
publicUsergetUser(){
returnuser;
publicvoidsetUser(Useruser){
this.user=user;
publicList<
getUsers(){
returnusers;
publicvoidsetUsers(List<
users){
this.users=users;
publicintgetId(){
publicvoidsetId(intid){
publicintgetAid(){
returnaid;
publicvoidsetAid(intaid){
this.aid=aid;
publicintgetPageNow(){
returnpageNow;
publicvoidsetPageNow(intpageNow){
this.pageNow=pageNow;
publicintgetPageSize(){