栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自学Java day35 电商后台管理系统-前后端分离 从jvav到架构师

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自学Java day35 电商后台管理系统-前后端分离 从jvav到架构师

前端:html+css+jvavscript vue框架 + ajax + axios + element ui

后端:jvav + mybatis框架 + mysql数据库

项目管理工具:maven

服务器:Tomcat

相关功能演示

首先启动Tomcat服务器

进入页面

调整每页显示数量:

页面跳转:

添加数据:

删除数据:

条件查询:

条件查询下的页面切换:

前端源码: 




    
    Title

    





    
    
        
            
                
                
                
            
        
        
            
        
        
            
        
        
            查询
        
    

    删除
    添加

    
    
        
        
            
                
            

            
                
            

            
                
            

            
                
            

            
                
            

            
                立即创建
                取消
            

        

    

    
    

    
    
    











后端源码(部分):

Dao层

mapper(实现对数据库的操作)

package com.company.web.mapper;

import com.company.web.pojo.Brand;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BrandMapper {
    @Select("select * from brand")
    List select();

    @Insert("insert into brand values(null,#{name },#{company},#{ordered},#{description},#{status})")
    void add(Brand brand);

    //@Delete("delete from brand where ??????")
    void del(@Param("ids") int[] ids);

    @Select("select * from brand limit #{begin},#{size}")
    List selectByPage(@Param("begin") int begin, @Param("size") int size);

    @Select("select count(*) from brand")
    int selectCount();

    //List selectx(@Param("status")int status,@Param("name") String name,@Param("company") String company,@Param("begin")int begin, @Param("size")int size);
    List selectx(@Param("brand")Brand brand, @Param("begin") int begin, @Param("size") int size);

    //@Select("select count(*) from brand where ???")
    int selectCountx(Brand brand);
}

mapper的xml配置






    


    
        delete
        from brand
        where id in
        
            #{id}
        
    

    
        select count(*)
        from brand
        
            
                and status = #{status}
            
            
                and name like #{name}
            
            
                and company like #{company}
            
        
    


service层(封装操作数据库的方法)

package com.company.web.server;

import com.company.web.pojo.Brand;
import com.company.web.mapper.BrandMapper;
import com.company.web.pojo.PageBean;
import com.company.web.servlet.SSF;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServer {
    public List select() {
        //1.加载mybatis核心配置文件
        SqlSessionFactory sqlSessionFactory = SSF.factory();

        //2.获取SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List list = brandMapper.select();
        sqlSession.close();
        return list;
    }

    public PageBean selectx(Brand brand, int currentPage, int pageSize) {
        //1.加载mybatis核心配置文件
        SqlSessionFactory sqlSessionFactory = SSF.factory();

        //2.获取SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        String name = brand.getName();
        if (name != null && name.length() > 0) {
            brand.setName("%" + name + "%");
        }

        String company = brand.getCompany();
        if (company != null && company.length() > 0) {
            brand.setCompany("%" + company + "%");
        }

        if (brand.getStatus() == 0) {
            brand.setStatus(null);
        }

        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;

        List list = brandMapper.selectx(brand, begin, size);
        int countx = brandMapper.selectCountx(brand);

        PageBean pageBean = new PageBean(list, countx);
        sqlSession.close();
        return pageBean;
    }

    public PageBean selectByPage(int currentPage, int pageSize) {
        //1.加载mybatis核心配置文件
        SqlSessionFactory sqlSessionFactory = SSF.factory();

        //2.获取SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;

        int count = brandMapper.selectCount();
        List list = brandMapper.selectByPage(begin, size);

        PageBean pageBean = new PageBean(list, count);

        sqlSession.close();

        return pageBean;
    }

    public void add(Brand brand) {
        //1.加载mybatis核心配置文件
        SqlSessionFactory sqlSessionFactory = SSF.factory();

        //2.获取SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    public void del(int[] ids) {
        //1.加载mybatis核心配置文件
        SqlSessionFactory sqlSessionFactory = SSF.factory();

        //2.获取SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.del(ids);
        sqlSession.commit();
        sqlSession.close();
    }

}

 servlet层(响应请求,调用service的方法)

package com.company.web.servlet;

import com.alibaba.fastjson.JSON;
import com.company.web.pojo.Brand;
import com.company.web.pojo.PageBean;
import com.company.web.server.BrandServer;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;


@WebServlet(urlPatterns = {"/selectxServlet"})
public class SelectxServlet extends HttpServlet {
    BrandServer brandServer = new BrandServer();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String _CurrentPage = req.getParameter("currentPage");
        String _PageSize= req.getParameter("pageSize");

        int currentPage = Integer.parseInt(_CurrentPage);
        int pageSize = Integer.parseInt(_PageSize);

        BufferedReader reader = req.getReader();

        String json = reader.readLine();
        Brand brand = JSON.parseObject(json,Brand.class);

        PageBean pageBean = brandServer.selectx(brand,currentPage,pageSize);
        json = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charset=utf8");
        resp.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        doGet(req, resp);
    }
}

至此,已完成对jvav se 、jvav sql、jvav web的学习,下面将开启spring篇

完整源码过于啰嗦,这里就不放了,有需要者欢迎评论/私信获取

世界线回溯,从jvav到架构师

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/852260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号