美文网首页
11、前后端分离开发——后台内容

11、前后端分离开发——后台内容

作者: youi_e050 | 来源:发表于2019-03-17 20:34 被阅读0次

首先我们根据需求分析数据库,建库建表准备数据

我们这次仿照电脑端的蓝墨云的页面来写 大概就是这样:


蓝墨云页面.png

数据库的两个表 一个是使用者的表 另一个是课程表


User.png course.png

里面的内容自己写,注释后面有解释

建完表我们开始在IDEA中建模块,webapp类型的maven项目
建立相应的package entity dao service controller
Pom依赖 web模块 webmvc模块 ,Jackson相关依赖


image.png

entity实体类

sysUser类
package com.spring.web.entity;

import lombok.Data;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "t_sys_user")
@Data
public class SysUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Long userId;
    private String mobile;
    private String password;
    private String username;
    private String avatar;

}

Course类

package com.spring.web.entity;


import lombok.Data;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name="t_course")
@Data
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Long courseId;
    private String courseName;
    private Long userId;
    private String courseClass;
    private String cover;
    private String courseCode;
    private Short finished;

}

CourseVo类

package com.spring.web.entity;

import lombok.Data;

@Data
public class CourseVO {
    private Long courseId;
    private String courseName;
    private Long userId;
    private String courseClass;
    private String cover;
    private String courseCode;
    private Short finished;
    private String username;
    private String avatar;
}

dao接口 增加自定义的复杂关联查询(注解)

BaseDAO接口

package com.spring.web.dao;


import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 通用DAO接口
 */
public interface BaseDAO<T>  extends Mapper<T>, MySqlMapper<T> {
}

CourseDAO接口

package com.spring.web.dao;

import com.spring.web.entity.Course;
import com.spring.web.entity.CourseVO;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CourseDAO extends BaseDAO<Course> {
    //自定义的多表关联查询
    @Results({@Result(column = "course_id", property = "courseId"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "course_class", property = "courseClass"),
            @Result(column = "cover", property = "cover"),
            @Result(column = "course_code", property = "courseCode"),
            @Result(column = "finished", property = "finished"),
            @Result(column = "username", property = "username"),
            @Result(column = "avatar", property = "avatar")
    })
    @Select("SELECT a.*,b.username,b.avatar FROM t_course a Left JOIN t_sys_user b ON a.user_id=b.user_id WHERE a.finished = 0  ")
    List<CourseVO> selectCurrentCourses();


    //自定义的多表关联查询
    @Results({@Result(column = "course_id", property = "courseId"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "course_class", property = "courseClass"),
            @Result(column = "cover", property = "cover"),
            @Result(column = "course_code", property = "courseCode"),
            @Result(column = "finished", property = "finished"),
            @Result(column = "username", property = "username"),
            @Result(column = "avatar", property = "avatar")
    })

    @Select("SELECT a.*,b.username,b.avatar FROM t_course a Left JOIN t_sys_user b ON a.user_id=b.user_id WHERE a.finished = 1  ")
    List<CourseVO> selectCurrentCourses1();

}

SysUserDAO接口

package com.spring.web.dao;

import com.spring.web.entity.SysUser;

public interface SysUserDAO extends  BaseDAO<SysUser>{
}

service接口,注入dao 调用相应方法

CourseService接口

package com.spring.web.service;

import com.spring.web.entity.CourseVO;

import java.util.List;

public interface CourseService {
    List<CourseVO> selectCurrentCourses();

    List<CourseVO> selectCurrentCourses1();
}

CourseServiceImpl类

package com.spring.web.service.impl;

import com.spring.web.dao.CourseDAO;
import com.spring.web.entity.CourseVO;
import com.spring.web.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDAO courseDAO;


    @Override
    public List<CourseVO> selectCurrentCourses() {
        return courseDAO.selectCurrentCourses();
    }
    @Override
    public List<CourseVO> selectCurrentCourses1() {
        return courseDAO.selectCurrentCourses1();
    }

}

对service进行单元测试,用junit

CourseServiceImplTest类

package com.spring.web.service.impl;

import com.spring.web.entity.CourseVO;
import com.spring.web.service.CourseService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;




@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring_mybatis.xml"})
public class CourseServiceImplTest {
    @Autowired
    private CourseService courseService;
    @Test
    public void selectCurrentCourses() {
        List<CourseVO> courseVOList = courseService.selectCurrentCourses();
        courseVOList.forEach(courseVO -> System.out.println(courseVO));
    }
    @Test
    public void selectCurrentCourses1() {
        List<CourseVO> courseVOList = courseService.selectCurrentCourses1();
        courseVOList.forEach(courseVO -> System.out.println(courseVO));
    }

}

CORSFilter类

package com.spring.web.util;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CORSFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("work");
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

ResponseUtil类

package com.spring.web.util;

public class ResponseUtil {
    private int code;
    private String message;
    private Object data;


    public ResponseUtil(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseUtil(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResponseUtil{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}

测试结果:


image.png

controller,使用RESTFUL风格请求 完成控制层

CourseController类

package com.spring.web.controller;

import com.spring.web.entity.CourseVO;
import com.spring.web.service.CourseService;
import com.spring.web.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "courses", method = RequestMethod.GET)
    public List<CourseVO> selectCourses() {
        List<CourseVO> courseVOList = courseService.selectCurrentCourses();
        return courseVOList;
    }
    @RequestMapping(value = "courses1", method = RequestMethod.GET)
    public List<CourseVO> selectCourses1() {
        List<CourseVO> courseVOList = courseService.selectCurrentCourses1();
        return courseVOList;
    }
}

对controller进行测试,杜绝一切404和500 用postman进行接口测试

如图:


image.png

当出来后就可以写前端了,稍后我会将前端内容放在web前端框架里面。

相关文章

  • 11、前后端分离开发——后台内容

    首先我们根据需求分析数据库,建库建表准备数据 我们这次仿照电脑端的蓝墨云的页面来写 大概就是这样: 数据库的两个表...

  • Java接口开发经验之谈

    前言:现在开发中常使用前后端分离,后台开发使用springMVC框架进行后台接口的开发。经过最近的开发对接口开发有...

  • AJAX数据模拟之Mock

    出现的原因 1、开发进度的不同 2、前后端分离 优势 1、前后端分离—–定义好接口文档之后,前端人员不用再等待后台...

  • mock

    出现的原因 1、开发进度的不同2、前后端分离 优势 1、前后端分离—–定义好接口文档之后,前端人员不用再等待后台的...

  • mock

    出现的原因 1、开发进度的不同2、前后端分离 优势 1、前后端分离—–定义好接口文档之后,前端人员不用再等待后台的...

  • 前后端分离

    1、目前都采取的是前、后端分离的开发模式 2、前端专注样式、展示效果 3、后台专注业务逻辑、数据管理、并发性、负载...

  • 0.django_rest项目功能

    django前后端分离 vue前端项目 xadmin后台系统 rest api开发 通用View实现rest ap...

  • Springboot中跨域问题解决

    需求: 在项目开发中,前后端分离,接口联调时候,前端页面直接调取后台接口,前后端开发使用不同的电脑,因此页面的aj...

  • 07-Flask之前后端分离

    一、后台服务器开发 服务器就是提供服务,提供数据的。 二、前端开发 三、前后端分离 爱鲜蜂(轮播图)示例 【后台人...

  • 07-Flask之前后端分离

    一、后台服务器开发 服务器就是提供服务,提供数据的。 二、前端开发 三、前后端分离 爱鲜蜂(轮播图)示例 【后台人...

网友评论

      本文标题:11、前后端分离开发——后台内容

      本文链接:https://www.haomeiwen.com/subject/jnbumqtx.html