spring+mybatis error
Mainly I wrote like this
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.psc.mrkong.func.login.mapper.LoginMapper">
<insert id="register">
insert into tb_userinfo (`name`,phone,`password`) values ( #{userName},#{phone},#{password})
</insert>
</mapper>
package com.psc.mrkong.func.login.mapper;
import org.apache.ibatis.annotations.Param;
import com.psc.mrkong.func.login.po.User;
public interface LoginMapper {
int register(String userName,String phone,String password);
}
The error reported when running the title: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'userName' not found. Available parameters are [0, 1, 2, param3, param1, param2]
Later, the online query said that it was a parameter problem, so it was changed to
package com.psc.mrkong.func.login.mapper;
import org.apache.ibatis.annotations.Param;
import com.psc.mrkong.func.login.po.User;
public interface LoginMapper {
int register(@Param("userName") String userName,@Param("phone") String phone,@Param("password") String password);
}
Operating normally
It turns out that when there are multiple parameters, if there is no @Param annotation, mybatis will not know which parameter you want to call
In fact, there are two ways to solve
It is to change the sql reference parameter of xml to the following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.psc.mrkong.func.login.mapper.LoginMapper">
<insert id="register">
insert into tb_userinfo (`name`,phone,`password`) values ( #{0},#{1},#{2})
</insert>
</mapper>