该程序是在网上下载的部分代码经过修改之后得到的,在运行该程序的时候会出现部分问题,在此不做详细的解释,如果有不懂的问题可以QQ:283365371一起来商讨.
1.Book.java文件源代码:
package com.book;
public class Book {
private String id;
private String name;
private String isbm;
private String author;
public Book(){
}
/**
* 部分初始化
* @param id
* @param name
* @param isbm
*/
public Book(String id, String name, String isbm) {
this.id = id;
this.name = name;
this.isbm = isbm;
}
/**
* 全部初始化
* @param id
* @param name
* @param isbm
* @param author
*/
public Book(String id, String name, String isbm, String author) {
this.id = id;
this.name = name;
this.isbm = isbm;
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIsbm() {
return isbm;
}
public void setIsbm(String isbm) {
this.isbm = isbm;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "[id=" + id + ",name=" + name + ",isbm=" + isbm + ",author=" + author + "]";
}
}
2.BookDAO.java源代码:
package com.book;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BookDAO extends HibernateDaoSupport{
private static final Log log = LogFactory.getLog(BookDAO.class);
public static final String NAME = "name";
public static final String ISBM = "isbm";
public static final String AUTHOR = "author";
protected void initDao(){
}
public void save(Book transientInstance){
log.debug("saving Book instance");
try{
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re){
log.error("save failed", re);
throw re;
}
}
public void delete(Book persistentInstance){
log.debug("deleting Book instance");
try{
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re){
log.error("delete failed", re);
throw re;
}
}
public Book findById(java.lang.Long id){
log.debug("getting Book instance with id: " + id);
try{
Book instance = (Book) getHibernateTemplate().get(
"edu.jlu.fuliang.domain.Book", id);
return instance;
} catch (RuntimeException re){
log.error("get failed", re);
throw re;
}
}
public List findByExample(Book instance){
log.debug("finding Book instance by example");
try{
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re){
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value){
log.debug("finding Book instance with property: " + propertyName
+ ", value: " + value);
try{
String queryString = "from Book as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re){
log.error("find by property name failed", re);
throw re;
}
}
public List findByName(Object name){
return findByProperty(NAME, name);
}
public List findByIsbm(Object isbm){
return findByProperty(ISBM, isbm);
}
public List findByAuthor(Object author){
return findByProperty(AUTHOR, author);
}
public List findAll(){
log.debug("finding all Book instances");
try{
String queryString = "from Book";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re){
log.error("find all failed", re);
throw re;
}
}
public Book merge(Book detachedInstance){
log.debug("merging Book instance");
try{
Book result = (Book) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re){
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Book instance){
log.debug("attaching dirty Book instance");
try{
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re){
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Book instance){
log.debug("attaching clean Book instance");
try{
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re){
log.error("attach failed", re);
throw re;
}
}
public static BookDAO getFromApplicationContext(ApplicationContext ctx){
return (BookDAO) ctx.getBean("BookDAO");
}
}
3.Book.hbm.xml源文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.book">
<class
name="Book"
table="book"
>
<meta attribute="sync-DAO">false</meta>
<id
name="id"
type="string"
column="id"
>
<generator class="uuid.hex"/>
</id>
<property
name="name"
column="name"
type="string"
not-null="false"
length="20"
/>
<property
name="isbm"
column="isbm"
type="string"
not-null="false"
length="20"
/>
<property
name="author"
column="author"
type="string"
not-null="false"
length="15"
/>
</class>
</hibernate-mapping>
4.三个配置文件
(A)applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="bookDAO" class="com.book.BookDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
(B)applicationContext-db.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://168.168.28.247:1433;DatabaseName=TestYu</value>
</property>
<property name="username">
<value>yuqiang</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<value>com/book/Book.hbm.xml</value>
</property>
</bean>
</beans>
(C)applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="bookManageServiceTarget" class="com.service.BookManageServiceImpl">
<property name="bookDAO">
<ref bean="bookDAO"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="bookManageService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="bookManageServiceTarget"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
5.BookManageService.java文件
package com.service;
import java.util.List;
import com.book.Book;
public interface BookManageService {
public List<Book> getAllBooks();
public List<Book> getBookByName(String name);
public void updateBook(Book book);
public void addBook(Book book);
public void deleteBook(long id);
}
6.BookManageServiceImpl.java文件
package com.service;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.book.Book;
import com.book.BookDAO;
public class BookManageServiceImpl implements BookManageService {
private BookDAO bookDAO;
public void addBook(Book book) {
bookDAO.save(book);
}
public void deleteBook(long id) {
Book book = bookDAO.findById(id);
bookDAO.delete(book);
}
public List<Book> getAllBooks() {
return bookDAO.findAll();
}
public List<Book> getBookByName(String name) {
return bookDAO.findByName(name);
}
public void updateBook(Book book) {
bookDAO.attachDirty(book);
}
public BookDAO getBookDAO() {
return bookDAO;
}
public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
}
其中还有两个配置文件(web.xml和dwr.xml文件)
7.web.xml源文件代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext-db.xml,/WEB-INF/classes/applicationContext-dao.xml,/WEB-INF/classes/applicationContext-service.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<description></description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
8.dwr.xml文件
<dwr>
<allow>
<convert converter="bean" match="com.book.Book"/>
<create creator="spring" javascript="BookManageService" >
<param name="beanName" value="bookManageService"/>
<include method="getAllBooks"/>
<include method="getBookByName"/>
<include method="updateBook"/>
<include method="addBook"/>
<include method="deleteBook"/>
</create>
</allow>
</dwr>
9.测试的主页面
<html>
<head><title>DWR test</title>
<script type='text/javascript' src='/spring/dwr/interface/BookManageService.js'></script>
<script type='text/javascript' src='/spring/dwr/engine.js'></script>
<script type='text/javascript' src='/spring/dwr/util.js'></script>
<script type="text/javascript">
var bookCache = {};
var count=1;
var cellFuncs =[
function(data) {return count++;},
function(data) { return data.name;},
function(data) { return data.isbm;},
function(data) { return data.author;}
];
function loadAllBooks(){
freshTable();
}
var freshTable=function(){
alert("===11111====");
BookManageService.getAllBooks(fillTable);
alert("===22222====");
}
var fillTable=function(rows){
alert("===33333====");
DWRUtil.removeAllRows("rows");
alert("===44444====");
DWRUtil.addRows("rows",rows,cellFuncs);
}
function addBook(){
var book = {name:null,isbm:null,author:null};
DWRUtil.getValues(book);
DWREngine.beginBatch();
BookManageService.addBook(book);
loadAllBooks();
DWREngine.endBatch();
}
</script>
</head>
<body οnlοad="loadAllBooks()">
<div>
<h2>Add book</h2>
<table>
<tr>
<td>Name:</td><td><input type="text" id="name"></td>
</tr>
<tr>
<td>ISBN:</td>
<td><input type="text" id="isbm"></td>
</tr>
<tr>
<td>Author:</td>
<td><input type="text" id="author"></td>
</tr>
<tr>
<td colspan="2">
<input type="button" id="add" value="Add" οnclick="addBook()">
<input type="reset" id="update" value="Back" >
</td>
</tr>
</table>
</div>
<hr>
<div id="list">
<table border="1">
<thead>
<tr>
<th>Num</th>
<th>Name</th>
<th>ISBN</th>
<th>Author</th>
</tr>
</thead>
<tbody id="rows">
</tbody>
</table>
</div>
</body>