1.根据需求,分析数据库、建库、建表、准备数据
2.建立web模块 webapp类型的maven项目
maven默认路径要与实际路径一致,否则添加依赖时会出现错误
3.手动创建是src{(蓝)、resource(橙)、test(绿)目录
4.建立package,entity dao service controller
5.pom依赖 : web模块依赖 webmvc模块依赖 jackson相关依赖
<jackson.version>2.9.8</jackson.version>
<jackson-mapper.version>1.9.13</jackson-mapper.version>
<!--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>
<!--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>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5-20081211</version>
</dependency>
6.entity实体类
7.dao接口,增加自定义的复杂关联查询(注解)
8.service接口,注入dao,调用相应方法
9.用JUnit对service做单元测试
10.controller,使用RESTful风格请求,完成控制层
11.用postman对controller进行测试,杜绝一切404,500
github具体代码
web.xml也需调试代码
tomcat配制
选中Module按F4
将Name改为web。
运行test,然后打开postman
运行结果在下方。
调试前端:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js-访问API接口数据-蓝墨云班课练习</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 通过CDN引入axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<style type="text/css">
body {
background-color: white;
}
.top {
width: 80%;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
hr{
width: 80%;
color: #efefef;
}
.container {
width: 80%;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
padding-left: 20px;
padding-top: 10px;
}
.container2 {
width: 80%;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
padding-left: 20px;
padding-top: 10px;
}
.card {
width: 190px;
display: flex;
flex-direction: column;
border: 1px solid #eee;
margin-right: 20px;
margin-bottom: 10px;
background-color: #fff;
justify-content: center;
padding-left: 5px;
padding-right: 5px;
padding-top: 5px;
border-radius: 20px;
}
.card img {
width: 100%;
}
p {
font-size: 12px;
margin-bottom: 5px;
}
.teacher {
display: flex;
justify-content: space-between;
}
.left {
display: flex;
}
.avatar img {
width: 30px;
height: 30px;
border-radius: 50%;
margin-right: 5px;
}
.code {
color: #00BBDD;
margin-top: 5px;
}
</style>
</head>
<body>
<div id="app">
<div class="top">
<p>进行中的班课</p>
<p>{{courses.length}}个进行中的班课</p>
</div>
<hr>
<div class="container">
<div class="card" v-for="(course, index) in courses" :key="index">
<div class="cover">
<img :src="'img/'+course.cover">
</div>
<div class="course-class">
<p>{{course.courseClass}}</p>
</div>
<div class="course-name">
<p>{{course.courseName}}</p>
</div>
<div class="teacher">
<div class="left">
<div class="avatar">
<img :src="'img/'+course.avatar">
</div>
<div class="username">
<p class="code">{{course.username}}</p>
</div>
</div>
<div class="course-code">
<p class="code" v-if="loginUserId === course.userId">{{course.courseCode}}</p>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
loginUserId: 1,
courses: []
},
created: function() {
var _this = this;
axios.get('http://127.0.0.1:8080/web/courses')
.then(function(response) {
console.log(response.data);
_this.courses = response.data;
})
}
})
</script>
</body>
</html>
效果如图:
这是一个动态结果 在数据库中修改数据可以直接反应到前端。