Ibatis 版Jpetstore5.0分析

本文出处:http://morner.spaces.live.com/blog/cns!81A0B358EC451EFB!221.entry
 
JpetStore 5.0用到了ibatis和beanaction。
beanaction是将struts中的form和action综合起来的----我是这样理解的(不懂什么反向原理)
再结合ibatis一个强大的sql map工具,它是将SQL语句映射成Java对象,而对于ORM工具,它的SQL语句是根据映射定义生成的:
在struts-config.xml中
<form-beans>
    <form-bean name="accountBean" type="com.ibatis.jpetstore.presentation.AccountBean"/>
    <form-bean name=" catalogBean" type="com.ibatis.jpetstore.presentation.CatalogBean"/>
    <form-bean name="cartBean" type="com.ibatis.jpetstore.presentation.CartBean"/>
    <form-bean name="orderBean" type="com.ibatis.jpetstore.presentation.OrderBean"/>
</form-beans>
 
下面是对所有beanaction的说明
    <action path="/shop/ searchProducts" type="org.apache.struts.beanaction.BeanAction"
            name=" catalogBean" scope="session"
            validate="false">
      <forward name="success" path="/catalog/SearchProducts.jsp"/>
    </action>
/shop/ searchProducts.do(如果在web.xml中设置的是.do),JpetStore 5.0有的是.shtml
 控制跳转到 catlogBean中的 searchProdusts方法
catlogBean中有属性
private CatalogService catalogService;
并有方法:
  public String s earchProducts() {
    if (keyword == null || keyword.length() < 1) {
      setMessage("Please enter a keyword to search for, then press the search button.");
      return FAILURE;
    } else {
      productList = catalogService.searchProductList(keyword.toLowerCase());
      return SUCCESS;
    }
  }
catalogService  中有
 DaoManager daoManager = DaoConfig.getDaoManager();
用DaoConfig.getDaoManger对daoManger进行初试化

public class DaoConfig {

  private static final String resource = "com/ibatis/jpetstore/persistence/dao.xml";//读取对应配置文件
  private static final DaoManager daoManager;

  static {
    try {
      daoManager = newDaoManager(null);
      Properties props = Resources.getResourceAsProperties("properties/database.properties");//数据库配置文件
      String url = props.getProperty("url");
      String driver = props.getProperty("driver");
      String username = props.getProperty("username");
      String password = props.getProperty("password");
      if (url.equals("jdbc:hsqldb:mem:jpetstore")) {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url, username, password);
        try {
          ScriptRunner runner = new ScriptRunner(conn, false, false);
          runner.setErrorLogWriter(null);
          runner.setLogWriter(null);
          runner.runScript(Resources.getResourceAsReader("ddl/hsql/jpetstore-hsqldb-schema.sql"));//建表
          runner.runScript(Resources.getResourceAsReader("ddl/hsql/jpetstore-hsqldb-dataload.sql"));
        } finally {
          conn.close();
        }
      }
    } catch (Exception e) {
      throw new RuntimeException("Description.  Cause: " + e, e);
    }

  }

  public static DaoManager getDaoManager() {
    return daoManager;
  }

  public static DaoManager newDaoManager(Properties props) {
    try {
      Reader reader = Resources.getResourceAsReader(resource);
      return DaoManagerBuilder.buildDaoManager(reader, props);
    } catch (Exception e) {
      throw new RuntimeException("Could not initialize DaoConfig.  Cause: " + e, e);
    }
  }

}

com/ibatis/jpetstore/persistence/dao.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE daoConfig
    PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
    "http://ibatis.apache.org/dtd/dao-2.dtd">

<daoConfig>

  <context>

    <transactionManager type="SQLMAP">
      <property name="SqlMapConfigResource"
        value="com/ibatis/jpetstore/persistence/sqlmapdao/sql/sql-map-config.xml"/>
    </transactionManager>

    <dao interface="com.ibatis.jpetstore.persistence.iface.ItemDao"
      implementation="com.ibatis.jpetstore.persistence.sqlmapdao.ItemSqlMapDao"/>

    <dao interface="com.ibatis.jpetstore.persistence.iface.SequenceDao"
      implementation="com.ibatis.jpetstore.persistence.sqlmapdao.SequenceSqlMapDao"/>

    <dao interface="com.ibatis.jpetstore.persistence.iface.AccountDao"
      implementation="com.ibatis.jpetstore.persistence.sqlmapdao.AccountSqlMapDao"/>

    <dao interface="com.ibatis.jpetstore.persistence.iface.CategoryDao"
      implementation="com.ibatis.jpetstore.persistence.sqlmapdao.CategorySqlMapDao"/>

    <dao interface="com.ibatis.jpetstore.persistence.iface.ProductDao"
      implementation="com.ibatis.jpetstore.persistence.sqlmapdao.ProductSqlMapDao"/>

    <dao interface="com.ibatis.jpetstore.persistence.iface.OrderDao"
      implementation="com.ibatis.jpetstore.persistence.sqlmapdao.OrderSqlMapDao"/>

  </context>

</daoConfig>

 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值