利用前后端Spring 和 View 前后端分离开发技术编写蓝慕云页面
1.根据需求,分析数据库、建库、建表、准备数据
(1) 创建 db_spring 数据库
(2) 建立 t_sys_user 表并添加数据
(3) 创建 t_course 表添加数据
2.建立web模块
-
在 idea 中新建 webapp 类型的 maven 项目
-
分别手动建立 "src" "resources" "test-java" 文件夹
将 "src" 设置为 "Sources Root"
"resources" 设置为 "Resources Root"
"test" 设置为 "Test Sources Root"
(选中文件夹右击 ---> Mark Directory as --->)
-
建立package : entity、dao、service、controller service-impl 如图所示层次:
- 在 pom.xml 中添加依赖(主要添加web模块依赖、webmvc模块依赖、jacksonmi模块依赖)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com,spring</groupId>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>web Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.1.5.RELEASE</spring.version>
<aspectj.version>1.9.2</aspectj.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<slf4j.version>1.7.12</slf4j.version>
<hutool.version>4.5.1</hutool.version>
<mysql.version>5.1.47</mysql.version>
<mybatis.version>3.5.0</mybatis.version>
<mybatis-spring.version>2.0.0</mybatis-spring.version>
<tk-mybatis.version>4.1.5</tk-mybatis.version>
<druid.version>1.1.14</druid.version>
<lombok.version>1.18.6</lombok.version>
<jackson.version>2.9.8</jackson.version>
<jackson-mapper.version>1.9.13</jackson-mapper.version>
</properties>
<dependencies>
<!--spring-aop依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!--aspectj依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!--hutool依赖-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<!--Spring-context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-tx事务依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-web依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<!--spring-test依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mybatis-spring依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--通用mapper依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${tk-mybatis.version}</version>
</dependency>
<!--druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--Lombok 依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- log4j日志依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--jackson相关依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-mapper.version}</version>
</dependency>
</dependencies>
<build>
<finalName>web</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3.编写代码
3.1 entity 实体类
- (1) 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;
}
- (2) 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;
}
- (3) CourseVO 类 ( VO:代表视图对象 )
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;
}
3.2 DAO接口,增加自定义的复杂关联查询(注解)
- (1) 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> {
}
- (2) SysUserDAO
package com.spring.web.dao;
import com.spring.web.entity.SysUser;
public interface SysUserDAO extends BaseDAO<SysUser> {
}
- (3) 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 ")
//WHERE a.finished = 0 LIMIT 0,4
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 ")
//WHERE a.finished = 0 LIMIT 0,4
List<CourseVO> selectCurrentCourses1();
}
- 数据库中t_course表的finished属性:0代表班课正在进行中,1代表班课已结束;
- selectCurrentCourses() 方法是获取所有正在进行中的蓝墨云班课
- selectCurrentCourses1() 方法是获取所有已结束的蓝墨云班课
3.3 service接口,注入dao,调用相应方法
- (1)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();
}
- (3) 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.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();
}
}
3.4 对 CourseServiceImpl 进行单元测试
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<CourseVO> courseVOList = courseService.selectCurrentCourses();
courseVOList.forEach(courseVO -> System.out.println(courseVO));
}
}
- 运行截图
3.5 controller接口,使用RESTful风格请求
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<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;
}
}
4.用postman对Contraller进行测试,杜绝一切404和500(需启动 Tomcat)
POST:数据库增添数据
DELETE:数据库删除数据
PUT:数据库改变数据
GET:数据库查询数据