發現的確是本好書,下面小結下spring+hiberante+dwr使用時的一些步驟
比如一篇BLOG,可以擁有多個評論,這是典型的一對多關係了,先看model層
1 model層
blog.java
public class Blog
{
private int id;
private String title;
private String content;
private Date addTime;
private Set<Comment> comments = new HashSet<Comment>();
public Blog()
{
}
//get,set等方法
comment.java
public class Comment
{
private int id;
private String user;
private String email;
private String url;
private String content;
private Date addTime;
private Blog blog;
//....get.set等方法
2 hbm文件
blog.hbm.xml:
<class name="Blog" table="blog_table">
<id name="id">
<generator class="identity"/>
</id>
<property name="title"/>
<property name="content" type="text"/>
<property name="addTime"/>
<set name="comments" inverse="true">
<key column="blog_id"/>
<one-to-many class="Comment"/>
</set>
</class>
comment.hbm.xml:
<class name="Comment" table="comment_table">
<id name="id">
<generator class="identity"/>
</id>
<property name="user"/>
<property name="email"/>
<property name="url"/>
<property name="content"/>
<property name="addTime"/>
<many-to-one name="blog" column="blog_id" not-null="true"/>
</class>
3 DAO層
blogdao.java (節省篇幅,舉例子而已)
public interface BlogDao
{
/**
* 根據主鍵加載Blog
* @param id 需要加載的Blog ID
* @return 加載的Blog
*/
Blog get(int id);
/**
* 保存Blog
* @param b 需要保存的Blog
*/
void save(Blog b);
public interface BlogDao
{
/**
* 根據主鍵加載Blog
* @param id 需要加載的Blog ID
* @return 加載的Blog
*/
Blog get(int id);
/**
* 保存Blog
* @param b 需要保存的Blog
*/
void save(Blog b);
。。。。。。
}
blogdaoimpl.java (BLOGADAO接口的實現類)
public class BlogDaoHibernate extends HibernateDaoSupport
implements BlogDao
{
//每頁顯示的消息數
private int pageSize;
//每頁的消息數通過依賴注入管理
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
/**
* 根據主鍵加載Blog
* @param id 需要加載的Blog ID
* @return 加載的Blog
*/
public Blog get(int id)
{
return (Blog)getHibernateTemplate().get(Blog.class , new Integer(id));
}
/**
* 保存Blog
* @param b 需要保存的Blog
*/
public void save(Blog b)
{
getHibernateTemplate().save(b);
}
...........................
4 業務邏輯層
業務邏輯層接口
blogmanager.java
public interface BlogManager
{
/**
* 創建一條Blog文章
* @param title Blog文章的標題
* @param content Blog文章的內容
* @return 新創建Blog文章的主鍵,如果創建失敗,返回-1。
*/
int createBlog(String title , String content)
throws BlogException;
.......................
}
業務邏輯層實現blogmanagerimpl.java
public class BlogManagerImpl implements BlogManager
{
private BlogDao blogDao;
private CommentDao commentDao;
public void setBlogDao(BlogDao blogDao)
{
this.blogDao = blogDao;
}
public void setCommentDao(CommentDao commentDao)
{
this.commentDao = commentDao;
}
/**
* 創建一條Blog文章
* @param title Blog文章的標題
* @param content Blog文章的內容
* @return 新創建Blog文章的主鍵,如果創建失敗,返回-1。
*/
public int createBlog(String title , String content)
throws BlogException
{
try
{
Blog b = new Blog();
b.setTitle(title);
b.setContent(content);
b.setAddTime(new Date());
blogDao.save(b);
return b.getId();
}
catch (Exception e)
{
e.printStackTrace();
throw new BlogException("保存Blog文章出錯");
}
}
5 配置業務邏輯組件
applicationcontext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3309/blog"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="root"/>
<property name="password" value="123456789"/>
<property name="maxPoolSize" value="40"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/>
</bean>
<!--定義了Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>Blog.hbm.xml</value>
<value>Comment.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="blogDao" class="org.yeeku.dao.impl.BlogDaoHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="pageSize" value="3"/>
</bean>
<bean id="commentDao" class="org.yeeku.dao.impl.CommentDaoHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="pageSize" value="3"/>
</bean>
<bean id="blogManager" class="org.yeeku.service.impl.BlogManagerImpl">
<property name="blogDao" ref="blogDao"/>
<property name="commentDao" ref="commentDao"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事務攔截器bean需要依賴注入一個事務管理器 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<!-- 下面定義事務傳播屬性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定義BeanNameAutoProxyCreator-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定對滿足哪些bean name的bean自動生成業務代理 -->
<property name="beanNames">
<!-- 下面是所有需要自動創建事務代理的bean-->
<list>
<value>blogManager</value>
</list>
<!-- 此處可增加其他需要自動創建事務代理的bean-->
</property>
<!-- 下面定義BeanNameAutoProxyCreator所需的事務攔截器-->
<property name="interceptorNames">
<list>
<!-- 此處可增加其他新的Interceptor -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
5 初始化 dwr
web.xml
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置DWR的核心Servlet -->
<servlet>
<!-- 指定DWR核心Servlet的名字 -->
<servlet-name>dwr-invoker</servlet-name>
<!-- 指定DWR核心Servlet的實現類 -->
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<!-- 指定DWR核心Servlet處於調試狀態 -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 指定核心Servlet的URL映射 -->
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<!-- 指定核心Servlet映射的URL -->
<url-pattern>/leedwr/*</url-pattern>
</servlet-mapping>
</web-app>
6 DWR.XML
<dwr>
<allow>
<create creator="spring" javascript="bm">
<param name="beanName" value="blogManager"/>
<include method="createComment"/>
<include method="createBlog"/>
<include method="getAllBlogByPage"/>
<include method="getCommentsByBlogAndPage"/>
<include method="getBlog"/>
<include method="getNewestBlog"/>
</create>
<convert converter="bean" match="org.yeeku.vo.CommentBean"/>
<convert converter="bean" match="org.yeeku.vo.BlogBean"/>
</allow>
</dwr
其中配置了javascript對象bm,該對象由dwr通過spring容器中的blogmanager創建,還定義了兩個javabean,可以被轉化爲javascript對象
7 客戶端調用
比如添加BLOG的功能:
.
/創建新Blog文章
function addBlog()
{
var blogTitle = $("blogTitle").value;
var blogContent = $("blogContent").value;
if (blogTitle == null || blogTitle == "" || blogContent == null || blogContent == "")
{
alert("新增Blog文章時,必須指定文章標題,文章內容");
}
else
{
bm.createBlog(blogTitle , blogContent , addBlogCb);
}
}
//創建新Blog文章的回調函數
function addBlogCb(data)
{
if (typeof data != "number" || data < 1)
{
alert("添加文章失敗");
}
else
{
$("blogTitle").value = "";
$("blogContent").value = ""
alert("添加文章成功");
window.close();
}