14.前后端分离开发

后端开发

后端开发的步骤:

  • 根据需求,分析数据库建库、建表、准备数据(主键类型应是包装类,bigint)
  • 建立web模块web类型的maven项目
  • 手动创建src、resources、test-java目录
  • 建立package、entity、dao、service、controller
  • pom依赖,web模块依赖,webmvc,jackson依赖
  • entity实体类
  • dao接口,增加自定义的复杂关联查询(注解)
  • service接口注入dao,调用相应方法
  • 对service做单元测试(junit)
  • controller,使用RESTful风格请求,完成控制层
  • 对controller进行测试(使用postman,get查,put修改,post插,delete删)VO(试图对象)

新建一个模块web

  • image.png

依赖

  • pom依赖



    4.0.0

    com.spring
    web
    1.0-SNAPSHOT
    war

    web Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
        5.1.5.RELEASE
        1.9.2
        4.12
        1.2.17
        1.7.12
        5.1.47
        3.5.0
        2.0.0
        4.1.5
        1.1.14
        1.18.6
        2.9.8
        1.9.13
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        

        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        

        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        

        
        
            org.mybatis
            mybatis-spring
            ${mybatis-spring.version}
        

        
        
            tk.mybatis
            mapper
            ${tk-mybatis.version}
        

        
        
            com.alibaba
            druid
            ${druid.version}
        

        
        
            org.projectlombok
            lombok
            ${lombok.version}
            provided
        

        
        
            junit
            junit
            ${junit.version}
            test
        
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            ${jackson-mapper.version}
        
        
            javax.servlet
            servlet-api
            2.5
        
    

    
        web
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    ${project.build.sourceEncoding}
                
            
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.1.1
                
                    web
                
            
        
        
        
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                true
            
        
    

  • web依赖


    web
    
        index.html
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        contextConfigLocation
        classpath:Spring-mybatis.xml
    

    
    
        Spring character encoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        Spring character encoding
        /*
    

    
    
        springMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMvc-servlet.xml
        
        1
    

    
    
        springMVC
        /
    

    
    
        CORSFilter
        com.spring.web.util.CORSFilter
    
    
        CORSFilter
        /*
    


  • springMVC-servlet


    
    
    
    
    
    

  • Spring-mybatis



    
    

    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
    

    
    
        
        
        
        
            
                mappers = com.spring.web.dao.BaseDAO
                IDENTITY = MYSQL
            
        
    

    
    
        
    
    
    

建立Course,CourseVO,SysUser类

  • 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;
}
  • 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;
}

建立BaseDAO,CourseDAO,SysUserDAo接口

  • BaseDAO接口
package com.spring.web.dao;

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

/**
 * 通用DAO接口
 */
public interface BaseDAO extends Mapper, MySqlMapper {
}
  • 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 {
    //自定义的多表关联查询
    @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`")
    List selectCurrentCourses();
}
  • SysUserDAO接口
package com.spring.web.dao;

import com.spring.web.entity.SysUser;

public interface SysUserDAO extends BaseDAO {
}

Service类

  • CourseService类
package com.spring.web.service;

import com.spring.web.entity.CourseVO;

import java.util.List;

public interface CourseService {
    List selectCurrentCourses();
}
  • CourseService实现类
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 java.util.List;

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

    @Override
    public List selectCurrentCourses() {
        return courseDAO.selectCurrentCourses();
    }
}

对CourseServiceImpl进行测试

  • 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;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/Spring-mybatis.xml"})
public class CourseServiceImplTest {
    @Autowired
    private CourseService courseService;

    @Test
    public void selectCurrentCourses() {
        List courseVOList = courseService.selectCurrentCourses();
        courseVOList.forEach(courseVO -> System.out.println(courseVO));
    }
}

controller包

  • CourseController类
package com.spring.web.controller;

import com.spring.web.entity.CourseVO;
import com.spring.web.service.CourseService;
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 selectCourses() {
        List courseVOList = courseService.selectCurrentCourses();
        return courseVOList;
    }
}
  • 对controller进行测试
    测试结果


    image.png

util包

  • 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 +
                '}';
    }
}

前端开发

-新建一个html文件



    
        
        Vue.js-访问API接口数据-蓝墨云班课练习
        
        
        
        
        
        
        
    
    
        

进行中的班课

{{count}}个进行中的班课


{{course.courseClass}}

{{course.courseName}}

{{course.username}}

{{course.courseCode}}

已结束的班课的班课

{{courses.length-count}}个已结束的班课


{{course.courseClass}}

{{course.courseName}}

{{course.username}}

{{course.courseCode}}

结果


image.png

你可能感兴趣的:(14.前后端分离开发)