问题描述(更新测试失败)
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 ‘id=4’ at line 1
The error may exist in cn/js/ccit/mapper/CustomerMapper.java (best guess)
The error may involve cn.js.ccit.mapper.CustomerMapper.updateCustomer-Inline
The error occurred while setting parameters
SQL: update t_customer set username=?, jobs=?,phone=?,login_Account=?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 ‘id=4’ at line 1
@Update("update t_customer set username=#{username}, jobs=#{jobs},phone=#{phone},login_Account=#{loginAccount}"
+"where id=#{id}")
int updateCustomer(Customer customer);//更新一个用户
@Test
public void updateTest() throws IOException {
// 2.获取和数据库的一次会话
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
// 3.使用sqlSession操作数据库,获取到dao接口的实现
CustomerMapper mapper = sqlSession.getMapper(CustomerMapper.class);
// 4.调用之前定义的方法
int updateCustomer = mapper.updateCustomer(new Customer(4, null, null, "88888888888",null));
System.out.println(updateCustomer);
sqlSession.commit();
} finally {
sqlSession.close();
}
}
原因分析:
缺少空格
解决方案:
@Update("update t_customer set username=#{username}, jobs=#{jobs},phone=#{phone},login_Account=#{loginAccount} "
+"where id=#{id}")
int updateCustomer(Customer customer);//更新一个用户