ToB企服应用市场:ToB评测及商务社交产业平台

标题: JavaWeb综合案例 [打印本页]

作者: 科技颠覆者    时间: 2023-1-11 12:09
标题: JavaWeb综合案例
JavaWeb综合案例

笔记目录:(https://www.cnblogs.com/wenjie2000/p/16378441.html)
视频教程(P154~P163)
环境搭建

1.查询所有


2.新增品牌


Servlet代码优化

Web 层的Servlet个数太多了,不利于管理和编写
将Servlet进行归类,对于同一个实体的操作方法,写到一个Servlet中。比如: BrandServlet、
UserServlet(需要用到反射的知识)

3.修改品牌

后台代码和新增品牌类似
4.删除品牌

后台代码和新增品牌类似
5.批量删除


6.分页查询
  1. select * from tb_brand limit 0,5;-- 从第1条开始查,查5条
复制代码
页面传递的参数:当前页码、每页显示条数
参数1:开始索引=(当前页码-1)*每页显示条数
参数2:查询的条目数=每页显示条数

有两种数据,ajax只能一次性接收数据,两种数据需要后端同时发给前端,必须将两种数据封装到一起。又需要将数据转成json,所以只能创建新的Bean类来建立对象。

7.条件查询


前端代码优化

之前的代码需要用到var _this=this;
  1. var _this=this;
  2. axios({
  3.     method: "post",
  4.     url: "http://localhost:8080/brand-case/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
  5.     data:this.brand,
  6. }).then(function(response){
  7.     //设置表格数据
  8.     _this.tableData = response.data.rows;//{"rows":[],"totalCount":52}
  9.     //设置总记录数
  10.     _this.totalCount = response.data.totalCount;
  11. })
复制代码
可以改成以下方式,就不需要定义_this来获取data()中的数据了
  1. axios({
  2.     method: "post",
  3.     url: "http://localhost:8080/brand-case/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
  4.     data:this.brand,
  5. }).then(response =>{
  6.     //设置表格数据
  7.     this.tableData = response.data.rows;//{"rows":[],"totalCount":52}
  8.     //设置总记录数
  9.     this.totalCount = response.data.totalCount;
  10. })
复制代码
完整代码

文件结构

代码
brand-case
<blockquote>src/main
java
com.itwen
mapper
BrandMapper.java
  1. package com.itwen.mapper;
  2. import com.itwen.pojo.Brand;
  3. import org.apache.ibatis.annotations.*;
  4. import java.util.List;
  5. public interface BrandMapper {
  6. /**
  7.      * 查询所有
  8.      * @return
  9.      */
  10.     @Select("select * from tb_brand")
  11.     @ResultMap("brandResultMap")
  12.     List<Brand> selectAll();
  13.     /**
  14.      * 添加数据
  15.      * @param brand
  16.      */
  17.     @Insert("insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
  18.     void add(Brand brand);
  19.     /**
  20.      * 修改数据
  21.      * @param brand
  22.      */
  23.     @Update("UPDATE tb_brand SET brand_name= #{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description},status=#{status} WHERE id=#{id}")
  24.     void update(Brand brand);
  25.     /**
  26.      * 删除数据
  27.      * @param id
  28.      */
  29.     @Delete("delete from tb_brand where id=#{id}")
  30.     void deleteById(int id);
  31.     /**
  32.      * 批量删除
  33.      * @param ids
  34.      */
  35.     void deleteByIds(@Param("ids") int[] ids);
  36.     /**
  37.      * 分页查询
  38.      * @param begin
  39.      * @param size
  40.      * @return
  41.      */
  42.     @Select("select * from tb_brand limit #{begin},#{size}")
  43.     @ResultMap("brandResultMap")
  44.     List<Brand> selectByPage(@Param("begin") int begin,@Param("size") int size);
  45.     /**
  46.      * 查询总记录数
  47.      * @return
  48.      */
  49.     @Select("select count(*) from tb_brand")
  50.     int selectTotalCount();
  51.     /**
  52.      * 分页条件查询
  53.      * @param begin
  54.      * @param size
  55.      * @param brand
  56.      * @return
  57.      */
  58.     List<Brand> selectByPageAndCondition(@Param("begin") int begin,@Param("size") int size,@Param("brand") Brand brand);
  59.     /**
  60.      * 查询总记录数
  61.      * @return
  62.      */
  63.     int selectTotalCountByCondition(Brand brand);
  64. }
复制代码
pojo
Brand.java
  1. package com.itwen.pojo;
  2. public class Brand {
  3. private Integer id;
  4. private String brandName;
  5. private String companyName;
  6. private Integer ordered;
  7. private String description;
  8. private Integer status;
  9. public String getStatusStr(){
  10.      if (status==null){
  11.          return "未知";
  12.      }
  13.      return status==0? "禁用":"启用";
  14. }
  15. public Integer getId() {
  16.      return id;
  17. }
  18. public void setId(Integer id) {
  19.      this.id = id;
  20. }
  21. public String getBrandName() {
  22.      return brandName;
  23. }
  24. public void setBrandName(String brandName) {
  25.      this.brandName = brandName;
  26. }
  27. public String getCompanyName() {
  28.      return companyName;
  29. }
  30. public void setCompanyName(String companyName) {
  31.      this.companyName = companyName;
  32. }
  33. public Integer getOrdered() {
  34.      return ordered;
  35. }
  36. public void setOrdered(Integer ordered) {
  37.      this.ordered = ordered;
  38. }
  39. public String getDescription() {
  40.      return description;
  41. }
  42. public void setDescription(String description) {
  43.      this.description = description;
  44. }
  45. public Integer getStatus() {
  46.      return status;
  47. }
  48. public void setStatus(Integer status) {
  49.      this.status = status;
  50. }
  51. }
复制代码
PageBean.java
  1. package com.itwen.pojo;
  2. import java.util.List;
  3. public class PageBean<T> {
  4. //总记录数
  5. private int totalCount;
  6. //当前页数据
  7. private List<T> rows;//限制数据类型是为了防止存入的数据类型不一致,不使用<Brand>使用<T>是考虑到以后可能不只是用来存储Brand数组,例如查询用户列表
  8. public int getTotalCount() {
  9.      return totalCount;
  10. }
  11. public void setTotalCount(int totalCount) {
  12.      this.totalCount = totalCount;
  13. }
  14. public List<T> getRows() {
  15.      return rows;
  16. }
  17. public void setRows(List<T> rows) {
  18.      this.rows = rows;
  19. }
  20. }
复制代码
service
impl
BrandServiceImpl.java
  1. package com.itwen.service.impl;
  2. import com.itwen.mapper.BrandMapper;
  3. import com.itwen.pojo.Brand;
  4. import com.itwen.pojo.PageBean;
  5. import com.itwen.service.BrandService;
  6. import com.itwen.util.SqlSessionFactoryUtils;
  7. import org.apache.ibatis.session.SqlSession;
  8. import org.apache.ibatis.session.SqlSessionFactory;
  9. import java.util.List;
  10. public class BrandServiceImpl implements BrandService {
  11. //创建sqlSessionFactory工厂对象
  12. SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
  13. public List<Brand> selectAll(){
  14.      //获取sqlSession对象
  15.      SqlSession sqlSession = sqlSessionFactory.openSession();
  16.      //获取BrandMapper
  17.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  18.      //调用方法
  19.      List<Brand> brands = mapper.selectAll();
  20.      //释放资源
  21.      sqlSession.close();
  22.      return brands;
  23. }
  24. @Override
  25. public void add(Brand brand) {
  26.      //获取sqlSession对象
  27.      SqlSession sqlSession = sqlSessionFactory.openSession();
  28.      //获取BrandMapper
  29.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  30.      //调用方法
  31.      mapper.add(brand);
  32.      sqlSession.commit();//提交事务
  33.      //释放资源
  34.      sqlSession.close();
  35. }
  36. @Override
  37. public void update(Brand brand) {
  38.      //获取sqlSession对象
  39.      SqlSession sqlSession = sqlSessionFactory.openSession();
  40.      //获取BrandMapper
  41.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  42.      //调用方法
  43.      mapper.update(brand);
  44.      sqlSession.commit();//提交事务
  45.      //释放资源
  46.      sqlSession.close();
  47. }
  48. @Override
  49. public void deleteById(int id) {
  50.      //获取sqlSession对象
  51.      SqlSession sqlSession = sqlSessionFactory.openSession();
  52.      //获取BrandMapper
  53.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  54.      //调用方法
  55.      mapper.deleteById(id);
  56.      sqlSession.commit();//提交事务
  57.      //释放资源
  58.      sqlSession.close();
  59. }
  60. @Override
  61. public void deleteByIds(int[] ids) {
  62.      //获取sqlSession对象
  63.      SqlSession sqlSession = sqlSessionFactory.openSession();
  64.      //获取BrandMapper
  65.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  66.      //调用方法
  67.      mapper.deleteByIds(ids);
  68.      sqlSession.commit();//提交事务
  69.      //释放资源
  70.      sqlSession.close();
  71. }
  72. @Override
  73. public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
  74.      //获取sqlSession对象
  75.      SqlSession sqlSession = sqlSessionFactory.openSession();
  76.      //获取BrandMapper
  77.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  78.      //调用方法
  79.      int totalCount = mapper.selectTotalCount();//总记录数
  80.      int begin=(currentPage-1)*pageSize;
  81.      List<Brand> brands = mapper.selectByPage(begin, pageSize);//当前页数据
  82.      //封装
  83.      PageBean<Brand> pageBean = new PageBean<>();
  84.      pageBean.setTotalCount(totalCount);
  85.      pageBean.setRows(brands);
  86.      //释放资源
  87.      sqlSession.close();
  88.      return pageBean;
  89. }
  90. @Override
  91. public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
  92.      //获取sqlSession对象
  93.      SqlSession sqlSession = sqlSessionFactory.openSession();
  94.      //获取BrandMapper
  95.      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  96.      //模糊查询,需要加上%(不能在写sql语句部分加,会出问题。例如: like %‘华为’%)
  97.      String brandName = brand.getBrandName();
  98.      if (brandName!=null && brandName.length()>0){
  99.          brand.setBrandName("%"+brandName+"%");
  100.      }
  101.      String companyName = brand.getCompanyName();
  102.      if (companyName!=null && companyName.length()>0){
  103.          brand.setCompanyName("%"+companyName+"%");
  104.      }
  105.      //调用方法
  106.      int totalCount = mapper.selectTotalCountByCondition(brand);//总记录数
  107.      int begin=(currentPage-1)*pageSize;
  108.      List<Brand> brands = mapper.selectByPageAndCondition(begin, pageSize,brand);//当前页数据
  109.      //封装
  110.      PageBean<Brand> pageBean = new PageBean<>();
  111.      pageBean.setTotalCount(totalCount);
  112.      pageBean.setRows(brands);
  113.      //释放资源
  114.      sqlSession.close();
  115.      return pageBean;
  116. }
  117. }
复制代码
BrandService.java
  1. package com.itwen.service;
  2. import com.itwen.pojo.Brand;
  3. import com.itwen.pojo.PageBean;
  4. import java.util.List;
  5. //这里service使用接口是为了降低与servlet的耦合度,目前展示没多大用,以后学了sping就懂了
  6. public interface BrandService {
  7.     /**
  8.      * 查询所有
  9.      * @return
  10.      */
  11.     List<Brand> selectAll();
  12.     /**
  13.      * 添加数据
  14.      * @param brand
  15.      */
  16.     void add(Brand brand);
  17.     /**
  18.      * 修改数据
  19.      * @param brand
  20.      */
  21.     void update(Brand brand);
  22.     /**
  23.      * 删除数据
  24.      * @param id
  25.      */
  26.     void deleteById(int id);
  27.     /**
  28.      * 批量删除
  29.      * @param ids
  30.      */
  31.     void deleteByIds(int[] ids);
  32.     /**
  33.      * 分页查询
  34.      * @param currentPage 当前页码
  35.      * @param pageSize 每页显示条数
  36.      * @return
  37.      */
  38.     PageBean<Brand> selectByPage(int currentPage, int pageSize);
  39.     /**
  40.      * 分页条件查询
  41.      * @param currentPage
  42.      * @param pageSize
  43.      * @param brand
  44.      * @return
  45.      */
  46.     PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize,Brand brand);
  47. }
复制代码
util
SqlSessionFactoryUtils.java
  1. package com.itwen.util;
  2. import org.apache.ibatis.io.Resources;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. public class SqlSessionFactoryUtils {
  8. private static SqlSessionFactory sqlSessionFactory;
  9. static {
  10.      try {
  11.          String resource = "mybatis-config.xml";
  12.          InputStream inputStream = Resources.getResourceAsStream(resource);
  13.          sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  14.      } catch (IOException e) {
  15.          e.printStackTrace();
  16.      }
  17. }
  18. public static SqlSessionFactory getSqlSessionFactory() {
  19.      return sqlSessionFactory;
  20. }
  21. }
复制代码
web.servlet
BaseServlet.java
  1. package com.itwen.web.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.lang.reflect.InvocationTargetException;
  8. import java.lang.reflect.Method;
  9. public class BaseServlet extends HttpServlet {
  10. @Override
  11. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12.      //获取请求路径
  13.      String uri = req.getRequestURI();// brand-case/brand/selectAll
  14. //        System.out.println(uri);
  15.      //获取最后一段路径(也是方法名)
  16.      int index = uri.lastIndexOf('/');
  17.      String methodName = uri.substring(index+1);// /selectAll
  18. //        System.out.println(methodName);
  19.      Class<? extends BaseServlet> cls = this.getClass();//获取当前类
  20.      //获取方法Method对象
  21.      try {
  22.          Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
  23.          method.invoke(this,req,resp);
  24.      } catch (NoSuchMethodException e) {
  25.          e.printStackTrace();
  26.      } catch (IllegalAccessException e) {
  27.          e.printStackTrace();
  28.      } catch (InvocationTargetException e) {
  29.          e.printStackTrace();
  30.      }
  31. }
  32. }
复制代码
mybatis-config.xml
  1. package com.itwen.web.servlet;
  2. import com.alibaba.fastjson.JSON;
  3. import com.itwen.pojo.Brand;
  4. import com.itwen.pojo.PageBean;
  5. import com.itwen.service.BrandService;
  6. import com.itwen.service.impl.BrandServiceImpl;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.util.List;
  14. @WebServlet("/brand/*")
  15. public class BrandServlet extends BaseServlet{
  16. private BrandService brandService=new BrandServiceImpl();
  17. /**
  18.      * 查询所有
  19.      * @param req
  20.      * @param resp
  21.      * @throws ServletException
  22.      * @throws IOException
  23.      */
  24.     public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25.         //调用service查询
  26.         List<Brand> brands = brandService.selectAll();
  27.         //数据转为json
  28.         String jsonString = JSON.toJSONString(brands);
  29.         //写数据
  30.         resp.setContentType("text/json;charset=utf-8");
  31.         resp.getWriter().write(jsonString);
  32.     }
  33.     /**
  34.      * 添加品牌
  35.      * @param req
  36.      * @param resp
  37.      * @throws ServletException
  38.      * @throws IOException
  39.      */
  40.     public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  41.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  42.         //接收品牌数据
  43.         BufferedReader br = req.getReader();
  44.         String params = br.readLine();
  45.         //转为Brand对象
  46.         Brand brand = JSON.parseObject(params, Brand.class);
  47.         //调用service添加
  48.         brandService.add(brand);
  49.         //响应成功标识
  50.         resp.getWriter().write("success");
  51.     }
  52.     /**
  53.      * 修改信息
  54.      * @param req
  55.      * @param resp
  56.      * @throws ServletException
  57.      * @throws IOException
  58.      */
  59.     public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  60.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  61.         //接收品牌数据
  62.         BufferedReader br = req.getReader();
  63.         String params = br.readLine();
  64.         //转为Brand对象
  65.         Brand brand = JSON.parseObject(params, Brand.class);
  66.         //调用service添加
  67.         brandService.update(brand);
  68.         //响应成功标识
  69.         resp.getWriter().write("success");
  70.     }
  71.     public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  72.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  73.         //接收品牌数据
  74.         BufferedReader br = req.getReader();
  75.         String params = br.readLine();
  76.         //转为 int
  77.         int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
  78.         //调用services批量删除
  79.         brandService.deleteById(id);
  80.         //响应成功标识
  81.         resp.getWriter().write("success");
  82.     }
  83.     /**
  84.      * 批量删除品牌
  85.      * @param req
  86.      * @param resp
  87.      * @throws ServletException
  88.      * @throws IOException
  89.      */
  90.     public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  91.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  92.         //接收品牌数据
  93.         BufferedReader br = req.getReader();
  94.         String params = br.readLine();
  95.         //转为 int[]
  96.         int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
  97.         //调用services批量删除
  98.         brandService.deleteByIds(ids);
  99.         //响应成功标识
  100.         resp.getWriter().write("success");
  101.     }
  102.     /**
  103.      * 分页查询
  104.      * @param req
  105.      * @param resp
  106.      * @throws ServletException
  107.      * @throws IOException
  108.      */
  109.     public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  110.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  111.         String _currentPage = req.getParameter("currentPage");
  112.         String _pageSize = req.getParameter("pageSize");
  113.         int currentPage = Integer.parseInt(_currentPage);
  114.         int pageSize = Integer.parseInt(_pageSize);
  115.         //调用service查询
  116.         PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
  117.         //数据转为json
  118.         String jsonString = JSON.toJSONString(brandPageBean);
  119.         //写数据
  120.         resp.setContentType("text/json;charset=utf-8");
  121.         resp.getWriter().write(jsonString);
  122.     }
  123.     public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  124.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  125.         String _currentPage = req.getParameter("currentPage");
  126.         String _pageSize = req.getParameter("pageSize");
  127.         int currentPage = Integer.parseInt(_currentPage);
  128.         int pageSize = Integer.parseInt(_pageSize);
  129.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  130.         //接收品牌数据
  131.         BufferedReader br = req.getReader();
  132.         String params = br.readLine();
  133.         //转为Brand对象
  134.         Brand brand = JSON.parseObject(params, Brand.class);
  135.         //调用service查询
  136.         PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
  137.         //数据转为json
  138.         String jsonString = JSON.toJSONString(pageBean);
  139.         //写数据
  140.         resp.setContentType("text/json;charset=utf-8");
  141.         resp.getWriter().write(jsonString);
  142.     }
  143. }
复制代码
webapp
WEB-INF
web.xml(这个不自己写,创建项目结构时使用idea生成)
  1. [/code][/indent]brand.html
  2. [code]        Titlepackage com.itwen.web.servlet;
  3. import com.alibaba.fastjson.JSON;
  4. import com.itwen.pojo.Brand;
  5. import com.itwen.pojo.PageBean;
  6. import com.itwen.service.BrandService;
  7. import com.itwen.service.impl.BrandServiceImpl;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.util.List;
  15. @WebServlet("/brand/*")
  16. public class BrandServlet extends BaseServlet{
  17. private BrandService brandService=new BrandServiceImpl();
  18. /**
  19.      * 查询所有
  20.      * @param req
  21.      * @param resp
  22.      * @throws ServletException
  23.      * @throws IOException
  24.      */
  25.     public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  26.         //调用service查询
  27.         List<Brand> brands = brandService.selectAll();
  28.         //数据转为json
  29.         String jsonString = JSON.toJSONString(brands);
  30.         //写数据
  31.         resp.setContentType("text/json;charset=utf-8");
  32.         resp.getWriter().write(jsonString);
  33.     }
  34.     /**
  35.      * 添加品牌
  36.      * @param req
  37.      * @param resp
  38.      * @throws ServletException
  39.      * @throws IOException
  40.      */
  41.     public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  42.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  43.         //接收品牌数据
  44.         BufferedReader br = req.getReader();
  45.         String params = br.readLine();
  46.         //转为Brand对象
  47.         Brand brand = JSON.parseObject(params, Brand.class);
  48.         //调用service添加
  49.         brandService.add(brand);
  50.         //响应成功标识
  51.         resp.getWriter().write("success");
  52.     }
  53.     /**
  54.      * 修改信息
  55.      * @param req
  56.      * @param resp
  57.      * @throws ServletException
  58.      * @throws IOException
  59.      */
  60.     public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  61.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  62.         //接收品牌数据
  63.         BufferedReader br = req.getReader();
  64.         String params = br.readLine();
  65.         //转为Brand对象
  66.         Brand brand = JSON.parseObject(params, Brand.class);
  67.         //调用service添加
  68.         brandService.update(brand);
  69.         //响应成功标识
  70.         resp.getWriter().write("success");
  71.     }
  72.     public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  73.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  74.         //接收品牌数据
  75.         BufferedReader br = req.getReader();
  76.         String params = br.readLine();
  77.         //转为 int
  78.         int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
  79.         //调用services批量删除
  80.         brandService.deleteById(id);
  81.         //响应成功标识
  82.         resp.getWriter().write("success");
  83.     }
  84.     /**
  85.      * 批量删除品牌
  86.      * @param req
  87.      * @param resp
  88.      * @throws ServletException
  89.      * @throws IOException
  90.      */
  91.     public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  92.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  93.         //接收品牌数据
  94.         BufferedReader br = req.getReader();
  95.         String params = br.readLine();
  96.         //转为 int[]
  97.         int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
  98.         //调用services批量删除
  99.         brandService.deleteByIds(ids);
  100.         //响应成功标识
  101.         resp.getWriter().write("success");
  102.     }
  103.     /**
  104.      * 分页查询
  105.      * @param req
  106.      * @param resp
  107.      * @throws ServletException
  108.      * @throws IOException
  109.      */
  110.     public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  111.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  112.         String _currentPage = req.getParameter("currentPage");
  113.         String _pageSize = req.getParameter("pageSize");
  114.         int currentPage = Integer.parseInt(_currentPage);
  115.         int pageSize = Integer.parseInt(_pageSize);
  116.         //调用service查询
  117.         PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
  118.         //数据转为json
  119.         String jsonString = JSON.toJSONString(brandPageBean);
  120.         //写数据
  121.         resp.setContentType("text/json;charset=utf-8");
  122.         resp.getWriter().write(jsonString);
  123.     }
  124.     public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  125.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  126.         String _currentPage = req.getParameter("currentPage");
  127.         String _pageSize = req.getParameter("pageSize");
  128.         int currentPage = Integer.parseInt(_currentPage);
  129.         int pageSize = Integer.parseInt(_pageSize);
  130.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  131.         //接收品牌数据
  132.         BufferedReader br = req.getReader();
  133.         String params = br.readLine();
  134.         //转为Brand对象
  135.         Brand brand = JSON.parseObject(params, Brand.class);
  136.         //调用service查询
  137.         PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
  138.         //数据转为json
  139.         String jsonString = JSON.toJSONString(pageBean);
  140.         //写数据
  141.         resp.setContentType("text/json;charset=utf-8");
  142.         resp.getWriter().write(jsonString);
  143.     }
  144. }                        查询                            批量删除        新增package com.itwen.web.servlet;
  145. import com.alibaba.fastjson.JSON;
  146. import com.itwen.pojo.Brand;
  147. import com.itwen.pojo.PageBean;
  148. import com.itwen.service.BrandService;
  149. import com.itwen.service.impl.BrandServiceImpl;
  150. import javax.servlet.ServletException;
  151. import javax.servlet.annotation.WebServlet;
  152. import javax.servlet.http.HttpServletRequest;
  153. import javax.servlet.http.HttpServletResponse;
  154. import java.io.BufferedReader;
  155. import java.io.IOException;
  156. import java.util.List;
  157. @WebServlet("/brand/*")
  158. public class BrandServlet extends BaseServlet{
  159. private BrandService brandService=new BrandServiceImpl();
  160. /**
  161.      * 查询所有
  162.      * @param req
  163.      * @param resp
  164.      * @throws ServletException
  165.      * @throws IOException
  166.      */
  167.     public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  168.         //调用service查询
  169.         List<Brand> brands = brandService.selectAll();
  170.         //数据转为json
  171.         String jsonString = JSON.toJSONString(brands);
  172.         //写数据
  173.         resp.setContentType("text/json;charset=utf-8");
  174.         resp.getWriter().write(jsonString);
  175.     }
  176.     /**
  177.      * 添加品牌
  178.      * @param req
  179.      * @param resp
  180.      * @throws ServletException
  181.      * @throws IOException
  182.      */
  183.     public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  184.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  185.         //接收品牌数据
  186.         BufferedReader br = req.getReader();
  187.         String params = br.readLine();
  188.         //转为Brand对象
  189.         Brand brand = JSON.parseObject(params, Brand.class);
  190.         //调用service添加
  191.         brandService.add(brand);
  192.         //响应成功标识
  193.         resp.getWriter().write("success");
  194.     }
  195.     /**
  196.      * 修改信息
  197.      * @param req
  198.      * @param resp
  199.      * @throws ServletException
  200.      * @throws IOException
  201.      */
  202.     public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  203.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  204.         //接收品牌数据
  205.         BufferedReader br = req.getReader();
  206.         String params = br.readLine();
  207.         //转为Brand对象
  208.         Brand brand = JSON.parseObject(params, Brand.class);
  209.         //调用service添加
  210.         brandService.update(brand);
  211.         //响应成功标识
  212.         resp.getWriter().write("success");
  213.     }
  214.     public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  215.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  216.         //接收品牌数据
  217.         BufferedReader br = req.getReader();
  218.         String params = br.readLine();
  219.         //转为 int
  220.         int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
  221.         //调用services批量删除
  222.         brandService.deleteById(id);
  223.         //响应成功标识
  224.         resp.getWriter().write("success");
  225.     }
  226.     /**
  227.      * 批量删除品牌
  228.      * @param req
  229.      * @param resp
  230.      * @throws ServletException
  231.      * @throws IOException
  232.      */
  233.     public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  234.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  235.         //接收品牌数据
  236.         BufferedReader br = req.getReader();
  237.         String params = br.readLine();
  238.         //转为 int[]
  239.         int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
  240.         //调用services批量删除
  241.         brandService.deleteByIds(ids);
  242.         //响应成功标识
  243.         resp.getWriter().write("success");
  244.     }
  245.     /**
  246.      * 分页查询
  247.      * @param req
  248.      * @param resp
  249.      * @throws ServletException
  250.      * @throws IOException
  251.      */
  252.     public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  253.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  254.         String _currentPage = req.getParameter("currentPage");
  255.         String _pageSize = req.getParameter("pageSize");
  256.         int currentPage = Integer.parseInt(_currentPage);
  257.         int pageSize = Integer.parseInt(_pageSize);
  258.         //调用service查询
  259.         PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
  260.         //数据转为json
  261.         String jsonString = JSON.toJSONString(brandPageBean);
  262.         //写数据
  263.         resp.setContentType("text/json;charset=utf-8");
  264.         resp.getWriter().write(jsonString);
  265.     }
  266.     public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  267.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  268.         String _currentPage = req.getParameter("currentPage");
  269.         String _pageSize = req.getParameter("pageSize");
  270.         int currentPage = Integer.parseInt(_currentPage);
  271.         int pageSize = Integer.parseInt(_pageSize);
  272.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  273.         //接收品牌数据
  274.         BufferedReader br = req.getReader();
  275.         String params = br.readLine();
  276.         //转为Brand对象
  277.         Brand brand = JSON.parseObject(params, Brand.class);
  278.         //调用service查询
  279.         PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
  280.         //数据转为json
  281.         String jsonString = JSON.toJSONString(pageBean);
  282.         //写数据
  283.         resp.setContentType("text/json;charset=utf-8");
  284.         resp.getWriter().write(jsonString);
  285.     }
  286. }                                                                                提交                取消package com.itwen.web.servlet;
  287. import com.alibaba.fastjson.JSON;
  288. import com.itwen.pojo.Brand;
  289. import com.itwen.pojo.PageBean;
  290. import com.itwen.service.BrandService;
  291. import com.itwen.service.impl.BrandServiceImpl;
  292. import javax.servlet.ServletException;
  293. import javax.servlet.annotation.WebServlet;
  294. import javax.servlet.http.HttpServletRequest;
  295. import javax.servlet.http.HttpServletResponse;
  296. import java.io.BufferedReader;
  297. import java.io.IOException;
  298. import java.util.List;
  299. @WebServlet("/brand/*")
  300. public class BrandServlet extends BaseServlet{
  301. private BrandService brandService=new BrandServiceImpl();
  302. /**
  303.      * 查询所有
  304.      * @param req
  305.      * @param resp
  306.      * @throws ServletException
  307.      * @throws IOException
  308.      */
  309.     public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  310.         //调用service查询
  311.         List<Brand> brands = brandService.selectAll();
  312.         //数据转为json
  313.         String jsonString = JSON.toJSONString(brands);
  314.         //写数据
  315.         resp.setContentType("text/json;charset=utf-8");
  316.         resp.getWriter().write(jsonString);
  317.     }
  318.     /**
  319.      * 添加品牌
  320.      * @param req
  321.      * @param resp
  322.      * @throws ServletException
  323.      * @throws IOException
  324.      */
  325.     public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  326.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  327.         //接收品牌数据
  328.         BufferedReader br = req.getReader();
  329.         String params = br.readLine();
  330.         //转为Brand对象
  331.         Brand brand = JSON.parseObject(params, Brand.class);
  332.         //调用service添加
  333.         brandService.add(brand);
  334.         //响应成功标识
  335.         resp.getWriter().write("success");
  336.     }
  337.     /**
  338.      * 修改信息
  339.      * @param req
  340.      * @param resp
  341.      * @throws ServletException
  342.      * @throws IOException
  343.      */
  344.     public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  345.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  346.         //接收品牌数据
  347.         BufferedReader br = req.getReader();
  348.         String params = br.readLine();
  349.         //转为Brand对象
  350.         Brand brand = JSON.parseObject(params, Brand.class);
  351.         //调用service添加
  352.         brandService.update(brand);
  353.         //响应成功标识
  354.         resp.getWriter().write("success");
  355.     }
  356.     public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  357.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  358.         //接收品牌数据
  359.         BufferedReader br = req.getReader();
  360.         String params = br.readLine();
  361.         //转为 int
  362.         int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
  363.         //调用services批量删除
  364.         brandService.deleteById(id);
  365.         //响应成功标识
  366.         resp.getWriter().write("success");
  367.     }
  368.     /**
  369.      * 批量删除品牌
  370.      * @param req
  371.      * @param resp
  372.      * @throws ServletException
  373.      * @throws IOException
  374.      */
  375.     public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  376.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  377.         //接收品牌数据
  378.         BufferedReader br = req.getReader();
  379.         String params = br.readLine();
  380.         //转为 int[]
  381.         int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
  382.         //调用services批量删除
  383.         brandService.deleteByIds(ids);
  384.         //响应成功标识
  385.         resp.getWriter().write("success");
  386.     }
  387.     /**
  388.      * 分页查询
  389.      * @param req
  390.      * @param resp
  391.      * @throws ServletException
  392.      * @throws IOException
  393.      */
  394.     public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  395.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  396.         String _currentPage = req.getParameter("currentPage");
  397.         String _pageSize = req.getParameter("pageSize");
  398.         int currentPage = Integer.parseInt(_currentPage);
  399.         int pageSize = Integer.parseInt(_pageSize);
  400.         //调用service查询
  401.         PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
  402.         //数据转为json
  403.         String jsonString = JSON.toJSONString(brandPageBean);
  404.         //写数据
  405.         resp.setContentType("text/json;charset=utf-8");
  406.         resp.getWriter().write(jsonString);
  407.     }
  408.     public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  409.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  410.         String _currentPage = req.getParameter("currentPage");
  411.         String _pageSize = req.getParameter("pageSize");
  412.         int currentPage = Integer.parseInt(_currentPage);
  413.         int pageSize = Integer.parseInt(_pageSize);
  414.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  415.         //接收品牌数据
  416.         BufferedReader br = req.getReader();
  417.         String params = br.readLine();
  418.         //转为Brand对象
  419.         Brand brand = JSON.parseObject(params, Brand.class);
  420.         //调用service查询
  421.         PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
  422.         //数据转为json
  423.         String jsonString = JSON.toJSONString(pageBean);
  424.         //写数据
  425.         resp.setContentType("text/json;charset=utf-8");
  426.         resp.getWriter().write(jsonString);
  427.     }
  428. }                                                                        修改                    删除package com.itwen.web.servlet;
  429. import com.alibaba.fastjson.JSON;
  430. import com.itwen.pojo.Brand;
  431. import com.itwen.pojo.PageBean;
  432. import com.itwen.service.BrandService;
  433. import com.itwen.service.impl.BrandServiceImpl;
  434. import javax.servlet.ServletException;
  435. import javax.servlet.annotation.WebServlet;
  436. import javax.servlet.http.HttpServletRequest;
  437. import javax.servlet.http.HttpServletResponse;
  438. import java.io.BufferedReader;
  439. import java.io.IOException;
  440. import java.util.List;
  441. @WebServlet("/brand/*")
  442. public class BrandServlet extends BaseServlet{
  443. private BrandService brandService=new BrandServiceImpl();
  444. /**
  445.      * 查询所有
  446.      * @param req
  447.      * @param resp
  448.      * @throws ServletException
  449.      * @throws IOException
  450.      */
  451.     public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  452.         //调用service查询
  453.         List<Brand> brands = brandService.selectAll();
  454.         //数据转为json
  455.         String jsonString = JSON.toJSONString(brands);
  456.         //写数据
  457.         resp.setContentType("text/json;charset=utf-8");
  458.         resp.getWriter().write(jsonString);
  459.     }
  460.     /**
  461.      * 添加品牌
  462.      * @param req
  463.      * @param resp
  464.      * @throws ServletException
  465.      * @throws IOException
  466.      */
  467.     public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  468.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  469.         //接收品牌数据
  470.         BufferedReader br = req.getReader();
  471.         String params = br.readLine();
  472.         //转为Brand对象
  473.         Brand brand = JSON.parseObject(params, Brand.class);
  474.         //调用service添加
  475.         brandService.add(brand);
  476.         //响应成功标识
  477.         resp.getWriter().write("success");
  478.     }
  479.     /**
  480.      * 修改信息
  481.      * @param req
  482.      * @param resp
  483.      * @throws ServletException
  484.      * @throws IOException
  485.      */
  486.     public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  487.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  488.         //接收品牌数据
  489.         BufferedReader br = req.getReader();
  490.         String params = br.readLine();
  491.         //转为Brand对象
  492.         Brand brand = JSON.parseObject(params, Brand.class);
  493.         //调用service添加
  494.         brandService.update(brand);
  495.         //响应成功标识
  496.         resp.getWriter().write("success");
  497.     }
  498.     public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  499.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  500.         //接收品牌数据
  501.         BufferedReader br = req.getReader();
  502.         String params = br.readLine();
  503.         //转为 int
  504.         int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
  505.         //调用services批量删除
  506.         brandService.deleteById(id);
  507.         //响应成功标识
  508.         resp.getWriter().write("success");
  509.     }
  510.     /**
  511.      * 批量删除品牌
  512.      * @param req
  513.      * @param resp
  514.      * @throws ServletException
  515.      * @throws IOException
  516.      */
  517.     public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  518.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  519.         //接收品牌数据
  520.         BufferedReader br = req.getReader();
  521.         String params = br.readLine();
  522.         //转为 int[]
  523.         int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
  524.         //调用services批量删除
  525.         brandService.deleteByIds(ids);
  526.         //响应成功标识
  527.         resp.getWriter().write("success");
  528.     }
  529.     /**
  530.      * 分页查询
  531.      * @param req
  532.      * @param resp
  533.      * @throws ServletException
  534.      * @throws IOException
  535.      */
  536.     public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  537.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  538.         String _currentPage = req.getParameter("currentPage");
  539.         String _pageSize = req.getParameter("pageSize");
  540.         int currentPage = Integer.parseInt(_currentPage);
  541.         int pageSize = Integer.parseInt(_pageSize);
  542.         //调用service查询
  543.         PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
  544.         //数据转为json
  545.         String jsonString = JSON.toJSONString(brandPageBean);
  546.         //写数据
  547.         resp.setContentType("text/json;charset=utf-8");
  548.         resp.getWriter().write(jsonString);
  549.     }
  550.     public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  551.         //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
  552.         String _currentPage = req.getParameter("currentPage");
  553.         String _pageSize = req.getParameter("pageSize");
  554.         int currentPage = Integer.parseInt(_currentPage);
  555.         int pageSize = Integer.parseInt(_pageSize);
  556.         req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
  557.         //接收品牌数据
  558.         BufferedReader br = req.getReader();
  559.         String params = br.readLine();
  560.         //转为Brand对象
  561.         Brand brand = JSON.parseObject(params, Brand.class);
  562.         //调用service查询
  563.         PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
  564.         //数据转为json
  565.         String jsonString = JSON.toJSONString(pageBean);
  566.         //写数据
  567.         resp.setContentType("text/json;charset=utf-8");
  568.         resp.getWriter().write(jsonString);
  569.     }
  570. }                                                                                                                    提交                取消                                    
复制代码
pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5.       version="4.0">
  6. </web-app>
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4