1.新建项目工程
2.添加依赖
3.加载依赖
4.因为mybatis需要一个数据源,所以配置一个application.yml
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/easyproject?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.model
server:
port: 9000
启动项目:http://localhost:9000
5.创建测试路由路径
编写测试Controller,注意返回值和路径映射
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
return "This is ok.";
}
}
测试结果: 6.跨域配置
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8080", "null")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
}
|