免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 808 | 回复: 0
打印 上一主题 下一主题

Struts2+Spring2+Hibernate3 web应用示例(二) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-15 17:33 |只看该作者 |倒序浏览

三、       建立数据持久化层
1、编写实体类Books及books.hbm.xml映射文件。

package com.sterning.books.model;


import java.util.Date;



public class Books

{

    //    Fields

    private String bookId;//编号

    private String bookName;//书名

    private String bookAuthor;//作者

    private String bookPublish;//出版社

    private Date bookDate;//出版日期

    private String bookIsbn;//ISBN

    private String bookPage;//页数

    private String bookPrice;//价格

    private String bookContent;//内容提要

   

    //    Constructors


    public Books()

{}

   

    //    Property accessors



    public String getBookId()

{

        return bookId;

    }



    public void setBookId(String bookId)

{

        this.bookId = bookId;

    }



    public String getBookName()

{

        return bookName;

    }



    public void setBookName(String bookName)

{

        this.bookName = bookName;

    }



    public String getBookAuthor()

{

        return bookAuthor;

    }



    public void setBookAuthor(String bookAuthor)

{

        this.bookAuthor = bookAuthor;

    }



    public String getBookContent()

{

        return bookContent;

    }



    public void setBookContent(String bookContent)

{

        this.bookContent = bookContent;

    }



    public Date getBookDate()

{

        return bookDate;

    }



    public void setBookDate(Date bookDate)

{

        this.bookDate = bookDate;

    }



    public String getBookIsbn()

{

        return bookIsbn;

    }



    public void setBookIsbn(String bookIsbn)

{

        this.bookIsbn = bookIsbn;

    }



    public String getBookPage()

{

        return bookPage;

    }



    public void setBookPage(String bookPage)

{

        this.bookPage = bookPage;

    }



    public String getBookPrice()

{

        return bookPrice;

    }



    public void setBookPrice(String bookPrice)

{

        this.bookPrice = bookPrice;

    }



    public String getBookPublish()

{

        return bookPublish;

    }



    public void setBookPublish(String bookPublish)

{

        this.bookPublish = bookPublish;

    }

}

  com.sterning.books.model.Books.java
       接下来要把实体类Books的属性映射到books表,编写下面的books.hbm.xml文件:

xml version="1.0"?>

DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


hibernate-mapping>

     class name="com.sterning.books.model.Books" table="books" >

         id name="bookId" type="string">

            column name="book_id" length="5" />

            generator class="assigned" />

        id>

        property name="bookName" type="string">

            column name="book_name" length="100" />

        property>

         property name="bookAuthor" type="string">

            column name="book_author" length="100" />

        property>

        property name="bookPublish" type="string">

            column name="book_publish" length="100" />

        property>

         property name="bookDate" type="java.sql.Timestamp">

            column name="book_date" length="7" />

        property>

          property name="bookIsbn" type="string">

            column name="book_isbn" length="20" />

        property>

        property name="bookPage" type="string">

            column name="book_page" length="11" />

        property>

        property name="bookPrice" type="string">

            column name="book_price" length="4" />

        property>

         property name="bookContent" type="string">

            column name="book_content" length="100" />

        property>

     class>

hibernate-mapping>

        com.sterning.books.model.books.hbm.xml
2、hibernate.cfg.xml配置文件如下:(注意它的位置在scr/hibernate.cfg.xml

xml version="1.0" encoding="ISO-8859-1"?>

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="show_sql">trueproperty>


    mapping resource="com/sterning/books/model/books.hbm.xml">mapping>

session-factory>

hibernate-configuration>

  Com.sterning.bean.hibernate.hibernate.cfg.xml
四、       建立DAO
DAO访问层负责封装底层的数据访问细节,不仅可以使概念清晰,而且可以提高开发效率。
1、建立DAO的接口类:BooksDao

package com.sterning.books.dao.iface;


import java.util.List;


import com.sterning.books.model.Books;



public interface BooksDao

{

    List getAll();//获得所有记录

    List getBooks(int pageSize, int startRow);//获得所有记录

    int getRows();//获得总行数

    int getRows(String fieldname,String value);//获得总行数

    List queryBooks(String fieldname,String value);//根据条件查询

    List getBooks(String fieldname,String value,int pageSize, int startRow);//根据条件查询

    Books getBook(String bookId);//根据ID获得记录

    String getMaxID();//获得最大ID值

    void addBook(Books book);//添加记录

    void updateBook(Books book);//修改记录

    void deleteBook(Books book);//删除记录   

}

  com.sterning.books.dao.iface.BooksDao.java

2、实现此接口的类文件,BooksMapDao

package com.sterning.books.dao.hibernate;


import java.sql.SQLException;

import java.util.Iterator;

import java.util.List;


import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import com.sterning.books.dao.iface.BooksDao;

import com.sterning.books.model.Books;

import com.sterning.commons.PublicUtil;




/** *//**

* @author cwf

*

*/


public class BooksMapDao extends HibernateDaoSupport implements BooksDao

{



    public BooksMapDao()

{}



    /** *//**

     * 函数说明:添加信息

     * 参数说明:对象

     * 返回值:

     */


    public void addBook(Books book)

{

        this.getHibernateTemplate().save(book);

    }



    /** *//**

     * 函数说明:删除信息

     * 参数说明: 对象

     * 返回值:

     */


    public void deleteBook(Books book)

{

        this.getHibernateTemplate().delete(book);

    }



    /** *//**

     * 函数说明:获得所有的信息

     * 参数说明:

     * 返回值:信息的集合

     */


    public List getAll()

{

        String sql="FROM Books ORDER BY bookName";

        return this.getHibernateTemplate().find(sql);

    }

   


    /** *//**

     * 函数说明:获得总行数

     * 参数说明:

     * 返回值:总行数

     */


    public int getRows()

{

        String sql="FROM Books ORDER BY bookName";

        List list=this.getHibernateTemplate().find(sql);

        return list.size();

    }

   


    /** *//**

     * 函数说明:获得所有的信息

     * 参数说明:

     * 返回值:信息的集合

     */


    public List getBooks(int pageSize, int startRow) throws HibernateException

{

        final int pageSize1=pageSize;

        final int startRow1=startRow;


        return this.getHibernateTemplate().executeFind(new HibernateCallback()

{



            public List doInHibernate(Session session) throws HibernateException, SQLException

{

                // TODO 自动生成方法存根

                Query query=session.createQuery("FROM Books ORDER BY bookName");

                query.setFirstResult(startRow1);

                query.setMaxResults(pageSize1);

                return query.list();

            }

        });

    }



    /** *//**

     * 函数说明:获得一条的信息

     * 参数说明: ID

     * 返回值:对象

     */


    public Books getBook(String bookId)

{

        return (Books)this.getHibernateTemplate().get(Books.class,bookId);

    }



    /** *//**

     * 函数说明:获得最大ID

     * 参数说明:

     * 返回值:最大ID

     */


    public String getMaxID()

{

        String date=PublicUtil.getStrNowDate();

        String sql="SELECT MAX(bookId)+1 FROM Books  ";

        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

{

            noStr=noStr;

        }

        return noStr;

    }



    /** *//**

     * 函数说明:修改信息

     * 参数说明: 对象

     * 返回值:

     */


    public void updateBook(Books pd)

{

        this.getHibernateTemplate().update(pd);

    }



    /** *//**

     * 函数说明:查询信息

     * 参数说明: 集合

     * 返回值:

     */


    public List queryBooks(String fieldname,String value)

{

        System.out.println("value: "+value);

        String sql="FROM Books where "+fieldname+" like '%"+value+"%'"+"ORDER BY bookName";

        return this.getHibernateTemplate().find(sql);

    }

   


    /** *//**

     * 函数说明:获得总行数

     * 参数说明:

     * 返回值:总行数

     */


    public int getRows(String fieldname,String value)

{

        String sql="";

        if(fieldname==null||fieldname.equals("")||fieldname==null||fieldname.equals(""))

            sql="FROM Books ORDER BY bookName";

        else   

            sql="FROM Books where "+fieldname+" like '%"+value+"%'"+"ORDER BY bookName";

        List list=this.getHibernateTemplate().find(sql);

        return list.size();

    }

   


    /** *//**

     * 函数说明:查询信息

     * 参数说明: 集合

     * 返回值:

     */


    public List getBooks(String fieldname,String value,int pageSize, int startRow)

{

        final int pageSize1=pageSize;

        final int startRow1=startRow;

        final String queryName=fieldname;

        final String queryValue=value;

        String sql="";

        

        if(queryName==null||queryName.equals("")||queryValue==null||queryValue.equals(""))

            sql="FROM Books ORDER BY bookName";

        else   

            sql="FROM Books where "+fieldname+" like '%"+value+"%'"+"ORDER BY bookName";

        

        final String sql1=sql;


        return this.getHibernateTemplate().executeFind(new HibernateCallback()

{



            public List doInHibernate(Session session) throws HibernateException, SQLException

{

                // TODO 自动生成方法存根

                Query query=session.createQuery(sql1);

                query.setFirstResult(startRow1);

                query.setMaxResults(pageSize1);

                return query.list();

            }

        });

    }


}

com.sterning.books.dao.hibernate.BooksMapDao.java
未完待续.下篇写业务逻辑层 。。。。。。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/63390/showart_497163.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP