springboot

狂神springboot博客

第一个程序:完全不需要继承其他接口或者父类
注意要在程序入口application同级目录建controller、pojo。。。

package com.bagdje.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的

  • application.properties

    • 语法结构 :key=value
  • application.yml

    • 语法结构 :key:空格 value

yaml基础语法

说明:语法要求严格!

1、空格不能省略

2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。

3、属性和值的大小写都是十分敏感的。

配置yml和配置properties都可以获取到值 , 强烈推荐 yml;

如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;

如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties,不要犹豫!

对象的值使用yml文件中配置的,需要在resources下,新建application.yml

person:
  id: ${random.uuid}}
  name: chen
  childrens: [老大,老二]
  wifes: [大媳妇,二媳妇]

pojo中使用这个配置文件 注解中使用ConfigurationProperties

@ConfigurationProperties(prefix = "person")
public class Person {
    private String id;
    private String name;
    private String[] childrens;
    private List<String> wifes;
}

切换配置环境:


Properties

如何读取properties中的内容 使用PropertySource 和 Value 注解

@PropertySource(value = "classpath:person.properties")
public class Person {
    @Value("id")
    private String id;
    @Value("name")
    private String name;
}

整合JDBC

即使不使用第三方第数据库操作框架,如 MyBatis等,Spring 本身也对原生的JDBC 做了轻量级的封装,即JdbcTemplate

@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @RequestMapping("/hello")
    public List<Map<String, Object>> hello(){
        String sql="select * from user;";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
}

整合Druid

Druid 是阿里巴巴开源平台上一个数据库连接池实现

整合Mybatis

配置log4j打印sql信息

添加log4j配置文件:log4j.properties

log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO

静态资源

规定写在下面几个路径下

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

Thymeleaf


<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

页面放到这个路径下: classpath:/templates/

写一个demo试试: 前端页面还是要学习模板语法

package com.bagdje.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/t")
    public String test(){
        return "test";
    }
    @RequestMapping("/t1")
	public String test1(Model model){
        //存入数据
        model.addAttribute("msg","Hello,Thymeleaf");
        //classpath:/templates/test.html
        return "test1";
    }
    @RequestMapping("/t2")
    public String test2(Map<String,Object> map){
        //存入数据
        map.put("msg","<h1>Hello</h1>");
        map.put("users", Arrays.asList("qinjiang","kuangshen"));
        //classpath:/templates/test.html
        return "test2";
	}
}

Swagger

springboot 版本低一点是没问题的 我使用的2.5.10 高一点的2.6没有成功

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>

编写一个配置类-SwaggerConfig

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {  
}

访问测试 :http://localhost:8080/swagger-ui.html ,可以看到swagger的界面

使用:

实体类:

@ApiModel("用户实体")
public class User {
   @ApiModelProperty("用户名")
   public String username;
   @ApiModelProperty("密码")
   public String password;
}

controller:

@RestController
public class HelloWorld {
    @ApiOperation("陈天明的接口")
    @RequestMapping(value = "/hello",method= RequestMethod.GET)
    public String hellWorld(@ApiParam("随便的一个get入参") int id){
        return new String(String.valueOf(id)+"helloworld");
    }
}

异步

  1. 给方法添加@Async注解
  2. springboot启动类上加 @EnableAsync //开启异步注解功能

定时任务

  1. 方法加

    //秒 分 时 日 月 周 //0 * * * * MON-FRI //注意cron表达式的用法;

    @Scheduled(cron = "0 * * * * 0-7")

  2. springboot启动类上加@EnableScheduling

posted @ 2022-03-18 16:26  bagdje  阅读(25)  评论(0)    收藏  举报