mybatis做update:动态传入要更新的字段名和字段值(mybatis 3.5.7)

一,示例代码

1,AddressController.java
        Map<String,Object> upMap = new HashMap<String,Object>();
        upMap.put("address",address);
        upMap.put("receiver",receiver);
        upMap.put("isDefault",isDefault);
        upMap.put("userId",userId);
        upMap.put("tel",tel);
        upMap.put("country",country);
        upMap.put("enReceiver",enReceiver);
        upMap.put("email",email);
        upMap.put("zipcode",zipcode);
 
        boolean res = addressService.setOneAddress(upMap,addressId,userId);
2,AddressServiceImpl.java
    @Override
    public boolean setOneAddress(Map<String,Object> upMap, Long addressId,Long userId){
        int upNum = addressMapper.updateOneAddress(upMap,addressId,userId);
        if (upNum > 0) {
            int isDefault = Integer.parseInt(upMap.get("isDefault").toString());
            if (isDefault == 1){
                //设置当前id为缺省地址
                setOneUserAddressIsDefault(addressId,userId);
            }
            return true;
        } else {
            return false;
        }
    }
3,AddressMapper.java
 int updateOneAddress(@Param("upMap")Map<String,Object> upMap,@Param("addressId") Long addressId, @Param("userId")Long userId);
4,AddessMapper.xml
    <update id="updateOneAddress">
        UPDATE address SET
        <foreach collection="upMap" item="val" index="key" separator=",">
            ${key} = #{val}
        </foreach>
        WHERE addressId = #{addressId} and userId = #{userId}
    </update>

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,测试效果

1,界面 mybatis做update:动态传入要更新的字段名和字段值(mybatis 3.5.7) 2,保存时的mybatis日志
2022-02-26 11:05:45.760 [http-nio-10800-exec-9] [AbstractHandlerMapping.java:522] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.yj.storeback.controller.AddressController#addressEdited(Long, String, String, String, String, String, String, String, int)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19feb239] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@873696543 wrapping com.mysql.cj.jdbc.ConnectionImpl@2641b9f0] will not be managed by Spring
==>  Preparing: UPDATE address SET zipcode = ? , country = ? , isDefault = ? , address = ? , receiver = ? , tel = ? , userId = ? , email = ? , enReceiver = ? WHERE addressId = ? and userId = ?
==> Parameters: 053200(String), 中国(String), 1(Integer), 河北省衡水市冀州区恒大城(String), 老刘(String), 13811668888(String), 1(Long), ma@qq.com(String), laoma(String), 3(Long), 1(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19feb239]

三,查看mybatis的版本:

mybatis做update:动态传入要更新的字段名和字段值(mybatis 3.5.7)  
上一篇:Java期末复习速成(四)


下一篇:Android SwitchCompat和checkbox