关于mybatis中update操作遇到了问题解决; 看代码:
测试类:
@Test
public void updatwUser () throws IOException {
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//建立会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updatwUser("帅帅","帅帅",1);
sqlSession.commit();
sqlSession.close();
}
Mapper接口:
void updatwUser ( String j, String j1,int i);
Mapper.xml配置:
<update id="updatwUser">
update user
<set>
# 后面加,重点
<if test="param1!=null">name=#{param1},</if>
<if test="param2!=null">password=#{param2},</if>
</set>
where id=#{param3}
</update>
重点:一定要在name=#{param1},后面加上那个,password=#{param2},建议最后一个参数后也加,不加也行,mybatis会自动去掉最后一个, ->>>>>>一定加 ","
不加的话会报错:
org.apache.ibatis.exceptions.PersistenceException:
Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'password=‘帅帅’
where id=1' at line 4
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: update user SET name=? password=? where id=?
动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看MyBatis视频教程学习哦!
提枪策马乘胜追击04-21 20:01
代码小兵92504-17 16:07
代码小兵98804-25 13:57
杨晶珍05-11 14:54