java
com.itwen
mappermybatis-config.xml
BrandMapper.javapojo复制代码
- package com.itwen.mapper;
- import com.itwen.pojo.Brand;
- import org.apache.ibatis.annotations.*;
- import java.util.List;
- 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);
- /**
- * 修改数据
- * @param brand
- */
- @Update("UPDATE tb_brand SET brand_name= #{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description},status=#{status} WHERE id=#{id}")
- void update(Brand brand);
- /**
- * 删除数据
- * @param id
- */
- @Delete("delete from tb_brand where id=#{id}")
- void deleteById(int id);
- /**
- * 批量删除
- * @param ids
- */
- void deleteByIds(@Param("ids") int[] ids);
- /**
- * 分页查询
- * @param begin
- * @param size
- * @return
- */
- @Select("select * from tb_brand limit #{begin},#{size}")
- @ResultMap("brandResultMap")
- List<Brand> selectByPage(@Param("begin") int begin,@Param("size") int size);
- /**
- * 查询总记录数
- * @return
- */
- @Select("select count(*) from tb_brand")
- int selectTotalCount();
- /**
- * 分页条件查询
- * @param begin
- * @param size
- * @param brand
- * @return
- */
- List<Brand> selectByPageAndCondition(@Param("begin") int begin,@Param("size") int size,@Param("brand") Brand brand);
- /**
- * 查询总记录数
- * @return
- */
- int selectTotalCountByCondition(Brand brand);
- }
Brand.javaservicePageBean.java复制代码
- package com.itwen.pojo;
- public class Brand {
- private Integer id;
- private String brandName;
- private String companyName;
- private Integer ordered;
- private String description;
- private Integer status;
- public String getStatusStr(){
- if (status==null){
- return "未知";
- }
- return status==0? "禁用":"启用";
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getBrandName() {
- return brandName;
- }
- public void setBrandName(String brandName) {
- this.brandName = brandName;
- }
- public String getCompanyName() {
- return companyName;
- }
- public void setCompanyName(String companyName) {
- this.companyName = companyName;
- }
- public Integer getOrdered() {
- return ordered;
- }
- public void setOrdered(Integer ordered) {
- this.ordered = ordered;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public Integer getStatus() {
- return status;
- }
- public void setStatus(Integer status) {
- this.status = status;
- }
- }
复制代码
- package com.itwen.pojo;
- import java.util.List;
- public class PageBean<T> {
- //总记录数
- private int totalCount;
- //当前页数据
- private List<T> rows;//限制数据类型是为了防止存入的数据类型不一致,不使用<Brand>使用<T>是考虑到以后可能不只是用来存储Brand数组,例如查询用户列表
- public int getTotalCount() {
- return totalCount;
- }
- public void setTotalCount(int totalCount) {
- this.totalCount = totalCount;
- }
- public List<T> getRows() {
- return rows;
- }
- public void setRows(List<T> rows) {
- this.rows = rows;
- }
- }
implutil
BrandServiceImpl.javaBrandService.java复制代码
- package com.itwen.service.impl;
- import com.itwen.mapper.BrandMapper;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import com.itwen.service.BrandService;
- import com.itwen.util.SqlSessionFactoryUtils;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import java.util.List;
- public class BrandServiceImpl implements BrandService {
- //创建sqlSessionFactory工厂对象
- SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
- 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;
- }
- @Override
- public void add(Brand brand) {
- //获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //获取BrandMapper
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
- //调用方法
- mapper.add(brand);
- sqlSession.commit();//提交事务
- //释放资源
- sqlSession.close();
- }
- @Override
- public void update(Brand brand) {
- //获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //获取BrandMapper
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
- //调用方法
- mapper.update(brand);
- sqlSession.commit();//提交事务
- //释放资源
- sqlSession.close();
- }
- @Override
- public void deleteById(int id) {
- //获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //获取BrandMapper
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
- //调用方法
- mapper.deleteById(id);
- sqlSession.commit();//提交事务
- //释放资源
- sqlSession.close();
- }
- @Override
- public void deleteByIds(int[] ids) {
- //获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //获取BrandMapper
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
- //调用方法
- mapper.deleteByIds(ids);
- sqlSession.commit();//提交事务
- //释放资源
- sqlSession.close();
- }
- @Override
- public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
- //获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //获取BrandMapper
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
- //调用方法
- int totalCount = mapper.selectTotalCount();//总记录数
- int begin=(currentPage-1)*pageSize;
- List<Brand> brands = mapper.selectByPage(begin, pageSize);//当前页数据
- //封装
- PageBean<Brand> pageBean = new PageBean<>();
- pageBean.setTotalCount(totalCount);
- pageBean.setRows(brands);
- //释放资源
- sqlSession.close();
- return pageBean;
- }
- @Override
- public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
- //获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //获取BrandMapper
- BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
- //模糊查询,需要加上%(不能在写sql语句部分加,会出问题。例如: like %‘华为’%)
- String brandName = brand.getBrandName();
- if (brandName!=null && brandName.length()>0){
- brand.setBrandName("%"+brandName+"%");
- }
- String companyName = brand.getCompanyName();
- if (companyName!=null && companyName.length()>0){
- brand.setCompanyName("%"+companyName+"%");
- }
- //调用方法
- int totalCount = mapper.selectTotalCountByCondition(brand);//总记录数
- int begin=(currentPage-1)*pageSize;
- List<Brand> brands = mapper.selectByPageAndCondition(begin, pageSize,brand);//当前页数据
- //封装
- PageBean<Brand> pageBean = new PageBean<>();
- pageBean.setTotalCount(totalCount);
- pageBean.setRows(brands);
- //释放资源
- sqlSession.close();
- return pageBean;
- }
- }
复制代码
- package com.itwen.service;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import java.util.List;
- //这里service使用接口是为了降低与servlet的耦合度,目前展示没多大用,以后学了sping就懂了
- public interface BrandService {
- /**
- * 查询所有
- * @return
- */
- List<Brand> selectAll();
- /**
- * 添加数据
- * @param brand
- */
- void add(Brand brand);
- /**
- * 修改数据
- * @param brand
- */
- void update(Brand brand);
- /**
- * 删除数据
- * @param id
- */
- void deleteById(int id);
- /**
- * 批量删除
- * @param ids
- */
- void deleteByIds(int[] ids);
- /**
- * 分页查询
- * @param currentPage 当前页码
- * @param pageSize 每页显示条数
- * @return
- */
- PageBean<Brand> selectByPage(int currentPage, int pageSize);
- /**
- * 分页条件查询
- * @param currentPage
- * @param pageSize
- * @param brand
- * @return
- */
- PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize,Brand brand);
- }
SqlSessionFactoryUtils.javaweb.servlet复制代码
- package com.itwen.util;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import java.io.IOException;
- import java.io.InputStream;
- public class SqlSessionFactoryUtils {
- private static SqlSessionFactory sqlSessionFactory;
- static {
- try {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static SqlSessionFactory getSqlSessionFactory() {
- return sqlSessionFactory;
- }
- }
BaseServlet.java复制代码
- package com.itwen.web.servlet;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- public class BaseServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取请求路径
- String uri = req.getRequestURI();// brand-case/brand/selectAll
- // System.out.println(uri);
- //获取最后一段路径(也是方法名)
- int index = uri.lastIndexOf('/');
- String methodName = uri.substring(index+1);// /selectAll
- // System.out.println(methodName);
- Class<? extends BaseServlet> cls = this.getClass();//获取当前类
- //获取方法Method对象
- try {
- Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
- method.invoke(this,req,resp);
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- }
webapp复制代码
- package com.itwen.web.servlet;
- import com.alibaba.fastjson.JSON;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import com.itwen.service.BrandService;
- import com.itwen.service.impl.BrandServiceImpl;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.List;
- @WebServlet("/brand/*")
- public class BrandServlet extends BaseServlet{
- private BrandService brandService=new BrandServiceImpl();
- /**
- * 查询所有
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List<Brand> brands = brandService.selectAll();
- //数据转为json
- String jsonString = JSON.toJSONString(brands);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- /**
- * 添加品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.add(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 修改信息
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.update(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int
- int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteById(id);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 批量删除品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int[]
- int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteByIds(ids);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 分页查询
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- //调用service查询
- PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
- //数据转为json
- String jsonString = JSON.toJSONString(brandPageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service查询
- PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
- //数据转为json
- String jsonString = JSON.toJSONString(pageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- }
WEB-INFpom.xml
web.xml(这个不自己写,创建项目结构时使用idea生成)复制代码
- [/code][/indent]brand.html
- [code] Titlepackage com.itwen.web.servlet;
- import com.alibaba.fastjson.JSON;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import com.itwen.service.BrandService;
- import com.itwen.service.impl.BrandServiceImpl;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.List;
- @WebServlet("/brand/*")
- public class BrandServlet extends BaseServlet{
- private BrandService brandService=new BrandServiceImpl();
- /**
- * 查询所有
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List<Brand> brands = brandService.selectAll();
- //数据转为json
- String jsonString = JSON.toJSONString(brands);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- /**
- * 添加品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.add(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 修改信息
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.update(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int
- int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteById(id);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 批量删除品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int[]
- int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteByIds(ids);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 分页查询
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- //调用service查询
- PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
- //数据转为json
- String jsonString = JSON.toJSONString(brandPageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service查询
- PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
- //数据转为json
- String jsonString = JSON.toJSONString(pageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- } 查询 批量删除 新增package com.itwen.web.servlet;
- import com.alibaba.fastjson.JSON;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import com.itwen.service.BrandService;
- import com.itwen.service.impl.BrandServiceImpl;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.List;
- @WebServlet("/brand/*")
- public class BrandServlet extends BaseServlet{
- private BrandService brandService=new BrandServiceImpl();
- /**
- * 查询所有
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List<Brand> brands = brandService.selectAll();
- //数据转为json
- String jsonString = JSON.toJSONString(brands);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- /**
- * 添加品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.add(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 修改信息
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.update(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int
- int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteById(id);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 批量删除品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int[]
- int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteByIds(ids);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 分页查询
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- //调用service查询
- PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
- //数据转为json
- String jsonString = JSON.toJSONString(brandPageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service查询
- PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
- //数据转为json
- String jsonString = JSON.toJSONString(pageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- } 提交 取消package com.itwen.web.servlet;
- import com.alibaba.fastjson.JSON;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import com.itwen.service.BrandService;
- import com.itwen.service.impl.BrandServiceImpl;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.List;
- @WebServlet("/brand/*")
- public class BrandServlet extends BaseServlet{
- private BrandService brandService=new BrandServiceImpl();
- /**
- * 查询所有
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List<Brand> brands = brandService.selectAll();
- //数据转为json
- String jsonString = JSON.toJSONString(brands);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- /**
- * 添加品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.add(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 修改信息
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.update(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int
- int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteById(id);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 批量删除品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int[]
- int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteByIds(ids);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 分页查询
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- //调用service查询
- PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
- //数据转为json
- String jsonString = JSON.toJSONString(brandPageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service查询
- PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
- //数据转为json
- String jsonString = JSON.toJSONString(pageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- } 修改 删除package com.itwen.web.servlet;
- import com.alibaba.fastjson.JSON;
- import com.itwen.pojo.Brand;
- import com.itwen.pojo.PageBean;
- import com.itwen.service.BrandService;
- import com.itwen.service.impl.BrandServiceImpl;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.List;
- @WebServlet("/brand/*")
- public class BrandServlet extends BaseServlet{
- private BrandService brandService=new BrandServiceImpl();
- /**
- * 查询所有
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //调用service查询
- List<Brand> brands = brandService.selectAll();
- //数据转为json
- String jsonString = JSON.toJSONString(brands);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- /**
- * 添加品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.add(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 修改信息
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service添加
- brandService.update(brand);
- //响应成功标识
- resp.getWriter().write("success");
- }
- public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int
- int id = JSON.parseObject(params, int.class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteById(id);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 批量删除品牌
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为 int[]
- int[] ids = JSON.parseObject(params, int[].class);//fastjson也可以用来字符串转数组
- //调用services批量删除
- brandService.deleteByIds(ids);
- //响应成功标识
- resp.getWriter().write("success");
- }
- /**
- * 分页查询
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- //调用service查询
- PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
- //数据转为json
- String jsonString = JSON.toJSONString(brandPageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //接收数据 当前页码和每页条数 url?currentPage=1&pageSize=5
- String _currentPage = req.getParameter("currentPage");
- String _pageSize = req.getParameter("pageSize");
- int currentPage = Integer.parseInt(_currentPage);
- int pageSize = Integer.parseInt(_pageSize);
- req.setCharacterEncoding("UTF-8");//如果post的数据乱码,加上这句
- //接收品牌数据
- BufferedReader br = req.getReader();
- String params = br.readLine();
- //转为Brand对象
- Brand brand = JSON.parseObject(params, Brand.class);
- //调用service查询
- PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
- //数据转为json
- String jsonString = JSON.toJSONString(pageBean);
- //写数据
- resp.setContentType("text/json;charset=utf-8");
- resp.getWriter().write(jsonString);
- }
- } 提交 取消
复制代码
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
- </web-app>
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4