- 浏览: 421997 次
- 性别:
文章分类
最新评论
-
wrw_ahut:
你这个怎么停下来?怎么跳转activity
Android启动界面实现 -
huiqoo:
高仿UC半透明MENU菜单(有图有真相) -
wenzhixin:
多谢分享~
Android启动界面实现 -
orange9711:
学习学习
Android启动界面实现 -
qinweiping:
问下楼主 好像这个反编译 只能看到程序的主体结构吧 还是和源程 ...
APK反编译利器Apktool下载及用法介绍
struts+spring+hibernate实现数据库记分页显示
要分页就必须要有取数据库的起始位置和取多少记录,还有要有总记录
用spring来结合hibernate实现分页,
首先创建一个PageBean.java:
package com.binghe.spring;
public class PageBean {
private int count = 0; // 记录总数
private int pageSize = 20; // 每页显示记录数
private int pageCount = 0; // 总页数
private int page = 1; // 当前页数
private String totalCountSQL;// 得到总记录数sql语句
private String listSQL;// 得到查询记录sql语句
public int getCount() {
return count;
}
public void setCount(int count) {
if (pageSize != 0) {
pageCount = count / pageSize;
if (count % pageSize != 0) {
pageCount++;
}
}
this.count = count;
}
public String getListSQL() {
return listSQL;
}
public void setListSQL(String listSQL) {
this.listSQL = listSQL;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getTotalCountSQL() {
return totalCountSQL;
}
public void setTotalCountSQL(String totalCountSQL) {
this.totalCountSQL = totalCountSQL;
}
}
第二,创建一个接口:PaginateInterface.java
package com.binghe.spring;
import java.io.Serializable;
import java.util.List;
public interface PaginateInterface extends Serializable {
public List getList(PageBean page);
public String getToolsMenu(PageBean page);
public int getTotalCount(PageBean p, String str[], Object ob2[])
throws Exception;
public int getTotalCount(PageBean page) throws Exception;
public List getList(PageBean page, String str[], Object ob2[])
throws Exception;
}
第三,创建一个继承spring 的org.springframework.orm.hibernate3.support.HibernateDaoSupport的类Paginate.java,原码如下
package com.binghe.spring;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class Paginate extends HibernateDaoSupport implements PaginateInterface {
/**
* 显示用的菜单
*/
public String getToolsMenu(PageBean p) {
StringBuffer str = new StringBuffer("");
int next, prev;
pprev = p.getPage() - 1;
next = p.getPage() + 1;
if (p.getPage() > 1) {
str
.append("<a href=\"#\" onclick=\"document.forms(0).pages.value=1;document.forms(0).submit();\">首页</a> ");
} else {
str.append("<a href=\"#\">首页</a> ");
}
if (p.getPage() > 1) {
str.append("<a href=\"#\" onclick='document.forms(0).pages.value="
+ prev + ";document.forms(0).submit();'>上页</a> ");
} else {
str.append("<a href=\"#\">上页</a> ");
}
if (p.getPage() < p.getPageCount()) {
str.append("<a href=\"#\" onclick='document.forms(0).pages.value="
+ next + ";document.forms(0).submit();'>下页</a> ");
} else {
str.append("<a href=\"#\" >下页</a> ");
}
if (p.getPageCount() > 1 && p.getPage() != p.getPageCount()) {
str.append("<a href=\"#\" onclick='document.forms(0).pages.value="
+ p.getPageCount()
+ ";document.forms(0).submit();'>末页</a> ");
} else {
str.append("<a href=\"#\" >末页</a> ");
}
str.append(" 共" + p.getCount() + "条记录");
str
.append(" 每页<SELECT size=1 name=pagesize onchange='this.form.pages.value=1;this.form.pageSize.value=this.value;this.form.submit();'>");
if (p.getPageSize() == 3) {
str.append("<OPTION value=3 selected>3</OPTION>");
} else {
str.append("<OPTION value=3>3</OPTION>");
}
if (p.getPageSize() == 10) {
str.append("<OPTION value=10 selected>10</OPTION>");
} else {
str.append("<OPTION value=10>10</OPTION>");
}
if (p.getPageSize() == 20) {
str.append("<OPTION value=20 selected>20</OPTION>");
} else {
str.append("<OPTION value=20>20</OPTION>");
}
if (p.getPageSize() == 50) {
str.append("<OPTION value=50 selected>50</OPTION>");
} else {
str.append("<OPTION value=50>50</OPTION>");
}
if (p.getPageSize() == 100) {
str.append("<OPTION value=100 selected>100</OPTION>");
} else {
str.append("<OPTION value=100>100</OPTION>");
}
str.append("</SELECT>");
str.append("条 分" + p.getPageCount() + "页显示 转到");
str
.append("<SELECT size=1 name=Pagelist onchange='this.form.pages.value=this.value;this.form.submit();'>");
for (int i = 1; i < p.getPageCount() + 1; i++) {
if (i == p.getPage()) {
str.append("<OPTION value=" + i + " selected>" + i
+ "</OPTION>");
} else {
str.append("<OPTION value=" + i + ">" + i + "</OPTION>");
}
}
str.append("</SELECT>页");
str.append("<INPUT type=hidden value=" + p.getPage()
+ " name=\"pages\" > ");
str.append("<INPUT type=hidden value=" + p.getPageSize()
+ " name=\"pageSize\"> ");
return str.toString();
}
public int getTotalCount(PageBean p) throws Exception {
List list = getHibernateTemplate().find(p.getTotalCountSQL());
int count = 0;
if (list.size() > 0) {
count = ((Integer) list.get(0)).intValue();
}
return count;
}
public List getList(PageBean p) {
Session session = this.getSession();
Query q = session.createQuery(p.getListSQL());
q.setFirstResult((p.getPage() - 1) * p.getPageSize());
q.setMaxResults(p.getPageSize());
return q.list();
}
public List getList(PageBean p, String str[], Object ob2[]) {
Session session = this.getSession();
Query q = session.createQuery(p.getListSQL());
for (int i = 0; i < str.length; i++) {
q.setParameter(str[i], ob2[i]);
}
q.setFirstResult((p.getPage() - 1) * p.getPageSize());
q.setMaxResults(p.getPageSize());
return q.list();
}
public int getTotalCount(PageBean p, String str[], Object ob2[])
throws Exception {
List list = getHibernateTemplate().findByNamedParam(
p.getTotalCountSQL(), str, ob2);
int count = 0;
if (list.size() > 0) {
count = ((Integer) list.get(0)).intValue();
}
return count;
}
}
这样就定义好了分页的准备工作,下面就是怎样使用了。在spring中配置好,上面的借口要注入的地方,这是在spring配置文件中目标代码的配置代码:
<bean id="paginateProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="paginate" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
注入的地方:在bookTypeDAO中要调用目标方法
<bean id="bookTypeDAO"
class="com.binghe.spring.booktype.BookTypeDAOImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<property name="paginate">
<ref bean="paginateProxy" />
</property>
</bean>
在action中得到页面传来的页数和页面显示的记录数:
public ActionForward displayAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
ActionMessages errors = new ActionMessages();
UserBean ub = new UserBean();
if (session.getAttribute(Constants.SESSION_USER) != null) {
ub = (UserBean) session.getAttribute(Constants.SESSION_USER);
} else {
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"system.logon.not.logon"));
saveMessages(request, errors);
return mapping.findForward(Constants.FORWARD_LOGON);
}
PageBean page = new PageBean();
String pagesize = request.getParameter("pagesize");
String pages = request.getParameter("pages");
if (pagesize != null) {
page.setPageSize(Integer.parseInt(pagesize));
}
if (pages != null) {
page.setPage(Integer.parseInt(pages));
}
try {
List bts = bookTypeDAO.findAllBookType(page);
request.setAttribute(Constants.KEY_BOOKTYPES, bts);
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("toolsmenu", bookTypeDAO.getPaginate()
.getToolsMenu(bookTypeDAO.getPageBean()));
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
下面是BookDAO接口代码:
package com.binghe.spring.booktype;
import java.io.Serializable;
import java.util.List;
import com.binghe.hibernate.booktype.BookType;
import com.binghe.spring.PageBean;
import com.binghe.spring.PaginateInterface;
public interface BookTypeDAO extends Serializable {
public abstract PaginateInterface getPaginate();
public abstract PageBean getPageBean();
public abstract boolean checkBookTypeIsValid(String name) throws Exception;
public abstract void addBookType(BookType bookType) throws Exception;
public abstract void updateBookType(BookType bookType) throws Exception;
public abstract void deleteBookType(Integer id) throws Exception;
public abstract void deleteAllBookType(String ids[]) throws Exception;
public abstract BookType findBookTypeById(Integer id) throws Exception;
public abstract List findAllBookType() throws Exception;
public abstract List findAllBookType(PageBean page) throws Exception;
}
下面是BookDAOImp中的调用代码:
package com.binghe.spring.booktype;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.binghe.hibernate.booktype.BookType;
import com.binghe.utils.DeptBean;
import com.binghe.spring.PageBean;
import com.binghe.spring.Paginate;
import com.binghe.spring.PaginateInterface;
import com.binghe.utils.UserBean;
public class BookTypeDAOImp extends HibernateDaoSupport implements BookTypeDAO {
private PaginateInterface paginate;
private PageBean pageBean = new PageBean();
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public PaginateInterface getPaginate() {
return paginate;
}
public void setPaginate(PaginateInterface paginate) {
this.paginate = paginate;
}
public void addBookType(BookType bookType) throws Exception {
this.getHibernateTemplate().save(bookType);
}
public boolean checkBookTypeIsValid(String name) throws Exception {
String names[] = new String[1];
names[0] = name;
boolean valid = false;
List list = this.getHibernateTemplate().find(
"select b.id from BookType b where b.name=?", names);
if (list.size() > 0) {
valid = true;
}
return valid;
}
public void updateBookType(BookType bookType) throws Exception {
this.getHibernateTemplate().update(bookType);
}
public BookType findBookTypeById(Integer id) throws Exception {
String sql = "select b,u.userName,d.deptName "
+ " from BookType b,UserBean u,DeptBean d "
+ " where b.userId=u.OID and u.deptOID=d.OID and b.id=:id";
List list = this.getHibernateTemplate().findByNamedParam(sql, "id", id);
Iterator it = list.iterator();
BookType b = null;
UserBean u = new UserBean();
DeptBean d = new DeptBean();
if (it.hasNext()) {
Object o[] = (Object[]) it.next();
b = (BookType) o[0];
u.setUserName((String) o[1]);
d.setDeptName((String) o[2]);
u.setDept(d);
b.setUser(u);
}
return b;
}
public List findAllBookType() throws Exception {
List bookTypes = new ArrayList();
String sql = "select b,u.userName,d.deptName "
+ " from BookType b,UserBean u,DeptBean d "
+ " where b.userId=u.OID and u.deptOID=d.OID";
List list = this.getHibernateTemplate().find(sql);
Iterator it = list.iterator();
while (it.hasNext()) {
Object o[] = (Object[]) it.next();
BookType b = null;
UserBean u = new UserBean();
DeptBean d = new DeptBean();
b = (BookType) o[0];
u.setUserName((String) o[1]);
d.setDeptName((String) o[2]);
u.setDept(d);
b.setUser(u);
bookTypes.add(b);
}
if (bookTypes.size() > 0)
return bookTypes;
return null;
}
public List findAllBookType(PageBean p) throws Exception {
List bookTypes = new ArrayList();
String sql = "select b,u.userName,d.deptName "
+ " from BookType b,UserBean u,DeptBean d "
+ " where b.userId=u.OID and u.deptOID=d.OID";
String sql2 = "select count(*) from BookType";
p.setListSQL(sql);
p.setTotalCountSQL(sql2);
p.setCount(this.getPaginate().getTotalCount(p));
this.setPageBean(p);
List list = this.getPaginate().getList(p);
Iterator it = list.iterator();
while (it.hasNext()) {
Object o[] = (Object[]) it.next();
BookType b = null;
UserBean u = new UserBean();
DeptBean d = new DeptBean();
b = (BookType) o[0];
u.setUserName((String) o[1]);
d.setDeptName((String) o[2]);
u.setDept(d);
b.setUser(u);
bookTypes.add(b);
}
if (bookTypes.size() > 0)
return bookTypes;
return null;
}
public void deleteBookType(Integer id) throws Exception {
BookType bookType = (BookType) this.getHibernateTemplate().load(
BookType.class, id);
getHibernateTemplate().delete(bookType);
}
public void deleteAllBookType(String ids[]) throws Exception {
List list = new ArrayList();
for (int i = 0; i < ids.length; i++) {
BookType bookType = (BookType) this.getHibernateTemplate().load(
BookType.class, new Integer(ids[i]));
list.add(i, bookType);
}
getHibernateTemplate().deleteAll(list);
}
}
这是我写的一个项目中抽取的代码,上面全是代码形式没有文字说明,如果有经验的话我觉得会看懂的,看不懂的话可以发表评论。
要分页就必须要有取数据库的起始位置和取多少记录,还有要有总记录
用spring来结合hibernate实现分页,
首先创建一个PageBean.java:
package com.binghe.spring;
public class PageBean {
private int count = 0; // 记录总数
private int pageSize = 20; // 每页显示记录数
private int pageCount = 0; // 总页数
private int page = 1; // 当前页数
private String totalCountSQL;// 得到总记录数sql语句
private String listSQL;// 得到查询记录sql语句
public int getCount() {
return count;
}
public void setCount(int count) {
if (pageSize != 0) {
pageCount = count / pageSize;
if (count % pageSize != 0) {
pageCount++;
}
}
this.count = count;
}
public String getListSQL() {
return listSQL;
}
public void setListSQL(String listSQL) {
this.listSQL = listSQL;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getTotalCountSQL() {
return totalCountSQL;
}
public void setTotalCountSQL(String totalCountSQL) {
this.totalCountSQL = totalCountSQL;
}
}
第二,创建一个接口:PaginateInterface.java
package com.binghe.spring;
import java.io.Serializable;
import java.util.List;
public interface PaginateInterface extends Serializable {
public List getList(PageBean page);
public String getToolsMenu(PageBean page);
public int getTotalCount(PageBean p, String str[], Object ob2[])
throws Exception;
public int getTotalCount(PageBean page) throws Exception;
public List getList(PageBean page, String str[], Object ob2[])
throws Exception;
}
第三,创建一个继承spring 的org.springframework.orm.hibernate3.support.HibernateDaoSupport的类Paginate.java,原码如下
package com.binghe.spring;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class Paginate extends HibernateDaoSupport implements PaginateInterface {
/**
* 显示用的菜单
*/
public String getToolsMenu(PageBean p) {
StringBuffer str = new StringBuffer("");
int next, prev;
pprev = p.getPage() - 1;
next = p.getPage() + 1;
if (p.getPage() > 1) {
str
.append("<a href=\"#\" onclick=\"document.forms(0).pages.value=1;document.forms(0).submit();\">首页</a> ");
} else {
str.append("<a href=\"#\">首页</a> ");
}
if (p.getPage() > 1) {
str.append("<a href=\"#\" onclick='document.forms(0).pages.value="
+ prev + ";document.forms(0).submit();'>上页</a> ");
} else {
str.append("<a href=\"#\">上页</a> ");
}
if (p.getPage() < p.getPageCount()) {
str.append("<a href=\"#\" onclick='document.forms(0).pages.value="
+ next + ";document.forms(0).submit();'>下页</a> ");
} else {
str.append("<a href=\"#\" >下页</a> ");
}
if (p.getPageCount() > 1 && p.getPage() != p.getPageCount()) {
str.append("<a href=\"#\" onclick='document.forms(0).pages.value="
+ p.getPageCount()
+ ";document.forms(0).submit();'>末页</a> ");
} else {
str.append("<a href=\"#\" >末页</a> ");
}
str.append(" 共" + p.getCount() + "条记录");
str
.append(" 每页<SELECT size=1 name=pagesize onchange='this.form.pages.value=1;this.form.pageSize.value=this.value;this.form.submit();'>");
if (p.getPageSize() == 3) {
str.append("<OPTION value=3 selected>3</OPTION>");
} else {
str.append("<OPTION value=3>3</OPTION>");
}
if (p.getPageSize() == 10) {
str.append("<OPTION value=10 selected>10</OPTION>");
} else {
str.append("<OPTION value=10>10</OPTION>");
}
if (p.getPageSize() == 20) {
str.append("<OPTION value=20 selected>20</OPTION>");
} else {
str.append("<OPTION value=20>20</OPTION>");
}
if (p.getPageSize() == 50) {
str.append("<OPTION value=50 selected>50</OPTION>");
} else {
str.append("<OPTION value=50>50</OPTION>");
}
if (p.getPageSize() == 100) {
str.append("<OPTION value=100 selected>100</OPTION>");
} else {
str.append("<OPTION value=100>100</OPTION>");
}
str.append("</SELECT>");
str.append("条 分" + p.getPageCount() + "页显示 转到");
str
.append("<SELECT size=1 name=Pagelist onchange='this.form.pages.value=this.value;this.form.submit();'>");
for (int i = 1; i < p.getPageCount() + 1; i++) {
if (i == p.getPage()) {
str.append("<OPTION value=" + i + " selected>" + i
+ "</OPTION>");
} else {
str.append("<OPTION value=" + i + ">" + i + "</OPTION>");
}
}
str.append("</SELECT>页");
str.append("<INPUT type=hidden value=" + p.getPage()
+ " name=\"pages\" > ");
str.append("<INPUT type=hidden value=" + p.getPageSize()
+ " name=\"pageSize\"> ");
return str.toString();
}
public int getTotalCount(PageBean p) throws Exception {
List list = getHibernateTemplate().find(p.getTotalCountSQL());
int count = 0;
if (list.size() > 0) {
count = ((Integer) list.get(0)).intValue();
}
return count;
}
public List getList(PageBean p) {
Session session = this.getSession();
Query q = session.createQuery(p.getListSQL());
q.setFirstResult((p.getPage() - 1) * p.getPageSize());
q.setMaxResults(p.getPageSize());
return q.list();
}
public List getList(PageBean p, String str[], Object ob2[]) {
Session session = this.getSession();
Query q = session.createQuery(p.getListSQL());
for (int i = 0; i < str.length; i++) {
q.setParameter(str[i], ob2[i]);
}
q.setFirstResult((p.getPage() - 1) * p.getPageSize());
q.setMaxResults(p.getPageSize());
return q.list();
}
public int getTotalCount(PageBean p, String str[], Object ob2[])
throws Exception {
List list = getHibernateTemplate().findByNamedParam(
p.getTotalCountSQL(), str, ob2);
int count = 0;
if (list.size() > 0) {
count = ((Integer) list.get(0)).intValue();
}
return count;
}
}
这样就定义好了分页的准备工作,下面就是怎样使用了。在spring中配置好,上面的借口要注入的地方,这是在spring配置文件中目标代码的配置代码:
<bean id="paginateProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="paginate" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
注入的地方:在bookTypeDAO中要调用目标方法
<bean id="bookTypeDAO"
class="com.binghe.spring.booktype.BookTypeDAOImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<property name="paginate">
<ref bean="paginateProxy" />
</property>
</bean>
在action中得到页面传来的页数和页面显示的记录数:
public ActionForward displayAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
ActionMessages errors = new ActionMessages();
UserBean ub = new UserBean();
if (session.getAttribute(Constants.SESSION_USER) != null) {
ub = (UserBean) session.getAttribute(Constants.SESSION_USER);
} else {
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"system.logon.not.logon"));
saveMessages(request, errors);
return mapping.findForward(Constants.FORWARD_LOGON);
}
PageBean page = new PageBean();
String pagesize = request.getParameter("pagesize");
String pages = request.getParameter("pages");
if (pagesize != null) {
page.setPageSize(Integer.parseInt(pagesize));
}
if (pages != null) {
page.setPage(Integer.parseInt(pages));
}
try {
List bts = bookTypeDAO.findAllBookType(page);
request.setAttribute(Constants.KEY_BOOKTYPES, bts);
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("toolsmenu", bookTypeDAO.getPaginate()
.getToolsMenu(bookTypeDAO.getPageBean()));
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
下面是BookDAO接口代码:
package com.binghe.spring.booktype;
import java.io.Serializable;
import java.util.List;
import com.binghe.hibernate.booktype.BookType;
import com.binghe.spring.PageBean;
import com.binghe.spring.PaginateInterface;
public interface BookTypeDAO extends Serializable {
public abstract PaginateInterface getPaginate();
public abstract PageBean getPageBean();
public abstract boolean checkBookTypeIsValid(String name) throws Exception;
public abstract void addBookType(BookType bookType) throws Exception;
public abstract void updateBookType(BookType bookType) throws Exception;
public abstract void deleteBookType(Integer id) throws Exception;
public abstract void deleteAllBookType(String ids[]) throws Exception;
public abstract BookType findBookTypeById(Integer id) throws Exception;
public abstract List findAllBookType() throws Exception;
public abstract List findAllBookType(PageBean page) throws Exception;
}
下面是BookDAOImp中的调用代码:
package com.binghe.spring.booktype;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.binghe.hibernate.booktype.BookType;
import com.binghe.utils.DeptBean;
import com.binghe.spring.PageBean;
import com.binghe.spring.Paginate;
import com.binghe.spring.PaginateInterface;
import com.binghe.utils.UserBean;
public class BookTypeDAOImp extends HibernateDaoSupport implements BookTypeDAO {
private PaginateInterface paginate;
private PageBean pageBean = new PageBean();
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public PaginateInterface getPaginate() {
return paginate;
}
public void setPaginate(PaginateInterface paginate) {
this.paginate = paginate;
}
public void addBookType(BookType bookType) throws Exception {
this.getHibernateTemplate().save(bookType);
}
public boolean checkBookTypeIsValid(String name) throws Exception {
String names[] = new String[1];
names[0] = name;
boolean valid = false;
List list = this.getHibernateTemplate().find(
"select b.id from BookType b where b.name=?", names);
if (list.size() > 0) {
valid = true;
}
return valid;
}
public void updateBookType(BookType bookType) throws Exception {
this.getHibernateTemplate().update(bookType);
}
public BookType findBookTypeById(Integer id) throws Exception {
String sql = "select b,u.userName,d.deptName "
+ " from BookType b,UserBean u,DeptBean d "
+ " where b.userId=u.OID and u.deptOID=d.OID and b.id=:id";
List list = this.getHibernateTemplate().findByNamedParam(sql, "id", id);
Iterator it = list.iterator();
BookType b = null;
UserBean u = new UserBean();
DeptBean d = new DeptBean();
if (it.hasNext()) {
Object o[] = (Object[]) it.next();
b = (BookType) o[0];
u.setUserName((String) o[1]);
d.setDeptName((String) o[2]);
u.setDept(d);
b.setUser(u);
}
return b;
}
public List findAllBookType() throws Exception {
List bookTypes = new ArrayList();
String sql = "select b,u.userName,d.deptName "
+ " from BookType b,UserBean u,DeptBean d "
+ " where b.userId=u.OID and u.deptOID=d.OID";
List list = this.getHibernateTemplate().find(sql);
Iterator it = list.iterator();
while (it.hasNext()) {
Object o[] = (Object[]) it.next();
BookType b = null;
UserBean u = new UserBean();
DeptBean d = new DeptBean();
b = (BookType) o[0];
u.setUserName((String) o[1]);
d.setDeptName((String) o[2]);
u.setDept(d);
b.setUser(u);
bookTypes.add(b);
}
if (bookTypes.size() > 0)
return bookTypes;
return null;
}
public List findAllBookType(PageBean p) throws Exception {
List bookTypes = new ArrayList();
String sql = "select b,u.userName,d.deptName "
+ " from BookType b,UserBean u,DeptBean d "
+ " where b.userId=u.OID and u.deptOID=d.OID";
String sql2 = "select count(*) from BookType";
p.setListSQL(sql);
p.setTotalCountSQL(sql2);
p.setCount(this.getPaginate().getTotalCount(p));
this.setPageBean(p);
List list = this.getPaginate().getList(p);
Iterator it = list.iterator();
while (it.hasNext()) {
Object o[] = (Object[]) it.next();
BookType b = null;
UserBean u = new UserBean();
DeptBean d = new DeptBean();
b = (BookType) o[0];
u.setUserName((String) o[1]);
d.setDeptName((String) o[2]);
u.setDept(d);
b.setUser(u);
bookTypes.add(b);
}
if (bookTypes.size() > 0)
return bookTypes;
return null;
}
public void deleteBookType(Integer id) throws Exception {
BookType bookType = (BookType) this.getHibernateTemplate().load(
BookType.class, id);
getHibernateTemplate().delete(bookType);
}
public void deleteAllBookType(String ids[]) throws Exception {
List list = new ArrayList();
for (int i = 0; i < ids.length; i++) {
BookType bookType = (BookType) this.getHibernateTemplate().load(
BookType.class, new Integer(ids[i]));
list.add(i, bookType);
}
getHibernateTemplate().deleteAll(list);
}
}
这是我写的一个项目中抽取的代码,上面全是代码形式没有文字说明,如果有经验的话我觉得会看懂的,看不懂的话可以发表评论。
发表评论
-
Flex表格分页封装例子
2009-05-21 11:34 3347网上找了篇Flex分页封装的例子,可以参考使用,并可参照实现其 ... -
AppFuse项目笔记(1)
2006-12-21 17:40 1863一、Appfuse简介 Appfuse是M ... -
使用AppFuse的七个理由(下)
2006-12-21 17:47 1584理由 5:使用 AppGen 生成 ... -
使用 AppFuse 的七个理由(上)
2006-12-21 17:58 1207AppFuse 是一个开放源码的项目和应用程序, ... -
appfuse例子一步步解析!结合eclipse3.0一起使用
2006-12-21 18:02 1737Appfuse应用的 ... -
Struts+Hibernate分页的实现
2006-12-22 16:35 3841在进行web应用开发的时候经常要进行分页处理,经常看到一些人在 ...
相关推荐
《图书管理系统spring+struts+hibernate》是一款基于Java技术栈开发的图书管理软件,其核心框架包括Spring、Struts和Hibernate。该系统利用MySQL作为数据库存储数据,提供了完整的数据库备份,确保了数据的安全性与...
Struts2 Spring Hibernate IBatis Struts2 Spring Hibernate IBatisStruts2 Spring Hibernate IBatisStruts2 Spring Hibernate IBatis 只需要导入相应的jar包就行了 ,数据库是mysql :数据库名叫做mydatabase,表名...
例如,struts.xml中配置Action类及其结果视图,applicationContext.xml中配置Spring Bean,而hibernate.cfg.xml则定义了数据库连接和实体类映射。 在本项目中,"说明.txt"文件可能包含了详细的步骤指导,包括如何...
【标题】"Struts Spring Hibernate 航空票务系统"是一个典型的Java Web应用程序,它利用了三个核心的开源框架——Struts、Spring和Hibernate,来构建一个完整的航空票务预订系统。Struts作为MVC(Model-View-...
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
简单的功能 实现了对数据库的增删改操作 数据库用的mysql。高手请绕道. 保证能运行 里面包含jar包 现在网上找的例子很少有带JAR包的。我也是个菜鸟,找例子的时候费了很大劲!现在将做出来的和大家分享下 希望大家也...
Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自...通过这个例子,开发者不仅可以学习到如何整合Struts2、Spring和Hibernate,还能掌握如何实现数据的添加和分页显示,对Java Web开发有更深入的理解。
整合这三个框架的目的是提高开发效率,降低维护成本,通过Spring的IoC和AOP特性,实现组件间的解耦和事务管理,而Hibernate则简化了数据库操作。在实际项目中,这种整合提供了强大的后端架构,能够应对复杂的业务...
Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自负责不同的职责:Struts2作为MVC框架处理请求和展示,Spring提供依赖注入和事务管理,Hibernate则作为ORM框架处理数据库操作。将这三个框架整合在...
Struts、Spring 和 Hibernate 是Java Web开发中的三大主流框架,它们各司其职,共同构建了一个高效、可扩展且易于管理的Web应用程序。Struts作为MVC(模型-视图-控制器)架构的实现,主要负责处理用户请求并进行业务...
一个spring+struts+hibernate的例子,是eclipse的工程,用tomcat5和mysql,文件夹下包含所有的源码和库,另外还有一个.sql的文件用于建立数据库。大家觉得它有什么不好,欢迎交流
Struts提供MVC架构,Spring负责依赖注入和事务管理,而Hibernate则处理数据库交互。此外,对于Hibernate的使用,推荐安装Hibernate Synchronizer Eclipse Plugin,它可以帮助开发者更便捷地同步Hibernate实体类和...
在sshdemo这个文件夹中,可能包含的文件有struts-config.xml、spring-beans.xml、hibernate.cfg.xml、数据库脚本、Action类、DAO接口及其实现、服务类、以及相关的JSP页面等,这些都是SSH整合用户注册功能的关键组成...
总的来说,这个“Struts+Spring+Hibernate 集成实现的经典权限管理系统”提供了一个完整的解决方案,展示了如何在Java Web应用中有效地管理权限,同时也为开发者提供了一个学习和实践整合这三大框架的实例。...
Java EE是企业级应用开发的重要框架,而"精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码"的资源提供了丰富的实践学习材料,旨在帮助开发者深入了解和掌握Java后端开发技术。这个项目案例涵盖了四...
这个系统的核心架构是使用了经典的Java企业级开发框架组合:Struts、Hibernate、Spring以及Oracle数据库。接下来,我们将详细讨论这些技术在车辆管理系统中的作用和相关知识点。 1. Struts: Struts是Apache组织...
Spring、Hibernate和Struts是Java开发中非常重要的三个框架,它们构成了经典的MVC(Model-View-Controller)架构,被广泛应用于企业级Web应用开发。这些框架极大地简化了开发过程,提高了开发效率,并且提供了良好的...
具体而言,Spring可以通过其强大的IoC容器管理Struts和Hibernate组件,实现组件之间的解耦。例如,Spring可以配置Struts的Action对象,使其能够注入必要的业务逻辑服务;同时,Spring还可以配置Hibernate的...
【Spring+Struts+Hibernate 实现文件上传和下载】 在 J2EE 开发中,文件的上传和下载是一项基础但重要的功能。SSH(Struts、Spring、Hibernate)框架提供了便捷的方式来处理这一问题,无需依赖像 SmartUpload 或 ...