电商后台管理系统(基于SSM + Vue + Restful + jquery + axios)

1.项目架构

电商后台管理系统(基于SSM + Vue + Restful + jquery + axios)_第1张图片

2.config配置

JdbcConfig

public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager ds = new DataSourceTransactionManager(dataSource);
        return ds;
    }

}

 MyBatisConfig

public class MyBatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setTypeAliasesPackage("cn.itaxu.domain");
        factoryBean.setDataSource(dataSource);
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("cn.itaxu.dao");
        return msc;
    }

}

ServletConfig

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    // 加载spring配置
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    // 加载springMVC配置
    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    // 设置哪些方法归属springMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    // 处理POST请求中文乱码问题
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8");
        return new Filter[]{filter};
    }

}

SpringConfig

@Configuration
@ComponentScan("cn.itaxu.service")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement
public class SpringConfig {
}

SpringMvcConfig

@Configuration
@ComponentScan({"cn.itaxu.controller","cn.itaxu.config"})
@EnableWebMvc
public class SpringMvcConfig {
}

SpringMvcSupport

@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
    }
}

3.controller控制

BrandController

@RestController
@RequestMapping("/brands")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @PostMapping
    public Result save(@RequestBody Brand brand){
        boolean flag = brandService.save(brand);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        boolean flag = brandService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @DeleteMapping
    public Result deleteMultiply(@RequestBody int[] ids){
        boolean flag = brandService.deleteMultiply(ids);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @PutMapping
    public Result update(@RequestBody Brand brand){
        System.out.println(brand);
        boolean flag = brandService.update(brand);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Brand brand = brandService.getById(id);
        Integer code =brand != null ? Code.GET_OK:Code.GET_ERR;
        String msg = brand != null ? "success":"fail";
        return new Result(code,brand,msg);
    }

    @GetMapping
    public Result getAll(){
        List brandList = brandService.getAll();
        Integer code = brandList != null ? Code.GET_OK:Code.GET_ERR;
        String msg = brandList != null ? "success":"fail";
        return new Result(code,brandList,msg);
    }
    
    @PostMapping("/{currentPage}/{pageSize}")
    public Result selectByPageAndCondition(@PathVariable int currentPage,@PathVariable int pageSize,
                                           @RequestBody Brand brand) {
        PageBean pageBean = brandService.selectByPageAndCondition(currentPage, pageSize, brand);
        Integer code = pageBean != null ? Code.GET_OK:Code.GET_ERR;
        String msg = pageBean != null ? "success":"fail";
        return new Result(code,pageBean,msg);
    }
}

Code 响应状态码

public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;

    public static final Integer SYSTEM_ERR = 50001;
    public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
    public static final Integer SYSTEM_UNKNOW_ERR = 59999;

    public static final Integer BUSINESS_ERR = 60002;
}
ProjectExceptionAdvice 项目异常通知
@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        // 记录日志
        // 发送消息给运维
        // 发送消息给开发人员,ex对象发送给开发人员
        return new Result(ex.getCode(),"null","系统异常");
    }

    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        return new Result(ex.getCode(),"null","业务异常");
    }


    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
        // 记录日志
        // 发送消息给运维
        // 发送消息给开发人员,ex对象发送给开发人员
        return new Result(Code.SYSTEM_UNKNOW_ERR,"null","系统繁忙,请稍后再试!");
    }
}

Result 统一格式数据

public class Result {
    private Object data;
    private Integer code;
    private String  msg;

    public Result() {
    }

    public Result(Integer code,Object data) {
        this.data = data;
        this.code = code;
    }

    public Result(Integer code, Object data, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

4.exception 细化异常

BusinessException 业务异常

public class BusinessException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

}
SystemException 系统异常
public class SystemException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public SystemException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public SystemException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

}

5.service

BrandService接口

@Transactional
public interface BrandService {

    /**
     * 添加数据
     * @param brand
     * @return
     */
    public boolean save(Brand brand);

    /**
     * 根据id删除
     * @param id
     * @return
     */
    public boolean deleteById(Integer id);

    /**
     * 根据ids批量删除
     * @param ids
     * @return
     */
    public boolean deleteMultiply(@Param("ids") int[] ids);

    /**
     * 修改数据
     * @param brand
     * @return
     */
    public boolean update(Brand brand);

    /**
     * 查询单条
     * @param id
     * @return
     */
    public Brand getById(Integer id);

    /**
     * 查询所有
     * @return
     */
    public List getAll();

    /**
     * 分页查询
      * @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);
}

BrandServiceImpl

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandDao brandDao;
    
    @Override
    public boolean save(Brand brand) {
        return brandDao.save(brand) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return brandDao.deleteById(id) > 0;
    }

    @Override
    public boolean deleteMultiply(int[] ids) {
        return brandDao.deleteMultiply(ids) > 0;
    }

    @Override
    public boolean update(Brand brand) {
        return brandDao.update(brand) > 0;
    }

    @Override
    public Brand getById(Integer id) {
        return brandDao.getById(id);
    }

    @Override
    public List getAll() {
        return brandDao.getAll();
    }

    @Override
    public PageBean selectByPage(int currentPage, int pageSize) {
        int begin = (currentPage-1) * pageSize;
        int size = pageSize;
        List rows = brandDao.selectByPage(begin, size);
        int totalCount = brandDao.selectTotalCount();
        PageBean pageBean = new PageBean();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public PageBean selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        int begin = (currentPage-1) * pageSize;
        int size = pageSize;

        // 处理模糊查询
        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 brandList = brandDao.selectByPageAndCondition(begin, size, brand);
        // 查询当前页总记录条数
        int totalCount = brandDao.selectTotalCountByCondition(brand);

        // 封装PageBean
        PageBean pageBean = new PageBean<>(brandList,totalCount);
        return pageBean;
    }

}

6.前端代码

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

产品列表

js







7.页面展示

电商后台管理系统(基于SSM + Vue + Restful + jquery + axios)_第2张图片

 

电商后台管理系统(基于SSM + Vue + Restful + jquery + axios)_第3张图片

 电商后台管理系统(基于SSM + Vue + Restful + jquery + axios)_第4张图片

 电商后台管理系统(基于SSM + Vue + Restful + jquery + axios)_第5张图片

结语:

希望这些能够帮助到你的学习!!! 如果有需要源码的私信我

你可能感兴趣的:(前端,javascript,开发语言)