接口测试从零开始5_教你如何使用ibatis

来源:互联网 发布:开源商城cms 编辑:程序博客网 时间:2024/04/30 15:36
 

1.      创建pojo

public classPingJia

2.      创建Dao接口

publicinterfaceIPingJiaDao

 

3. 使用ibatis编写实现类

1. 通过pom依赖引入ibatis包

   <dependency>

       <groupId>com.ibatis</groupId>

       <artifactId>ibatis</artifactId>

       <version>2.3.4.726</version>

   </dependency>

 

2. 编写实现类,引入ibatisSqlMapClient和Resource

importcom.ibatis.common.resources.Resources;

importcom.ibatis.sqlmap.client.SqlMapClient;

importcom.ibatis.sqlmap.client.SqlMapClientBuilder;   

3. 配置POJO类的IBatis配置文件pingjia.ibatis.xm

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEsqlMapPUBLIC"-//ibatis.apache.org//DTD SQL Map 2.0//EN"

       "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMapnamespace="PingJiaDao">

  <!--命名空间,在此空间外要引用此空间的元素,则需要加上命名空间名-->

   <typeAliasalias="pingJia"type="com.koubei.testlearnning.pojo.PingJia"/> 

   <!--实体别名, 如果有用到全名的地方,可以用别名代替,受命名空间约束-->

   <resultMapclass="com.koubei.testlearnning.pojo.PingJia"id="PingJia">

      <resultcolumn="pingjiaid"property="pingJiaId"/>

      <resultcolumn="pingjiacontent"property="pingJiaContent"/>

      <resultcolumn="storeid"property="storeId"/>

      <resultcolumn="pingjiatype"property="pingJiaType"/>

      <resultcolumn="pingjiadate"property="pingJiaDate"/>

   </resultMap>

   <!--结果集映射,-->

   <deleteid="delete"parameterClass="java.lang.String">

   <!--删除操作-->

       delete frompingjiawherepingjiaid=#pingJiaId#

   </delete>

   <insertid="insert"parameterClass="pingJia">

   <!--插入操作, 对于自增主键的表,插入可以不配置插入的主键列,否则是必须的-->

       insert

       intopingjia(pingjiaid,pingjiacontent,storeid,

      pingjiatype,pingjiadate)

       values(#pingJiaId#, #pingJiaContent#, #storeId#, #pingJiaType#,#pingJiaDate#)

   </insert>

   <updateid="update">

   <!--更新操作-->

       update task_info

      <dynamicprepend="set">

   <!--动态sql:prepend表示链接关键字,property表示属性值-->

      <isNotNullprepend=" , "property="pingJiaContent">

             pingjiacontent=

              #excutePerson#

       </isNotNull>

          <isNotNullprepend=" , "property="storeid">

             storeid=#storeId#

          </isNotNull>

          <isNotNullprepend=" , "property="pingJiaType">

             pingjiatype

              =#pingJiaType#

       </isNotNull>

          <isNotNullprepend=" , "property="pingjiadate">

             pingjiadate

              =#pingJiaDate#

               </isNotNull>

      </dynamic>

       wherepingjiaid=#pingJiaId#

   </update>

   <selectid="getPingJiaList"parameterClass="java.lang.String"resultMap="PingJia">

   <!--查询操作,插入语句入参通过parameterClass="类别名"来设定,可以设定类别名,也可以设定为map,

   也可以设定为iBatis支持的原生类型(比如string、int、long等,  map是最强大的入参方式,任何入参方式

   都可以转换为这种入参方式,因为iBatis仅接受一个入参,当几个参数分布在不同对象中的时候,将这些对象

   的属性(或者对象本身put)到map中,然后一次传递给sql语句-->

      sselect* frompingjiawhere storeId = #storeId#

   </select>

      <selectid="getGoodPingNum"parameterClass="java.lang.String">

      sselectcount(1) frompingjiawhere storeId = #storeId# andpingjiatype=1

   </select>

      <selectid="getPingJiaNum"parameterClass="java.lang.String">

      sselectcount(1) frompingjiawhere storeId = #storeId#

   </select>

</sqlMap>

 

察看教你如何使用ibatis2

 

原文:http://www.51testing.com/?uid-128005-action-viewspace-itemid-804272


原创粉丝点击