a. 首先在pom文件中加上依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
b. 然后在配置文件application.yml文件中开启热部署:
spring:
devtools:
restart:
enabled: true
additional-paths: src/main/java
exclude: static/**
c. 开启idea相关配置:
a. 配置映射资源目录—web端直接访问
spring:
servlet:
multipart:
max-file-size: 10MB
web:
resources:
static-locations: /upload/
b. 撰写代码
@RestController
public class FileUploadController {
@PostMapping("/upload")
public String up(String nickname, MultipartFile photo, HttpServletRequest request) throws IOException {
System.out.println(nickname);
System.out.println(photo.getOriginalFilename());
System.out.println(photo.getContentType());
String path= request.getServletContext().getRealPath("/upload/");
System.out.println(path);
saveFile(photo,path);
return "上传成功!";
}
public void saveFile(MultipartFile photo, String path) throws IOException {
File dir= new File(path);
if(!dir.exists()){
dir.mkdir();
}
File file = new File(path+photo.getOriginalFilename());
photo.transferTo(file);
}
}
c. postman测试
a:一般只设置preHandle,作为执行controller之间的拦截器,先撰写拦截器类
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(!(handler instanceof HandlerMethod)){
return true;
}
System.out.println("LoginInterceptor");
return true;
}
}
b. 撰写配置类:绑定拦截器和拦截的资源访问路径,切记要加@Controller,才能被整个程序配置到
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");
}
}
a. pom文件中导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
b. 撰写Swagger配置类
@Configuration //告诉Spring容器这是一个配置类
@EnableSwagger2 //开启Swagger的功能
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com"))
.paths(PathSelectors.any()).build();
}
/**
* 这里为API文档显示的信息
*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder().title("演示项目API")
.description("学习Swagger2的演示项目")
.build();
}
}
c. SpringBoot2.6以上版本与Swagger有冲突,需要在application.yml中添加配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
a. 撰写MybatisPlus拦截器,将page拦截器加入MybatisPlus拦截器中
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
b. 分页功能使用:传入pageNum和pageSize,需要有条件查询再撰写相应的wrapper写入null处即可
@Override
public Page<User> selectUserList(Integer pageNum, Integer pageSize){
Page page=new
Page(pageNum,pageSize);
userMapper.selectPage(page,null);
return page;
}
利用Vue Cli脚手架去创建vue框架
a. 在后台控制器中开启跨域访问:在控制器上方添加@CrossOrigin
b. 在main.js中写入
先执行npm install axios
import axios from "axios";
axios.defaults.baseURL="http://localhost:8088"
Vue.prototype.$http=axios
c. 在具体的vue的methods中使用可以将axios变成this.$http,后面接口URL不再需要baseURL
用=>的原因是=>函数可以继承父类的this,避免还需要保存父类this指针
created() {
this.$http.get("/user/getAll").then((res)=>{
console.log(res.data);
this.tableData = res.data;
})
}
this.$http.post("/brands",this.addNewBrand).then((res)=>{
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20011){
this.dialogVisible = false;
this.$message.success("添加成功");
}else if(res.data.code == 20010){
this.$message.error("添加失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.addNewBrand={};
this.getAll();
});
a. npm下载element-ui
在CMD中切入该项目中,运行指令下载组件
npm i element-ui -S
b. 在main.js中导入并全局注册,之后在具体的vue文件中直接使用即可
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI); //全局注册
a. 在components文件下创建组件:组件名.vue:包含template、script、style三部分内容
<template>
<div>
<h1>{{title}}</h1>
<span>{{rating}}</span>
<button @click="fun">点击收藏</button>
</div>
</template>
<script>
export default {
name:"Hello",
props:["title","rating"],//相当于参数,谁使用这个组件都可以传递这个参数
data:function (){
return{
}
},
methods:{
fun(){
alert("收藏成功")
}
}
}
</script>
<style>
</style>
**b. 在其他Vue文件中使用Movie组件 **
(1)在Script中导入:
import Movie from "@/components/Movie.vue";
在method下方components内部写上组件注册
components:{
Movie //做一个注册才能够在上面template中使用该组件
}
当使用Movie组件时,在上方<template></template>中写入
<Movie v-for="movie in movies" :key="movie.id" :title="movie.title" :rating="movie.rating"></Movie>
data中加入数据:
movies:[
{id:1,title:"金刚狼1",rating:8.7},
{id:2,title:"金刚狼2",rating:8.8},
{id:3,title:"金刚狼3",rating:8.6}
]
<template>
<div id="app">
<!--声明路由链接-->
<router-link to="/discover">发现音乐</router-link>
<router-link to="/friends">关注</router-link>
<router-link to="/my">我的音乐</router-link>
<!--声明路由占位标签-->
<router-view></router-view>
</div>
</template>
import Vue from 'vue'
import VueRouter from 'vue-router'
import Discover from "@/components/Discover.vue";
import Friends from "@/components/Friends.vue";
import My from "@/components/My.vue";
//将VueRouter设置为Vue的插件
Vue.use(VueRouter)
const routes = [
{
path:"/", //表示首页会直接跳转至/discover页面
redirect:"/discover"
},
{
path: '/discover',
component:Discover
},
{
path: '/friends',
component:Friends
},
{
path: '/my',
component:My
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes //加进去
})
export default router
import router from './router'
new Vue({
router, //添加router
render: h => h(App)
}).$mount('#app')
<template>
<div>
<!--声明路由链接-->
<h1>发现音乐</h1>
<router-link to="/discover/playlist">歌单</router-link>
<router-link to="/discover/toplist">推荐</router-link>
<hr>
<!--声明路由占位标签-->
<router-view></router-view>
</div>
</template>
在组件Discover 中嵌套TopList和PlayList组件,可以直接写在Discover的children中
{
path: '/discover',
component:Discover,
children:[
{
path: "toplist",
component:TopList
},
{
path: "playlist",
component: PlayList
}
]
}
<template>
<div>
<h1>我的音乐</h1>
<router-link to="/my/1">商品1</router-link>
<router-link to="/my/2">商品2</router-link>
<router-link to="/my/3">商品3</router-link>
<hr>
<router-view></router-view>
</div>
</template>
<template>
<div>
<h3>商品{{id}}</h3>
</div>
</template>
<script>
export default {
props:["id"]
}
</script>
{
path: '/my',
component:My,
children: [
{
path:':id',
component:Product,
props:true
}
]
}
导航守卫(和后端的拦截器相同作用,比如没登录想要加购物车等行为会被拒绝,然后跳转至登录页面)
程序内部跳转页面
this.$router.push("/user/register")