前端页面
需要将表单隐藏的id一起返回给后端
<input type="hidden" name="id" value="${brand.id}">
表现层
@WebServlet("/updateBrandServlet")
public class updateBrandServlet extends HttpServlet {
private BrandService brandService = new BrandService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// 获取数据
Map<String, String[]> parameterMap = request.getParameterMap();
Brand brand = new Brand();
// 将数据一次行封装到实体类中
try {
BeanUtils.populate(brand,parameterMap);
} catch (Exception e) {
e.printStackTrace();
}
// 调用业务逻辑层操作数据库修改数据
brandService.updateBrand(brand);
// 重定向到查询数据库表现层,更新前端页面数据
response.sendRedirect(request.getContextPath()+"/selectAllServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
业务逻辑层
public class BrandService {
// 业务逻辑层操作数据库修改数据
public void updateBrand(Brand brand) {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.updateBrand(brand);
}
}
数据访问层
public interface BrandMapper {
// 数据修改
void updateBrand(Brand brand);
}
数据库操作
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cyf.mapper.BrandMapper">
<resultMap id="brand" type="com.cyf.pojo.Brand">
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<update id="updateBrand">
update tb_brand set brand_name = #{brandName},company_name = #{companyName},ordered = #{ordered},description = #{description},status = #{status} where id = #{id}
</update>
</mapper>