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://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/06/01/mybatis-zuo-update-dong-tai-chuan-ru-yao-geng-xin-de-zi/

         對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

說明:作者:劉宏締 郵箱: 371125307@qq.com

二,測試效果

1,界面
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的版本:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM