映射文件:

    <query name="editPassword">
        update User set password=? where id=?
    </query>

注:update后面是类名,同时sql语句中用的是单引号,不是双引号

service层:

1     // 修改密码
2     public Integer editPassword(String id, String password) {
3         return userDao.executeUpdate("editPassword",password,id);
4     }
5         

注:id为query中的query标签的name属性值

dao层:

 1 //更新操作
 2     public Integer executeUpdate(String queryname, Object... objects) {
 3         SessionFactory sessionFactory = this.getSessionFactory();
 4         Session session = sessionFactory.getCurrentSession();
 5         Query query = session.getNamedQuery(queryname);//从映射文件中读取名为queryname的querys
 6         int i = 0;
 7         for (Object object : objects) {
 8             query.setParameter(i++, object);
 9         }
10         int row = query.executeUpdate();
11         return row;        ////更新操作影响的行数
12     }                                                                                    

相关文章:

  • 2021-07-27
  • 2022-02-05
  • 2022-12-23
  • 2021-08-21
  • 2021-10-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2021-11-30
  • 2021-09-24
相关资源
相似解决方案