创建springboot的四种方法
-
Rest:一种资源描述风格
restful:按这种风格访问资源
常规风格:
http://localhost/users/getById?id=1
Rest风格:http://localhost/users/1
四个动作:
查询单个、多个:get
- 新增:post
- 修改:put
- 删除:delete
注解
@RequestBody 接收json格式参数
@Controller
public class UserController {
//新增
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String save(@RequestBody User user) {
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
//修改
@RequestMapping(value = "/users",method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user){
System.out.println("user update..."+user);
return "{'module':'user update'}";
}
}
@PathVariable 接收单个或较少参数
@Controller
public class BookController {
//删除
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete..."+id);
return "{'module':'user delete'}";
}
}
@RequestParam 接收非json格式参数,使用较少
精简注解
//@Controller+@ResponseBody=@RestController
@RestController
@RequestMapping(value = "/users")
public class UserController {
// @RequestMapping(method = RequestMethod.POST)=@PostMapping
@PostMapping
public String save(@RequestBody User user) {
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
}
读取配置文件
@RestController
@RequestMapping("/run")
public class TestController {
@Value("${driver1}")
private String driver;
@GetMapping
public String run() {
System.out.println(driver);
return "running...";
}
}
@RestController
@RequestMapping("/run")
public class RunController {
@Resource
private Environment env;
@GetMapping
public String run() {
System.out.println(env.getProperty("server.port"));
return "running...";
}
}
//控制器
@RestController
@RequestMapping("/run")
public class RunController {
@Resource
private Run run;
@GetMapping
public String run() {
System.out.println(run);
return "running...";
}
}
//对象类
@Component
@Data
//匹配yml的数据头
@ConfigurationProperties(prefix = "spring.datasource")
public class Run {
private String driver;
private String url;
private String username;
private String password;
}
junit
SpringBoot测试类所在包的级别中如果高于启动类所在包,就需要手动添加启动类的类文件@SpringBootTest(classes = DemoApplication.class)
也可以写为@SpringBootTest<br />@ContextConfiguration(classes = DemoApplication.class)
整合Mybatis
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
MyBatis Plus提供注解方法
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
//指定查询的表
@TableName("Users")
public interface UserMapper extends BaseMapper<User> { //mybatisPlus提供的方法,不需要自己写增删改查
}
Druid 监控sql语句
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
# 第一种
spring:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/woniuxy?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 123
# 第二种
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/woniuxy?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
yml中MyBatis相关配置
# MyBatis相关配置
mybatis:
# 加载的MyBatis配置文件路径,对应:resources/mappers
mapper-locations: /mappers/*.xml
# 别名配置,加上后配置文件中可以不用写类的全路径,直接写类名即可。简化配置
type-aliases-package: com.wnxy.pojo
configuration:
# 数据库中列下划线自动转驼峰
map-underscore-to-camel-case: true
#标准方式输出运行日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
MyBatis Plus分页
配置拦截器
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor() ;
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
调用分页方法
@RestController
@RequestMapping("run")
public class RunController {
@Resource
private UsersMapper usersMapper;
@GetMapping
public void main(){
Page page=new Page(1,2) ;
usersMapper.selectPage(page,null);
}
}
拦截器
项目打包部署
profile 配置
properties 配置环境
- 三种环境:
```properties
开发
application-dev.properties 配置端口:server.port=8081 application-test.properties 配置端口:server.port=8082 application-pro.properties 配置端口:server.port=8083
1. `application-dev.properties`
1. `application-test.properties`
1. `application-pro.properties`
2. 激活环境:在在application.xml中配置
```yaml
# properties和yml同时存在时,优先使用properties
# 开发环境
application-dev.properties
配置内容:server.port=8081
# 测试环境
application-test.properties
配置内容:server.port=8082
# 开发环境
application-pro.properties
配置内容:server.port=8083
# 激活application-test.properties
spring.profiles.active=test