Model层:
public class systemrun {
private Integer run;
private String name;
private String detail;
public Integer getRun() {
return run;
}
public void setRun(Integer run) {
this.run = run;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
Dao层:
import com.jc.model.systemrun;
import org.hibernate.Session;
import sessionFactory.HibernateSessionFactory;
public class SysDao {
/**
* 更新System表数据
*/
public void updateSys(systemrun system){
Session session = null; //Session对象
try {
//获取Session
session = HibernateSessionFactory.getSession();
session.beginTransaction(); //开启事物
session.update(system); //持久化user
session.getTransaction().commit(); //提交事物
} catch (Exception e) {
e.printStackTrace(); //打印异常信息
session.getTransaction().rollback();//回滚事物
}finally{
HibernateSessionFactory.closeSession(); //关闭Session
}
}
}
Servlet层:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String sys=request.getParameter("sys");
if("开始".equals(sys)){
/**
* 新建一个systemrun system,然后对该对象的run name detail赋值,最后调用dao.updateSys(systemrun sys)方法更新到数据库
*/
systemrun system=new systemrun();
system.setRun(0);
system.setName("start");
system.setDetail("System is running!");
SysDao sDao=new SysDao();
sDao.updateSys(system);
}
}
程序就是更改system的三个值:主键run及name和detail两个参数,但是主键的值不能这么改。
报这样的错:Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1求指导!