回显就是通过id进行查询:
![](https://i-blog.csdnimg.cn/blog_migrate/25f940c1629ff19479d3801d75447a10.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a1fb2e790e0e73a6767b908d4d40b695.png)
首先我们要做一个根据id查询,将数据回显到update页面里边
才能进行修改
BrandMapper:
记得给selectById上添加@ResultMap注释,这样才能保证页面上数据的完整
public interface BrandMapper {
/**
* 查询所有
* @return
*/
@Select("select * from tb_brand")
@ResultMap("brandResultMap")
List<Brand> selectAll();
/**
* 添加功能
* @param brand
*/
@Insert("insert into tb_brand values(null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
void add(Brand brand);
/**
* 根据id查询 记得添加ResultMao 否则页面上将不展示内容
* @param id
* @return
*/
@Select("select * from tb_brand where id= #{id}")
@ResultMap("brandResultMap")
Brand selectById(int id);
}
BrandService:
public class BrandService {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
/**
* 查询所有
* @return
*/
public List<Brand> selectAll(){
//获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取brandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
List<Brand> brands = mapper.selectAll();
//释放资源
sqlSession.close();
return brands;
}
/**
* 添加
* @param brand
*/
public void add(Brand brand){
//获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取brandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//调用方法
mapper.add(brand);
//提交事务
sqlSession.commit(true);
sqlSession.close();
}
/**
* 根据id查询
* @param id
* @return
*/
public Brand selectById(int id){
//获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取brandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
Brand brand = mapper.selectById(id);
//释放资源
sqlSession.close();
return brand;
}
}
SelectByIdServlet:
@WebServlet("/selectByIdServlet")
public class SelectByIdServlet extends HttpServlet {
private BrandService service= new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收id
String id = request.getParameter("id");
//2.调用service查询
Brand brand = service.selectById(Integer.parseInt(id));
//3.存储到request中
request.setAttribute("brand",brand);
//4.转发到update.jsp
request.getRequestDispatcher("/update.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
这里要注意:描述信息的${brand.description}要写的位置
还有<input>标签里 checked属性的使用,就是默认当前属性,所以我们在做状态的回显时,需要用jstl的id来进行逻辑判断
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改品牌</title>
</head>
<body>
<h3>修改品牌</h3>
<form action="/brand-demo/addServlet" method="post">
品牌名称:<input name="brandName" value="${brand.brandName}"><br>
企业名称:<input name="companyName" value="${brand.companyName}"><br>
排序:<input name="ordered" value="${brand.ordered}"><br>
描述信息:<textarea rows="5" cols="20" name="description"> ${brand.description}</textarea><br>
<c:if test="${brand.status==0}">
状态:
<input type="radio" name="status" value="0" checked>禁用
<input type="radio" name="status" value="1">启用<br>
</c:if>
<c:if test="${brand.status==1}">
状态:
<input type="radio" name="status" value="0" >禁用
<input type="radio" name="status" value="1" checked>启用<br>
</c:if>
<input type="submit" value="提交">
</form>
</body>
</html>
还有在brand.jsp页面跳转SelectByIdServlet页面时,需要注意要给Servlet进行id传参,时这样传递的:
![](https://i-blog.csdnimg.cn/blog_migrate/210a853ed442d6d5962df7daebc18126.png)