刘大猫. 2023-04-26 21:44 采纳率: 57.1%
浏览 14
已结题

批量修改中为什么传参Map对象时不能加@Param,不加就可以批量更新成功,加了就报错

我在使用Mybatis练习XML的批量更新时遇到了问题,
问题:批量修改中为什么传参Map对象时不能加@Param,不加就可以批量更新成功,加了就报错,代码如下

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Author 211145187
 * @Date 2023/4/17 22:33
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
    //id
    private Integer id;
    //用户名称
    private String username;
    //用户密码
    private String password;
    //用户手机号码
    private String mobile;
    //性别
    private Integer gender;

    public User(String username, String password, String mobile, Integer gender) {
        this.username = username;
        this.password = password;
        this.mobile = mobile;
        this.gender = gender;
    }

    public User(String username, Integer gender) {
        this.username = username;
        this.gender = gender;
    }

    public User(Integer id, String username, Integer gender) {
        this.id = id;
        this.username = username;
        this.gender = gender;
    }
//批量更新:(传参为Map且map种包含list集合)
@Test
public void batchUpdateByMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<User> list = new ArrayList<>();
        list.add(new User(18, "同学9", 1));
        list.add(new User(19, "同学10", 1));
        Map<String, Object> map = new HashMap<>();
        map.put("paramMap", list);
        System.out.println(mapper.batchUpdateByMap(map));
}
int batchUpdateByMap(Map<String, Object> map);
<update id="batchUpdateByMap">
        <foreach collection='paramMap' item='item' index='index' separator='; '>
            update litemall_user
            <set>
                <if test="item.username != null and item.username !=''">
                    username = #{item.username},
                </if>
                <if test="item.gender != null  and item.gender != 0">
                    gender = #{item.gender}
                </if>
            </set>
            where id = #{item.id}
        </foreach>
</update>

加了@Param之后长这样,int batchUpdateByMap(@Param("map") Map<String, Object> map);报错如下

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id = null
         ;  
            update litemall_user
              
   ' at line 3
### The error may exist in com/mybatis/mapper/SelectMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: update litemall_user                            where id = ?          ;               update litemall_user                            where id = ?
### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id = null
         ;  
            update litemall_user
              
   ' at line 3

    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:196)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:67)
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
    at com.sun.proxy.$Proxy9.batchUpdateByMap(Unknown Source)
    at com.mybatis.SelectMapperTest.batchUpdateByMap(SelectMapperTest.java:161)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id = null
         ;  
            update litemall_user
              
   ' at line 3
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:354)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194)
    ... 30 more
  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月26日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。