Struts 2.1.6 精简实例系列教程(3):新闻管理Model层的开发(整合iBatis)
2009-09-23 00:00:00 来源:WEB开发网一看这代码,也有点复杂,我的说法同上,大不了COPY,再略作修改,呵呵
好了,来写我们的业务逻辑层:
package cn.simple.manager;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import cn.simple.pojo.Article;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class ArticleManager {
/** *//**
* SqlMapClient instances are thread safe, so you only need one. In this
* case, we'll use a static singleton. So sue me. ;-)
*/
private static SqlMapClient 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.
throw new RuntimeException(
"Something bad happened while building the SqlMapClient instance."
+ e, e);
}
}
/** *//**
* 查询列表
* @return
* @throws SQLException
*/
public static List<Article> selectAllArticles() throws SQLException {
return sqlMapper.queryForList("selectAllArticles");
}
/** *//**
* 插入数据
* @param article
* @throws SQLException
*/
public static void insertArticle(Article article) throws SQLException {
sqlMapper.insert("insertArticle", article);
}
/** *//**
* 更新数据
* @param article
* @throws SQLException
*/
public static void updateArticle(Article article) throws SQLException {
sqlMapper.update("updateArticle", article);
}
/** *//**
* 删除数据
* @param id
* @throws SQLException
*/
public static void deleteArticle(int id) throws SQLException {
sqlMapper.delete("deleteArticleById", id);
}
/** *//**
* 单查数据
* @param id
* @return
* @throws SQLException
*/
public static Article queryArticleById(int id) throws SQLException {
Article article = (Article)sqlMapper.queryForObject("selectArticleById", id);
return article;
}
}
更多精彩
赞助商链接