- /** */ /**
- * 函数说明:获得一段记录信息
- * 参数说明:
- * 返回值:信息的集合
- */
- public List getProducts ( int pageSize, int startRow) throws HibernateException {
- final int pageSizepageSize1 = pageSize;
- final int startRowstartRow1 = startRow;
- return this .getHibernateTemplate().executeFind( new HibernateCallback() {
- public List doInHibernate(Session session) throws HibernateException, SQLException {
- Query query = session.createQuery( " FROM Products ORDER BY gameNameCn " );
- query.setFirstResult(startRow1);
- query.setMaxResults(pageSize1);
- return query.list();
- }
- } );
- }
- /** */ /**
- * 函数说明:获得一条的信息
- * 参数说明: ID
- * 返回值:对象
- */
- public Products getProduct(String gameId) {
- return (Products) this .getHibernateTemplate().get(Products. class ,gameId);
- }
- /** */ /**
- * 函数说明:获得最大ID
- * 参数说明:
- * 返回值:最大ID
- */
- public String getMaxID() {
- String sql = " SELECT MAX(gameId)+1 FROM Products " ;
- String noStr = null ;
- List ll = (List) this .getHibernateTemplate().find(sql);
- Iterator itr = ll.iterator();
- if (itr.hasNext()) {
- Object noint = itr.next();
- if (noint == null ) {
- noStr = " 1 " ;
- } else {
- noStr = noint.toString();
- }
- }
- if (noStr.length() == 1 ) {
- noStr = " 000 " + noStr;
- } else if (noStr.length() == 2 ) {
- noStr = " 00 " + noStr;
- } else if (noStr.length() == 3 ) {
- noStr = " 0 " + noStr;
- } else {
- noStrnoStr = noStr;
- }
- return noStr;
- }
- /** */ /**
- * 函数说明:修改信息
- * 参数说明: 对象
- * 返回值:
- */
- public void updateProductd(Products pd) {
- this .getHibernateTemplate().update(pd);
- }
- /** */ /**
- * 函数说明:查询的所有信息
- * 参数说明: 集合
- * 返回值:
- */
- public List queryProducts(String fieldname,String value) {
- System.out.println( " value: " + value);
- String sql = " FROM Products where " + fieldname + " like '% " + value + " %' " + " ORDER BY gameNameCn " ;
- return this .getHibernateTemplate().find(sql);
- }
- /** */ /**
- * 函数说明:获得总行数
- * 参数说明:
- * 返回值:总行数
- */
- public int getRows(String fieldname,String value) {
- String sql = " FROM Products where " + fieldname + " like '% " + value + " %' " + " ORDER BY gameNameCn " ;
- List list = this .getHibernateTemplate().find(sql);
- return list.size();
- }
- /** */ /**
- * 函数说明:查询的一段信息
- * 参数说明: 集合
- * 返回值:
- */
- public List queryProducts(String fieldname,String value, int pageSize, int startRow) {
- final int pageSizepageSize1 = pageSize;
- final int startRowstartRow1 = startRow;
- final String sql = " FROM Products where " + fieldname + " like '% " + value + " %' " + " ORDER BY gameNameCn " ;
- return this .getHibernateTemplate().executeFind( new HibernateCallback() {
- public List doInHibernate(Session session) throws HibernateException, SQLException {
- Query query = session.createQuery(sql);
- query.setFirstResult(startRow1);
- query.setMaxResults(pageSize1);
- return query.list();
- }
- } );
- }
- }
在com.game.bean.hibernate包中新建hibernate.cfg.xml,代码如下:
- <? xml version="1.0" encoding="GB2312" ?>
- <! DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
- < hibernate-configuration >
- < session-factory >
- < property name ="dialect" > org.hibernate.dialect.SQLServerDialect </ property >
- < property name ="show_sql" > true </ property >
- < mapping resource ="com/game/products/model/products.hbm.xml" ></ mapping >
- </ session-factory >
- </ hibernate-configuration >