Mybatis在我的工作中使用

来源:互联网 发布:淘宝千里眼是免费得吗 编辑:程序博客网 时间:2024/03/29 23:17

第一次在工作项目中接触到Mybatis+maven,做一个小记录,让自己以后可以随时回顾,也算是一个方法总结

以管理平台中支付订单的查询为例,首先是页面这样一个表单的提交

<form id="zqOrgPayBillListForm" action="page/zq/order/payBillList.shtml" method="post">
<span style="white-space:pre"></span>其中每个查询条件字段的输入都是一实体的方式  queryParam.mechSeqId:
<span style="white-space:pre"><td class="queryName" nowrap>商户订单号</td><span style="white-space:pre"></span><td class="queryContent"><span style="white-space:pre"></span><input id="mechSeqId" name="queryParam.mechSeqId" value="<s:property value="queryParam.mechSeqId" />" /><span style="white-space:pre"></span></td></span>
<span style="white-space:pre"></span>通过struts.xml中的<pre name="code" class="html"><span style="white-space:pre"></span><!-- 支付订单管理 --><action name="payBillList" method="payBillList" class="zqOrgPayBillAction"><result name="success">/page/zq/order/zqOrgPayBillList.jsp</result></action>

的配置来调用 类名为zqOrgPayBillAction的action,当然Action的配置在applicationAction.xml中:

<!--支付转清订单管理 --><bean id="zqOrgPayBillAction" class="com.ylink.ydzf.mp.zq.action.ZqOrgPayBillAction" scope="prototype"><property name="zqMechPayBillAppService" ref="zqMechPayBillAppService" /><property name="zqOrgInfoAppService" ref="zqOrgInfoAppService" /></bean>

其中property为Action中使用到的 Service ,也可以在Action中使用注解的方式注入,这里就不需要配置了。


接下来在看ZqOrgPayBillAction之前先主义一下一个ZqMechPayBillDTO类,与数据库中对应的是ZqMechPayBill类,这里的***DTO表示数据传输使用的类

private List<ZqMechPayBillDTO> zqMechPayBillList; //支付订单列表 List<***DTO>来存储查询的订单结果

接下来:

<span style="white-space:pre"></span>adaptQueryParams();//适配查询条件pageData = zqMechPayBillAppService.findPage(pageData, queryParam);<span style="white-space:pre"></span>//查询页面结果zqMechPayBillList = pageData.getRows();<span style="white-space:pre"></span>//结果数据page = new Page(pageData.getPageNumber(), pageData.getPageSize(), pageData.getTotal());// 统计金额totalZqMechPayBill = zqMechPayBillAppService.calPageAmount(queryParam);if (totalZqMechPayBill == null) {totalZqMechPayBill = new ZqMechPayBillDTO();


这里的findPage既调用放大,将参数传至 zqMechPayBillAppService:

public PageData<ZqMechPayBillDTO> findPage(PageData<ZqMechPayBillDTO> pageData, ZqMechPayBillDTO queryParam)throws BusiOrderAppCheckedException {try {return zqMechPayBillService.queryPage(pageData, queryParam);} catch (Exception e) {logger.error("", e);throw new BusiOrderAppCheckedException(EErrorCodeMessage.BUSI_SYSTEM_ERROR.getValue(),EErrorCodeMessage.BUSI_SYSTEM_ERROR.getDisplayName());}}

zqMechPayBillAppService又讲参数传值zqMechPayBillService(**APPService为远程调用):

public PageData<ZqMechPayBillDTO> queryPage(PageData<ZqMechPayBillDTO> pageData, ZqMechPayBillDTO queryParam) {List<ZqMechPayBillDTO> items = zqMechPayBillMapper.queryPage(queryParam, pageData.getBeginIndex(), pageData.getEndIndex());long count = zqMechPayBillMapper.queryPageCount(queryParam);pageData.setRows(items);pageData.setTotal(count);return pageData;}
zqMechPayBillService中,参数传进了zqMechPayBillMapper.queryPage中,这里的zqMechPayBillMapper就是对数据库操作的一个接口:

zqMechPayBillMapper.java(接口)

zqMechPayBillMapper.xml(存放对数据库CURD的sql语句)

zqMechPayBillMapper.java中有:

 

 List<ZqMechPayBillDTO> queryPage(@Param("queryParam")ZqMechPayBillDTO queryParam,    @Param("beginIndex")int beginIndex , @Param("endIndex")int endIndex);
即把queryParam 存放进queryParam传入zqMechPayBillMapper.xml,把beginIndex存放进beginIndex,endIndex存放进endIndex传入zqMechPayBillMapper.xml中

在zqMechPayBillMapper.xml中:

有<select id="queryPage" resultMap="EBaseResultMap"> </select>这样包含的语句表示对数据库的操作。

数据取得以后再如前面所说的以zqMechPayBillList传入jsp页面例如:

<span style="white-space:pre"></span><s:if test="zqMechPayBillList != null && zqMechPayBillList.size() > 0"><s:iterator id="bill" value="ZqMechPayBillList" status="st">
<span style="white-space:pre"></span><tr>
<span style="white-space:pre"><td align="center" nowrap><span style="white-space:pre"></span><s:property value="#bill.curryType" /><span style="white-space:pre"></span></td> </span>
<span style="white-space:pre"></span></tr>
<span style="white-space:pre"></span></s:iterator>
<span style="white-space:pre"></span></s:if>





0 0
原创粉丝点击