JavaWeb项目实战(结合Vue实现增删改查所有功能板块)

效果图如下:JavaWeb项目实战(结合Vue实现增删改查所有功能板块)_第1张图片

JavaWeb项目实战(结合Vue实现增删改查所有功能板块)_第2张图片 

JavaWeb项目实战(结合Vue实现增删改查所有功能板块)_第3张图片 

JavaWeb项目实战(结合Vue实现增删改查所有功能板块)_第4张图片 

JavaWeb项目实战(结合Vue实现增删改查所有功能板块)_第5张图片 

 

 

 

 项目依赖: 

1.mysql驱动包

2.junit用来测试

3.mybatis

4.javax.servlet

5.fastjson用来进行数据转换

准备对象去实现对数据库数据的表现

package cn.itaxu.pojo;

/**
 * @Description: cn.itaxu.pojo
 * @author: Axu
 * @date:2022/11/2 22:28
 */
public class Brand {
    // id 主键
    private Integer id;
    // 品牌名称
    private String brandName;
    // 企业名称
    private String companyName;
    // 排序字段
    private Integer ordered;
    // 描述信息
    private String description;
    // 状态:0:禁用  1:启用
    private Integer status;



    public Brand() {
    }

    public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
        this.id = id;
        this.brandName = brandName;
        this.companyName = companyName;
        this.ordered = ordered;
        this.description = description;
        this.status = status;
    }

    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 String getStatusStr(){
        if (status==null){
            return "未知";
        }
        return status == 0 ? "禁用" : "启用";
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}

PageBean用来实现分页查询的表现

import java.util.List;

/**
 * 分页查询的JavaBean
 * @Description: cn.itaxu.pojo
 * @author: Axu
 * @date:2022/11/7 11:00
 */
public class PageBean {
    // 总记录数
    private int totalCount;
    // 当前页数据
    private List rows;

    public PageBean() {
    }

    public PageBean(int totalCount, List rows) {
        this.totalCount = totalCount;
        this.rows = rows;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "totalCount=" + totalCount +
                ", rows=" + rows +
                '}';
    }
}

 对应的增删改查接口

package cn.itaxu.mapper;


import cn.itaxu.pojo.Brand;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description: cn.itaxu.mapper
 * @author: Axu
 * @date:2022/11/4 20:57
 */
public interface BrandMapper {

    /**
     * 查询所有
     * @return
     */
    @Select("select * from tb_brand")
    @ResultMap("brandResultMap")
    List selectAll();

    /**
     * 添加数据
     * @param brand
     * @return
     */
    @Insert("insert into tb_brand values(#{id},#{brandName},#{companyName}," +
            "#{ordered},#{description},#{status})")
    void add(Brand brand);

    /**
     * 批量删除
     * @param ids
     * @return
     */
    void deleteByIds(@Param("ids") int[] ids);

    /**
     * 更新数据
     * @param brand
     */
    @Update("update tb_brand set brand_name=#{brandName},company_name=#{companyName},ordered=#{ordered}," +
            "brand_info=#{description},status=#{status} where id=#{id}")
    void update(Brand brand);

    /**
     *分页查询
     * @param begin
     * @param size
     * @return
     */
    @Select("select * from tb_brand limit #{begin} , #{size}")
    @ResultMap("brandResultMap")
    List selectByPage(@Param("begin") int begin,@Param("size") int size);

    /**
     * 总记录条数
     * @return
     */
    @Select("select count(*) from tb_brand")
    int selectTotalCount();

    /**
     * 动态 SQL 查询数据
     * @param begin
     * @param size
     * @param brand
     * @return
     */
   List selectByPageAndCondition(@Param("begin") int begin,@Param("size") int size,@Param("brand") Brand brand);

    /**
     * 根据条件查询总记录数
     * @param brand
     * @return
     */
   int selectTotalCountByCondition(Brand brand);

    /**
     * 回显数据
     * @param id
     * @return
     */
   @Select("select * from tb_brand where id=#{id}")
   @ResultMap("brandResultMap")
   Brand selectById(int id);

    /**
     * 删除单条数据
     * @param id
     */
    @Delete("delete from tb_brand where id=#{id}")
   void deleteById(int id);

}

业务层接口(降低耦合度)

package cn.itaxu.service;

import cn.itaxu.pojo.Brand;
import cn.itaxu.pojo.PageBean;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description: cn.itaxu.service
 * @author: Axu
 * @date:2022/11/6 22:45
 */
public interface BrandService {

    /**
     * 查询所有品牌
     * @return
     */
    List selectAll();

    /**
     * 添加数据
     */
    void add(Brand brand);

    /**
     * 批量删除
     * @param ids
     */
    void deleteByIds(int[] ids);

    /**
     * 分页查询
     * @param currentPage 当前页码
     * @param pageSize 每页显示条数
     * @return
     */
    PageBean selectByPage(int currentPage,int pageSize);

    /**
     * 分页条件查询
     * @param currentPage
     * @param pageSize
     * @param brand
     * @return
     */
    PageBean selectByPageAndCondition(int currentPage,int pageSize,Brand brand);

    /**
     * 回显数据
     * @param id
     * @return
     */
   Brand selectById(int id);

    /**
     * 更新数据
     * @param brand
     */
   void update(Brand brand);

    /**
     * 删除单条数据
     * @param id
     */
   void deleteById(int id);
}

业务层接口实现类 

package cn.itaxu.service.impl;

import cn.itaxu.mapper.BrandMapper;
import cn.itaxu.pojo.Brand;
import cn.itaxu.pojo.PageBean;
import cn.itaxu.service.BrandService;
import cn.itaxu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @Description: cn.itaxu.service.impl
 * @author: Axu
 * @date:2022/11/6 22:45
 */
public class BrandServiceImpl implements BrandService {
   private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有品牌数据
     * @return
     */
    @Override
    public List selectAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List brands = mapper.selectAll();
        sqlSession.close();
        return brands;
    }

    /**
     * 添加数据
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据 ids 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(int[] ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 分页查询
     * @param currentPage 当前页码
     * @param pageSize 每页显示条数
     * @return
     */
    @Override
    public PageBean selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        // 计算开始索引
        int begin = (currentPage-1) * pageSize;
        // 计算条目数
        int size = pageSize;

        // 查询当前页数据
        List rows = mapper.selectByPage(begin, size);

        // 查询总记录数
        int totalCount = mapper.selectTotalCount();

        // 封装pageBean对象
        PageBean pageBean = new PageBean();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        // 释放资源
        sqlSession.close();

        return pageBean;
    }

    /**
     * 条件分页查询
     * @param currentPage
     * @param pageSize
     * @param brand
     * @return
     */
    @Override
    public PageBean selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        // 计算开始索引
        int begin = (currentPage-1) * pageSize;
        // 计算查询条件数
        int size = pageSize;

        // 处理brand条件,模糊表达式
        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+"%");
        }

        // 查询当前页数据
        List rows = mapper.selectByPageAndCondition(begin, size, brand);
        // 查询总记录
        int totalCount = mapper.selectTotalCountByCondition(brand);

        // 封装PageBean对象
        PageBean brandBean = new PageBean();
        brandBean.setRows(rows);
        brandBean.setTotalCount(totalCount);

        // 释放资源
        sqlSession.close();

        return brandBean;
    }

    /**
     * 根据 id 查询
     * @param id
     * @return
     */
    @Override
    public Brand selectById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = mapper.selectById(id);
        sqlSession.close();
        return brand;
    }

    /**
     * 更新数据
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand b = mapper.selectById(brand.getId());
        if (b!=null){
            mapper.update(brand);
        }
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据 id 删除单条数据
     * @param id
     */
    @Override
    public void deleteById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteById(id);
        sqlSession.commit();
        sqlSession.close();
    }
}

 实现复杂sql语句的xml配置文件


    
        
        
        
    
    
        delete from tb_brand where id in
            
                #{id}
            
    

    
    
    

#前端代码

处理中心 后台品牌模型 消息中心 订单管理 查询 批量删除 新增 提交 取消 提交 取消

产品列表

需要引入的外部文件






整体javascript逻辑代码

#后台代码

基类(大大简化了开发效率,不需要创建多个功能类,它是管理所有功能的基类)

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;
/**
 * 替换HttpServlet,根据请求的最后一段路径来进行方法分发
 * @Description: cn.itaxu.web.servlet
 * @author: Axu
 * @date:2022/11/7 9:02
 */
public class BaseServlet extends HttpServlet {

    // 根据请求的最后一段路径来进行方法分发
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取请求路径
        String uri = req.getRequestURI(); //  /brand/selectAll
        // 2.获取最后一段路径
        int index = uri.lastIndexOf("/");
        String methodName = uri.substring(index+1);
        // 3.执行方法
        // 3.1 获取BrandServlet 字节码对象 Class
        // 这里的this 代表 BrandServlet,谁调用我,我代表谁
        Class cls = this.getClass();
        // 3.2 获取方法的Method对象
        try {
            Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 3.3 执行方法
           method.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

业务层的功能实现类

import cn.itaxu.pojo.Brand;
import cn.itaxu.pojo.PageBean;
import cn.itaxu.service.BrandService;
import cn.itaxu.service.impl.BrandServiceImpl;
import com.alibaba.fastjson.JSON;

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;

/**
 * @Description: cn.itaxu.web.servlet
 * @author: Axu
 * @date:2022/11/7 9:06
 */
@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private BrandService service =  new BrandServiceImpl();

    /**
     * 查询所有
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.调用BrandService完成查询
        List brands = service.selectAll();

        // 2.将集合转为JSON数据
        String s = JSON.toJSONString(brands);

        // 3.响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 添加数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        // 1.获取请求体数据
        BufferedReader br = request.getReader();
        String params = br.readLine();

        // 2.将JSON字符串转为Java对象
        Brand brand = JSON.parseObject(params, Brand.class);

        // 3.调用service
        service.add(brand);

        // 4.响应成功标识
        response.getWriter().write("success");
    }

    /**
     * 批量删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // 解决POST请求中文乱码
        request.setCharacterEncoding("UTF-8");

        // 1.接收数据 [1,2,3]
        BufferedReader br = request.getReader();
        String params = br.readLine();

        // 2.将JSON数据转为 int 数组
        int[] ids = JSON.parseObject(params, int[].class);

        // 3.调用service
        service.deleteByIds(ids);

        // 4.响应成功标识
        response.getWriter().write("success");
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
      // 1.接收当前页码 和 每页显示条数  url?currentPage=1&pageSize=5
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        // 2.调用service
        PageBean brandBean = service.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        // 3.将集合转为JSON数据
        String s = JSON.toJSONString(brandBean);

        // 4.响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 解决POST请求乱码问题
        request.setCharacterEncoding("UTF-8");

        // 1.接收当前页码 和 每页显示条数  url?currentPage=1&pageSize=5
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        // 获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();

        // 2.将JSON数据转为Java对象
        Brand brand = JSON.parseObject(params, Brand.class);

        // 3.调用service
        PageBean brandBean = service.selectByPageAndCondition(Integer.parseInt(currentPage),
                Integer.parseInt(pageSize),brand);

        // 4.将集合转为JSON数据
        String s = JSON.toJSONString(brandBean);

        // 5.响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 回显数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 接收表单的id
        BufferedReader br = request.getReader();
        String _id = br.readLine();

        // 将JSON数据转为int数据
        Integer id = JSON.parseObject(_id, int.class);

        // 调用service
        Brand brand = service.selectById(id);

        // 将Java对象转为JSON数据
        String s = JSON.toJSONString(brand);

        // 响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 更新数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 解决POST请求中文乱码问题
        request.setCharacterEncoding("UTF-8");
        // 接收请求体数据
        BufferedReader br = request.getReader();
        String s = br.readLine();

        // 把JSON数据转为Java对象
        Brand brand = JSON.parseObject(s, Brand.class);

        // 调用service
        service.update(brand);

        // 响应成功标识
        response.getWriter().write("success");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决POST请求中文乱码
        request.setCharacterEncoding("UTF-8");

        // 1.接收请求体数据
        BufferedReader br = request.getReader();
        String params = br.readLine();

        // 2.将JSON数据转为 int 数据
        int id = JSON.parseObject(params, int.class);

        // 3.调用service
        service.deleteById(id);

        // 4.响应成功标识
        response.getWriter().write("success");
    }

}

你可能感兴趣的:(java,开发语言)