首先我们根据需求分析数据库,建库建表准备数据
我们这次仿照电脑端的蓝墨云的页面来写 大概就是这样:
蓝墨云页面.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前端框架里面。
网友评论