myibatis dao层update语句使用的generic po类

表数据更新语句类似:update 表名 set field1= value1,field2=value2 where 条件字段1=value3 and 条件字段2= value4;

 

在dao层,value1~value4一般是从一个vo值对象的属性中取值(不纠结于vo、po、bo、dto这些概念和区别,这里把dao层中的pojo也称为vo),这种方式存在的一个问题是:在这个vo对象中,不容易分辨出哪些属性用于set新值,哪些属性用于where条件中。

 

UpdateVoTemplate类

为了解决该问题,写了个voUpdateVoTemplate,该类具有两个属性vo对象:newValue属性对象用于update语句setwhereValue属性用于where条件。这个类是generic类,实际使用时要传两个具体的vo类进行实例化。

/**

 *用于update sql语句的模板vo

 * @author wanggb

 *

 * @param <UpdateValueVo>  update setvo

 * @param <WhereVo>      update where条件vo

 */

public class UpdateVoTemplate<UpdateValueVo,WhereVo>

{

   private UpdateValueVo newValue;

       

   private WhereVo whereValue;

        

   public UpdateVoTemplate(UpdateValueVo newValue,WhereVo where) {

       this.whereValue = where;

       this.newValue = newValue;

   }

 

   // get方法省略

 

}

 

使用demo

如果有个这样的更新ip数据的语句update ip_pool set status =2,session_id='s001' where status =1 and ip in('58.215.180.214');

1)提取sql语句status=2,session_id='s001'中的两个属性产生一个IpStatusSetVo对象

public class IpStatusSetVo

{

    private int status;

    private String session_id;

    //getter and setter方法省略

    public IpStatusSetVo(int status, String session_id)

    {

      ...

    }

}

 

2)提取where status =1 and ip in('58.215.180.214')中的两个属性产生一个IpStatusWhereCondVo对象

public class IpStatusWhereCondVo

{

   private int status;

   private String[ ] ips;

   //getter and setter方法省略

   public IpStatusWhereCondVo (int status, String[ ] ips)

   {

      ...

   }

}

 

3IpPoolDao方法: int  updateIpStatus(UpdateVoTemplate<IpStatusSetVo, IpStatusWhereCondVo> ipStatusUpdateVo);   //使用上面的两个类IpStatusSetVo, IpStatusWhereCondVo实例化模板类UpdateVoTemplate

 

4)对应的myibatis sql语句

<update id="updateIpStatus" parameterType="updateVoTemplate" flushCache="true">

    update ip_pool set status= #{newValue.status},session_id=#{newValue.session_id}

        where status = #{whereValue.status} and ip in

         <foreach collection="whereValue.ips"  index="index" item="item" 

             open="(" separator="," close=")">

             #{item}

         </foreach>

</update>

 

5)service层调用:

IpStatusSetVo newValueVo = new IpStatusSetVo(2,"s001");

IpStatusWhereCondVo whereValuevo = new IpStatusWhereCondVo(1,new String[]{"58.215.180.214"});

UpdateVoTemplate< IpStatusSetVo, IpStatusWhereCondVo> ipStatusUpdateVo = new UpdateVoTemplate<IpStatusSetVo, IpStatusWhereCondVo>(newValueVo, whereValuevo);

ipPoolDao.updateIpStatus(ipStatusUpdateVo);

 

6)如果UpdateValueVoWhereVo只有一个属性,例如:

update ip_pool set status=2,session_id='s001' where ip='58.215.180.214';

该语句中的where条件只有一个ip字段,则可简化为UpdateVoTemplate<IpStatusSetVo,String> 

调用如下:

IpStatusSetVo newValueVo = new IpStatusSetVo(2,"s001");

UpdateVoTemplate< IpStatusSetVo,String> ipStatusUpdateVo = new UpdateVoTemplate< IpStatusSetVo,String>(newValueVo, "58.215.180.214");

ipPoolDao.updateIpStatus(ipStatusUpdateVo);

 

sql如下:

<update id="updateIpStatus" parameterType="updateVoTemplate" flushCache="true">

   update ip_pool set status= #{newValue.status},session_id=#{newValue.session_id}

       where ip = #{whereValue}

</update>

 

猜你喜欢

转载自wanshi.iteye.com/blog/2221940