在Struts 2中实现CRUD(Struts 2系列之十)

 

CRUD是Create(创建)、Read(读取)、Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影。如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了,所以大家使用新框架开发OLTP(Online Transaction Processing)应用程序时,首先会研究一下如何编写CRUD。这类似于大家在学习新编程语言时喜欢编写“Hello World”。

本文旨在讲述Struts 2上的CRUD开发,所以为了例子的简单易懂,我不会花时间在数据库的操作上。取而代之的是一个模拟数据库的哈希表(Hash Map)。

具体实现

首先,让我们看看的“冒牌”的DAO(Data Access Object,数据访问对象),代码如下:

None.gif package tutorial.dao;
None.gif
None.gif
import java.util.Collection;
None.gif
import java.util.concurrent.ConcurrentHashMap;
None.gif
import java.util.concurrent.ConcurrentMap;
None.gif
None.gif
import tutorial.model.Book;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class BookDao dot.gif {
InBlock.gif    
private static final BookDao instance;
InBlock.gif    
private static final ConcurrentMap<String, Book> data;
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
static dot.gif{
InBlock.gif        instance
= new BookDao();
InBlock.gif        data
= new ConcurrentHashMap<String, Book>();
InBlock.gif        data.put(
"978-0735619678", new Book("978-0735619678", "Code Complete, Second Edition", 32.99));
InBlock.gif        data.put(
"978-0596007867", new Book("978-0596007867", "The Art of Project Management", 35.96));
InBlock.gif        data.put(
"978-0201633610", new Book("978-0201633610", "Design Patterns: Elements of Reusable Object-Oriented Software", 43.19));
InBlock.gif        data.put(
"978-0596527341", new Book("978-0596527341", "Information Architecture for the World Wide Web: Designing Large-Scale Web Sites", 25.19));
InBlock.gif        data.put(
"978-0735605350", new Book("978-0735605350", "Software Estimation: Demystifying the Black Art", 25.19));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private BookDao() dot.gif{}
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static BookDao getInstance() dot.gif{
InBlock.gif        
return instance;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Collection<Book> getBooks() dot.gif{
InBlock.gif        
return data.values();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Book getBook(String isbn) dot.gif{
InBlock.gif        
return data.get(isbn);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void storeBook(Book book) dot.gif{
InBlock.gif        data.put(book.getIsbn(), book);
ExpandedSubBlockEnd.gif    }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void removeBook(String isbn) dot.gif{
InBlock.gif        data.remove(isbn);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void removeBooks(String[] isbns) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(String isbn : isbns) dot.gif{
InBlock.gif            data.remove(isbn);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
清单1 src/tutorial/dao/BookDao.java

以上代码相信不用解释大家也清楚,我使用ConcurrentMap数据结构存储Book对象,这主要是为了方便检索和保存Book对象;另外,我还将data变量设为静态唯一来模拟应用程序的数据库。

接下来是的数据模型Book类,代码如下:

None.gif package tutorial.model;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class Book dot.gif {
InBlock.gif    
private String isbn;
InBlock.gif    
private String title;
InBlock.gif    
private double price;
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Book() dot.gif{        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Book(String isbn, String title, double price) dot.gif{
InBlock.gif        
this.isbn = isbn;
InBlock.gif        
this.title = title;
InBlock.gif        
this.price = price;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getIsbn() dot.gif{
InBlock.gif        
return isbn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setIsbn(String isbn) dot.gif{
InBlock.gif        
this.isbn = isbn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public double getPrice() dot.gif{
InBlock.gif        
return price;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setPrice(double price) dot.gif{
InBlock.gif        
this.price = price;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getTitle() dot.gif{
InBlock.gif        
return title;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setTitle(String title) dot.gif{
InBlock.gif        
this.title = title;
ExpandedSubBlockEnd.gif    }
   
ExpandedBlockEnd.gif}
清单2 src/tutorial/model/Book.java

Book类有三个属性isbn,、title和price分别代表书籍的编号、名称和价格,其中编号用于唯一标识书籍(相当数据库中的主键)。

然后,我们再来看看Action类的代码:

None.gif package tutorial.action;
None.gif
None.gif
import java.util.Collection;
None.gif
None.gif
import tutorial.dao.BookDao;
None.gif
import tutorial.model.Book;
None.gif
None.gif
import com.opensymphony.xwork2.ActionSupport;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class BookAction extends ActionSupport dot.gif {
InBlock.gif    
private static final long serialVersionUID = 872316812305356L;
InBlock.gif    
InBlock.gif    
private String isbn;
InBlock.gif    
private String[] isbns;
InBlock.gif    
private Book book;
InBlock.gif    
private Collection<Book> books;
InBlock.gif    
private BookDao dao =  BookDao.getInstance();
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Book getBook() dot.gif{
InBlock.gif        
return book;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setBook(Book book) dot.gif{
InBlock.gif        
this.book = book;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getIsbn() dot.gif{
InBlock.gif        
return isbn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setIsbn(String isbn) dot.gif{
InBlock.gif        
this.isbn = isbn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String[] getIsbns() dot.gif{
InBlock.gif        
return isbns;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setIsbns(String[] isbns) dot.gif{
InBlock.gif        
this.isbns = isbns;
ExpandedSubBlockEnd.gif    }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Collection<Book> getBooks() dot.gif{
InBlock.gif        
return books;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setBooks(Collection<Book> books) dot.gif{
InBlock.gif        
this.books = books;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String load() dot.gif{
InBlock.gif        book
= dao.getBook(isbn);
InBlock.gif        
return SUCCESS;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String list() dot.gif{
OutliningIndicator
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值