创建springboot的四种方法


  1. Rest:一种资源描述风格

    书写简化,隐藏真实的访问名称,使用复数描述模块

    restful:按这种风格访问资源

    常规风格:http://localhost/users/getById?id=1
    Rest风格:http://localhost/users/1

    四个动作:

  2. 查询单个、多个:get

  3. 新增:post
  4. 修改:put
  5. 删除:delete

注解

@RequestBody 接收json格式参数

  1. @Controller
  2. public class UserController {
  3. //新增
  4. @RequestMapping(value = "/users",method = RequestMethod.POST)
  5. @ResponseBody
  6. public String save(@RequestBody User user) {
  7. System.out.println("user save..." + user);
  8. return "{'module':'user save'}";
  9. }
  10. //修改
  11. @RequestMapping(value = "/users",method = RequestMethod.PUT)
  12. @ResponseBody
  13. public String update(@RequestBody User user){
  14. System.out.println("user update..."+user);
  15. return "{'module':'user update'}";
  16. }
  17. }

@PathVariable 接收单个或较少参数

  1. @Controller
  2. public class BookController {
  3. //删除
  4. @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
  5. @ResponseBody
  6. public String delete(@PathVariable Integer id){
  7. System.out.println("user delete..."+id);
  8. return "{'module':'user delete'}";
  9. }
  10. }

@RequestParam 接收非json格式参数,使用较少

精简注解

  1. //@Controller+@ResponseBody=@RestController
  2. @RestController
  3. @RequestMapping(value = "/users")
  4. public class UserController {
  5. // @RequestMapping(method = RequestMethod.POST)=@PostMapping
  6. @PostMapping
  7. public String save(@RequestBody User user) {
  8. System.out.println("user save..." + user);
  9. return "{'module':'user save'}";
  10. }
  11. }

读取配置文件

  1. @RestController
  2. @RequestMapping("/run")
  3. public class TestController {
  4. @Value("${driver1}")
  5. private String driver;
  6. @GetMapping
  7. public String run() {
  8. System.out.println(driver);
  9. return "running...";
  10. }
  11. }
  1. @RestController
  2. @RequestMapping("/run")
  3. public class RunController {
  4. @Resource
  5. private Environment env;
  6. @GetMapping
  7. public String run() {
  8. System.out.println(env.getProperty("server.port"));
  9. return "running...";
  10. }
  11. }
  1. //控制器
  2. @RestController
  3. @RequestMapping("/run")
  4. public class RunController {
  5. @Resource
  6. private Run run;
  7. @GetMapping
  8. public String run() {
  9. System.out.println(run);
  10. return "running...";
  11. }
  12. }
  13. //对象类
  14. @Component
  15. @Data
  16. //匹配yml的数据头
  17. @ConfigurationProperties(prefix = "spring.datasource")
  18. public class Run {
  19. private String driver;
  20. private String url;
  21. private String username;
  22. private String password;
  23. }

junit

SpringBoot测试类所在包的级别中如果高于启动类所在包,就需要手动添加启动类的类文件
@SpringBootTest(classes = DemoApplication.class)
也可以写为
@SpringBootTest<br />@ContextConfiguration(classes = DemoApplication.class)

整合Mybatis

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.5.1</version>
  5. </dependency>

MyBatis Plus提供注解方法

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.5.1</version>
  5. </dependency>
  1. //指定查询的表
  2. @TableName("Users")
  3. public interface UserMapper extends BaseMapper<User> { //mybatisPlus提供的方法,不需要自己写增删改查
  4. }

Druid 监控sql语句

  1. <groupId>com.alibaba</groupId>
  2. <artifactId>druid-spring-boot-starter</artifactId>
  3. <version>1.2.8</version>
  1. # 第一种
  2. spring:
  3. datasource:
  4. druid:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/woniuxy?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
  7. username: root
  8. password: 123
  9. # 第二种
  10. spring:
  11. datasource:
  12. driver-class-name: com.mysql.jdbc.Driver
  13. url: jdbc:mysql://localhost:3306/woniuxy?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
  14. username: root
  15. password: 123
  16. type: com.alibaba.druid.pool.DruidDataSource

yml中MyBatis相关配置

  1. # MyBatis相关配置
  2. mybatis:
  3. # 加载的MyBatis配置文件路径,对应:resources/mappers
  4. mapper-locations: /mappers/*.xml
  5. # 别名配置,加上后配置文件中可以不用写类的全路径,直接写类名即可。简化配置
  6. type-aliases-package: com.wnxy.pojo
  7. configuration:
  8. # 数据库中列下划线自动转驼峰
  9. map-underscore-to-camel-case: true
  10. #标准方式输出运行日志
  11. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

MyBatis Plus分页

配置拦截器

  1. @Configuration
  2. public class MPConfig {
  3. @Bean
  4. public MybatisPlusInterceptor mybatisPlusInterceptor(){
  5. MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor() ;
  6. interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
  7. return interceptor;
  8. }
  9. }

调用分页方法

  1. @RestController
  2. @RequestMapping("run")
  3. public class RunController {
  4. @Resource
  5. private UsersMapper usersMapper;
  6. @GetMapping
  7. public void main(){
  8. Page page=new Page(1,2) ;
  9. usersMapper.selectPage(page,null);
  10. }
  11. }

拦截器

项目打包部署

profile 配置

properties 配置环境

  1. 三种环境: ```properties

    开发

    application-dev.properties 配置端口:server.port=8081 application-test.properties 配置端口:server.port=8082 application-pro.properties 配置端口:server.port=8083
  1. 1. `application-dev.properties`
  2. 1. `application-test.properties`
  3. 1. `application-pro.properties`
  4. 2. 激活环境:在在application.xml中配置
  5. ```yaml
  6. # properties和yml同时存在时,优先使用properties
  7. # 开发环境
  8. application-dev.properties
  9. 配置内容:server.port=8081
  10. # 测试环境
  11. application-test.properties
  12. 配置内容:server.port=8082
  13. # 开发环境
  14. application-pro.properties
  15. 配置内容:server.port=8083
  1. # 激活application-test.properties
  2. spring.profiles.active=test

yml 配置环境