spring、(SpringConfig)
springMVC、(ServletConfig SpringMvcConfig)
Mybatis、(MybatisConfig JdbcConfig jdbc.properties)
表和实体类
dao(接口+自动代理)
service(接口+实现类):业务层接口测试(整合JUnit)
controller:表现层接口测试(PostMan)
1、pom导入坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
2、创建类
3、 mybatis和spring整合
SpringConfig(导入JdbcConfig、MyBatisConfig)
JdbcConfig(定义数据源,加载properties)
MyBatisConfig(定义SqlSessionFactoryBean,映射配置)
spring整合MVC
功能
-实体类
-dao接口
-service接口及实现类
-业务层
表现层与前端数据传输
package com.wrx.controller;
import com.wrx.domain.Book;
import com.wrx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public Result save(@RequestBody Book book) {
boolean flag = bookService.save(book);
return new Result(flag?Code.SAVE_OK:Code.SAVE_ERROR,flag);
}
@PutMapping
public Result update(@RequestBody Book book) {
boolean flag = bookService.update(book);
return new Result(flag?Code.UPDATE_OK:Code.UPDATE_ERROR,flag);
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
boolean flag = bookService.delete(id);
return new Result(flag?Code.DELETE_OK:Code.DELETE_ERROR,flag);
}
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
Book byId = bookService.getById(id);
Integer code = byId!=null?Code.GET_OK:Code.GET_ERROR;
String msg = byId!=null?"":"查询失败";
return new Result(code,byId,msg);
}
@GetMapping
public Result getAll() {
List<Book> bookList = bookService.getAll();
Integer code = bookList!=null?Code.GET_OK:Code.GET_ERROR;
String msg = bookList!=null?"":"查询失败";
return new Result(code,bookList,msg);
}
}
异常处理
业务异常、系统异常、其他异常
前后台联调
axios.get("/books").then((res)=>{});
axios.post("/books",this.formData).then((res)=>{});
axios.delete("/books/"+row.id).then((res)=>{});
axios.put("/books",this.formData).then((res)=>{});
axios.get("/books/"+row.id).then((res)=>{});
methods: {
//列表
getAll() {
//发送ajax请求
axios.get("/books").then((res)=>{
this.dataList=res.data.data;
})
},
//弹出添加窗口
handleCreate() {
this.dialogFormVisible=true;
this.resetForm();
},
//重置表单
resetForm() {
this.formData = {};
},
//添加
handleAdd () {
axios.post("/books",this.formData).then((res)=>{
//操作成功,关闭弹窗,显示数据;
if(res.data.code==20011){
this.dialogFormVisible=false;
this.$message.success("添加成功");
}else if(res.data.code==20010){
this.$message.error("添加失败");
}else {
this.$message.error(res.data.msg);
}
// 失败
}).finally(()=>{
this.getAll();
});
},
//弹出编辑窗口
handleUpdate(row) {
//根据id查询,弹窗加载数据 row.id
//ajax请求
axios.get("/books/"+row.id).then((res)=>{
if(res.data.code==20041){
this.formData=res.data.data;
this.dialogFormVisible4Edit=true;
}else {
this.$message.error(res.data.msg);
}
});
},
//编辑
handleEdit() {
axios.put("/books",this.formData).then((res)=>{
//操作成功,关闭弹窗,显示数据;
if(res.data.code==20031){
this.dialogFormVisible4Edit=false;
this.$message.success("修改成功");
}else if(res.data.code==20030){
this.$message.error("修改失败");
}else {
this.$message.error(res.data.msg);
}
// 失败
}).finally(()=>{
this.getAll();
});
},
// 删除
handleDelete(row) {
//弹窗、删除、取消删除
this.$confirm("此操作永久删除,是否继续","提示",{
type: 'info'
}).then(()=>{
//删除处理
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.code==20021){
this.$message.success("删除成功");
}else {
this.$message.error("删除失败");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//取消删除
this.$message.info("取消删除");
});
}
}
拦截器
拦截器 | 过滤器 |
SpringMvc技术 | Servelt技术 |
仅针对SpringMvc的访问进行增强 | 对所有访问进行增强 |
执行顺序: preHandle return true: controller postHandle afterCompletion returm false: 结束 |
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterceptor).addPathPatterns("/books");
}
//路径通过可变参数设置多个
}
也可以使用WebMvcConfigurer简化开发
@Configuration
@ComponentScan("com.wrx.controller")
@EnableWebMvc
//注解@EnableWebMvc启用spring mvc的配置
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterceptor).addPathPatterns("/books");
}
}
拦截器链
运行顺序参照拦截器添加顺序为准
拦截器运行中断,仅运行配置在前面的拦截器的afterCompletion操作