java的model层实例_Struts 2.1.6 精简实例系列教程(3):新闻管理Model层的开发(整合iBatis)...
本期開始講Model層的開發,整合iBatis框架,iBatis是Apache旗下Java數據持久層的框架,跟Hibernate是同一類型的框架。大家可到它的官方網站去下載http://ibatis.apache.org/java.cgi,如下圖:
我這里下載的是當前最新版本iBatis 2.3.4 , 下載之后,解壓包是這樣的:
我們在lib目錄下,找到“ibatis-2.3.4.726.jar”文件,加入到我們項目的lib目錄下,就行。在這里,我們先說下怎么學習這個iBatis框架:上圖中,有個simple_example的文件夾,它里面就包含了一個超級簡單且容易理解的例子,大家可以去學習一下。By the way,如果你學過Hibernate的話,你會發覺iBatis要比Hibernate好學很多。關于Hibernate和iBatis的爭論,網上有很多,大家有興趣可以去了解一下。
好,我們先建立數據庫和設計數據庫吧。我這項目用的是MySQL 5.0。生成數據庫和數據表的SQL語句如下:
create database simpledb;
create table article
(
ID int auto_increment not null primary key,
TITLE varchar(25),
AUTHOR varchar(25),
CONTENT text,
PUBTIME date
);
這是我們常見的新聞表及其中的字段。
接下來,寫一個與表對應的新聞類,Article.java,這個其實是POJO類,代碼如下:
packagecn.simple.pojo;
importjava.util.Date;
publicclassArticle{
????
privateintid;
privateString?title;
privateString?author;
privateString?content;
privateDate?pubtime;
????
/**?*//***********getter和setter方法***********/
publicintgetId(){
returnid;
????}
publicvoidsetId(intid){
this.id=id;
????}
publicString?getTitle(){
returntitle;
????}
publicvoidsetTitle(String?title){
this.title=title;
????}
publicString?getAuthor(){
returnauthor;
????}
publicvoidsetAuthor(String?author){
this.author=author;
????}
publicString?getContent(){
returncontent;
????}
publicvoidsetContent(String?content){
this.content=content;
????}
publicDate?getPubtime(){
returnpubtime;
????}
publicvoidsetPubtime(Date?pubtime){
this.pubtime=pubtime;
????}????
}
有了數據表和實體類,現在來寫兩者之間映射的配置文件Article.xml。代碼如下:
<?xml ?version="1.0"?encoding="UTF-8"?>
sqlMap??????
????PUBLIC?"-//ibatis.apache.org//DTD?SQL?Map?2.0//EN"??????
????"http://ibatis.apache.org/dtd/sql-map-2.dtd">
????????select?*?from?article
????????select
????????ID?as?id,
????????TITLE?as?title,
????????AUTHOR?as?author,
????????CONTENT?as?content,
????????PUBTIME?as?pubtime
????????from?Article
????????where?ID=#id#
????????insert?into?article?(
????????????TITLE,
????????????AUTHOR,
????????????CONTENT,
????????????PUBTIME
????????)?values?(
????????????#title#,
????????????#author#,
????????????#content#,
????????????#pubtime#
????????)
????????update?article?set
????????TITLE?=?#title#,
????????AUTHOR?=?#author#,
????????CONTENT?=?#content#,
????????PUBTIME?=?#pubtime#
????????where
????????ID?=?#id#
????????delete?from?article?where?ID?=?#id#
大家不要覺得這個映射文件很復雜,其實,這挺容易理解的,如果大家賴得寫的話,可復制iBatis自帶的simple_example下的例子的映射文件,然后修改一下就行。
有了表、實體類、表與實體之間的映射文件,之后,該做什么呢?學過Hibernate的朋友會想到那個數據庫連接信息的配置文件,當然,iBatis也需要類似的文件,即SqlMapConfig.xml,代碼如下:
<?xml ?version="1.0"?encoding="UTF-8"?>
sqlMapConfig??????
????PUBLIC?"-//ibatis.apache.org//DTD?SQL?Map?Config?2.0//EN"??????
????"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
一看這代碼,也有點復雜,我的說法同上,大不了COPY,再略作修改,呵呵
好了,來寫我們的業務邏輯層:
packagecn.simple.manager;
importjava.io.IOException;
importjava.io.Reader;
importjava.sql.SQLException;
importjava.util.List;
importcn.simple.pojo.Article;
importcom.ibatis.common.resources.Resources;
importcom.ibatis.sqlmap.client.SqlMapClient;
importcom.ibatis.sqlmap.client.SqlMapClientBuilder;
publicclassArticleManager{
/**?*//**?????*?SqlMapClient?instances?are?thread?safe,?so?you?only?need?one.?In?this
?????*?case,?we'll?use?a?static?singleton.?So?sue?me.?;-)
*/privatestaticSqlMapClient?sqlMapper;
/**?*//**?????*?It's?not?a?good?idea?to?put?code?that?can?fail?in?a?class?initializer,
?????*?but?for?sake?of?argument,?here's?how?you?configure?an?SQL?Map.
*/
static{
try{
????????????Reader?reader=Resources.getResourceAsReader("SqlMapConfig.xml");
????????????sqlMapper=SqlMapClientBuilder.buildSqlMapClient(reader);
????????????reader.close();
????????}catch(IOException?e){
//Fail?fast.thrownewRuntimeException(
"Something?bad?happened?while?building?the?SqlMapClient?instance."+e,?e);
????????}????}
/**?*//**?????*?查詢列表
?????*@return?????*@throwsSQLException
*/
publicstaticListselectAllArticles()throwsSQLException{
returnsqlMapper.queryForList("selectAllArticles");
????}????
/**?*//**?????*?插入數據
?????*@paramarticle
?????*@throwsSQLException
*/
publicstaticvoidinsertArticle(Article?article)throwsSQLException{
????????sqlMapper.insert("insertArticle",?article);
????}????
/**?*//**?????*?更新數據
?????*@paramarticle
?????*@throwsSQLException
*/
publicstaticvoidupdateArticle(Article?article)throwsSQLException{
????????sqlMapper.update("updateArticle",?article);
????}
/**?*//**?????*?刪除數據
?????*@paramid
?????*@throwsSQLException
*/
publicstaticvoiddeleteArticle(intid)throwsSQLException{
????????sqlMapper.delete("deleteArticleById",?id);
????}????
/**?*//**?????*?單查數據
?????*@paramid
?????*@return?????*@throwsSQLException
*/
publicstaticArticle?queryArticleById(intid)throwsSQLException{
????????Article?article=(Article)sqlMapper.queryForObject("selectArticleById",?id);
returnarticle;
????}
}
寫一個Junit測試類來測試一下吧,代碼如下:
packagecn.simple.manager;
importjava.sql.SQLException;
importjava.util.Date;
importjava.util.List;
importorg.junit.Test;
importcn.simple.pojo.Article;
publicclassArticleManagerTest{
????@Test
publicvoidtestSelectAllArticles()throwsSQLException{
????????Listlist=ArticleManager.selectAllArticles();
for(Article?a?:?list){
????????????System.out.println(a.getTitle()+a.getAuthor()+a.getContent()+a.getPubtime());
????????}????}
????@Test
publicvoidtestInsertArticle()throwsSQLException{
for(inti=0;?i<10;?i++){
????????????Article?article=newArticle();
????????????article.setTitle("title-"+i);
????????????article.setAuthor("author-"+i);
????????????article.setContent("content-"+i);
????????????article.setPubtime(newDate());
????????????ArticleManager.insertArticle(article);
????????}????}
????@Test
publicvoidtestUpdateArticle()throwsSQLException{
????????Article?article=newArticle();
????????article.setId(3);
????????article.setTitle("title-title");
????????article.setAuthor("author-author");
????????ArticleManager.updateArticle(article);
????}
????@Test
publicvoidtestDeleteArticle()throwsSQLException{
????????ArticleManager.deleteArticle(5);
????}
}
到此,我們的項目文件列表截圖如下:
新聞管理的Model層開發完畢,可以供我們的Action調用了,好,Struts 2.1.6 精簡實例系列教程,敬請大家期待下文!
本文原創,轉載請注明出處,謝謝!http://www.blogjava.net/rongxh7(心夢帆影JavaEE技術博客)
posted on 2009-07-26 03:02 心夢帆影 閱讀(3483) 評論(9) ?編輯 ?收藏 所屬分類: Struts2.1.6系列教程
總結
以上是生活随笔為你收集整理的java的model层实例_Struts 2.1.6 精简实例系列教程(3):新闻管理Model层的开发(整合iBatis)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java swing 弹出登录框_用Ja
- 下一篇: java rmi 还有用吗_java r