package com.itheima.mapper; import com.itheima.pojo.Brand; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Select; import java.util.List; public interface BrandMapper { /* * 查询所有 * */ @Select("select * from tb_brand") @ResultMap("brandResultMap") List<Brand> selectAll(); /* * 添加 * */ @Insert("insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})") void add(Brand brand); /* * 根据Id查询 * */ @Select("select * from tb_brand where id=#{id}") @ResultMap("brandResultMap") Brand selectById(int id); }
package com.itheima.service; import com.itheima.mapper.BrandMapper; import com.itheima.pojo.Brand; import com.itheima.util.SqlSessionFactoryUtils; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class BrandService { //1. 使用工具类获util取到getSqlSessionFactory SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory(); /* * 查询所有 * */ public List<Brand> selectAll(){ // 调用BrandMapper.selectAll() //2. 获取SqlSession SqlSession sqlSession = factory.openSession(); //3. 获取BrandMapper BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); //4. 调用方法selectAll() List<Brand> brands=mapper.selectAll(); //返回brand集合,释放资源 sqlSession.close(); return brands; } /* * 添加 * */ public void add(Brand brand){ //2. 获取SqlSession SqlSession sqlSession = factory.openSession(); //3. 获取BrandMapper BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); //调用方法 mapper.add(brand); //提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); } /* * 根据Id查询 * */ public Brand selectById(int id){ //2. 获取SqlSession SqlSession sqlSession = factory.openSession(); //3. 获取BrandMapper BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); //4. 调用方法selectById() Brand brand = mapper.selectById(id); //返回brand,释放资源 sqlSession.close(); return brand; } }
package com.itheima.web; import com.itheima.pojo.Brand; import com.itheima.service.BrandService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @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); } }
<%-- Created by IntelliJ IDEA. User: admin Date: 2023/6/30 Time: 13:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ 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>
标签:BrandMapper,回显,修改,brand,案例,sqlSession,import,Brand,itheima From: https://www.cnblogs.com/Karl-hut/p/17516753.html