如何在springmvc中更新mysql数据?

时间:2015-08-01 09:24:57

标签: java mysql spring spring-mvc

在我的Spring项目中,我想根据url更新mysql表字段:

我的网址如下:

localhost:9191/access/name/122(It can be any number,this is the stu_id)

我想从url中获取id,然后更新相应的mysql表的名称字段

在我的数据库中,我有表名" user"。

mysql查询是:

UPDATE student
SET name='rahul'
WHERE stu_id='122(or whatever the number)';

我怎么能这样做在春天mvc ???? 我已经完成了以下

User.java是:

public User(String stu_id, String name) {
        super();
        this.stu_id = stu_id;
        this.name = name;
    }

UserDao.java

public interface UserDao {


     public void updateUser(User user);

}

UserDaoImpl.java是:

@Transactional
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
    private SessionFactory sessionFactory;

public void updateUser(User user) {

        sessionFactory.getCurrentSession().update(user);
        }
    }

UserService.java是:

public interface UserService {

    User updateUser(String stu_id, String name);

}

UserServiceImpl.java是:

@Service("userService")
@Transactional(propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {

public User updateUser(String stu_id, String name) {

        //here how can i update the query??????


return user;
}

UserController.java是:

@Controller
@RequestMapping(value="/access/name/{foo}", method=RequestMethod.GET)
public class UserController {



}

2 个答案:

答案 0 :(得分:0)

使用方法中的注释:
@RequestMapping(value =" / access / name / {foo}",method = RequestMethod.GET) public void foo(@PathVariable(" id")int id,HttpServletRequest request){

}

然后通常如何更新数据库只需执行常规操作。

答案 1 :(得分:0)

调用updateRecord()的dao方法 即如果您使用HibernateTemplate然后直接使用调用update()方法引用Hibernate模板并传递您的pojo obj。 它简单有效。 离。

hibernateTemplate.update(pojo);

并将其作为

返回
return hibernateTemplate.update(pojo);