资讯专栏INFORMATION COLUMN

SpringBoot

gaomysion / 2455人阅读

摘要:前面一种使用的是模板进行查询的。并且最好在接口的方法内使用注解来声明下。,非常适合在应用程序启动之初进行一些数据初始化的工作。接下来我们直接创建一个类继承,并实现它的方法。在实践中,使用也可以达到相同的目的,两着差别不大。

框架技术:SpringBoot+Mybatis+MySQL等
配置文件

配置文件application.propreties文件

spring.datasource.url=jdbc:mysql://localhost:3306/blogs?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

如果使用yml文件 则使用下面配置

#spring配置
spring:
  profiles:
    active: dev
  thymeleaf:
    mode: HTML
    cache: false
#视图层配置
  mvc:
    view:
      prefix: /templates
    favicon:
      enabled: false
#数据库
  datasource:
    url: jdbc:mysql://localhost:3308/blogs?characterEncoding=utf8&useSSL=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
Controller层

注意: 使用Restful时 要加上@PathVariable注解 。前面一种使用的是JDBCTemplate模板进行查询的。

Service层

ServiceImpl层

注意:需要扫描Sercies包时,加上@Service注解

Mapper层

注意:需要扫描Mapper包时,需要在接口前面声明@Mapper注解。这里的方法可以使用@Select、@Insert、@Update、@Delete注解,后面跟上sql语句就可以查询,当然也可以使用Mapper.xml形式进行查询。后面带有参数的使用一般使用#{params}或${params},前者会解析成字符串(也就是带有双引号)后者会解析成原来的样子(比如传入的是int类型的数字,解析成数字,传入字符串,则解析成字符串)。并且最好在接口的方法内使用@Param()注解来声明下。

,非常适合在应用程序启动之初进行一些数据初始化的工作。

@SpringBootApplication
public class CommandLineRunnerApplication {
    public static void main(String[] args) {
        System.out.println("The service to start.");
        SpringApplication.run(CommandLineRunnerApplication.class, args);
        System.out.println("The service has started.");
    }
}

接下来我们直接创建一个类继承 CommandLineRunner ,并实现它的 run() 方法。

@Component
public class Runner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The Runner start to initialize ...");
    }
}

我们在 run() 方法中打印了一些参数来看出它的执行时机。完成之后启动项目进行测试:

...
The service to start.
  .   ____          _            __ _ _
 / / ___"_ __ _ _(_)_ __  __ _    
( ( )\___ | "_ | "_| | "_ / _` |    
 /  ___)| |_)| | | | | || (_| |  ) ) ) )
  "  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)
...
2018-04-21 22:21:34.706  INFO 27016 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ""
2018-04-21 22:21:34.710  INFO 27016 --- [           main] com.neo.CommandLineRunnerApplication     : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128)
The Runner start to initialize ...
The service has started.

根据控制台的打印信息我们可以看出 CommandLineRunner 中的方法会在 Spring Boot 容器加载之后执行,执行完成后项目启动完成。

如果我们在启动容器的时候需要初始化很多资源,并且初始化资源相互之间有序,那如何保证不同的 CommandLineRunner 的执行顺序呢?Spring Boot 也给出了解决方案。那就是使用 @Order 注解。

我们创建两个 CommandLineRunner 的实现类来进行测试:

第一个实现类:

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner1 start to initialize ...");
    }
}

第二个实现类:

@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner2 start to initialize ...");
    }
}

添加完成之后重新启动,观察执行顺序:

...
The service to start.
  .   ____          _            __ _ _
 / / ___"_ __ _ _(_)_ __  __ _    
( ( )\___ | "_ | "_| | "_ / _` |    
 /  ___)| |_)| | | | | || (_| |  ) ) ) )
  "  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)
...
2018-04-21 22:21:34.706  INFO 27016 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ""
2018-04-21 22:21:34.710  INFO 27016 --- [           main] com.neo.CommandLineRunnerApplication     : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128)
The OrderRunner1 start to initialize ...
The OrderRunner2 start to initialize ...
The Runner start to initialize ...
The service has started.

通过控制台的输出我们发现,添加 @Order 注解的实现类最先执行,并且@Order()里面的值越小启动越早。

在实践中,使用ApplicationRunner也可以达到相同的目的,两着差别不大。

转载:https://www.cnblogs.com/ityou...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74080.html

相关文章

  • 市长信箱邮件查询服务: 将SpringBoot应用部署到Docker

    摘要:市长信箱邮件查询服务将应用部署到在上一章我完成了将部署到的工作和都具有能快速启动的特性因此是一对用来部署微服务的黄金搭档在计划中基于的应用也将部署到之上那我们就开始行动吧将部署到上需要执行以下步骤保证打包后的可执行能正常启动在应用中编写镜像 市长信箱邮件查询服务: 将SpringBoot应用部署到Docker 在上一章, 我完成了将ES部署到Docker的工作. SpringBoot和...

    SKYZACK 评论0 收藏0
  • Gradle构建SpringBoot程序依赖管理之依赖版本自动控制

    摘要:前言体系中很多库之间相互依赖,但是由于版本之间差异比较大,我们需要比较精确的知道每个库的版本对应关系,不然很容易造成库与库之间对应不上,导致部分功能无效,甚至是异常情况。 前言:Spring体系中很多库之间相互依赖,但是由于版本之间差异比较大,我们需要比较精确的知道每个库的版本对应关系,不然很容易造成库与库之间对应不上,导致部分功能无效,甚至是异常情况。程序员排查起来一个头两个大,本篇...

    刘德刚 评论0 收藏0
  • springboot启动 @ComponentScan错误

    摘要:错误提示根据的来运行会报出下面的错误解决方法原来是是偷懒没有把放到某个下面,而是直接放到了。 错误提示 根据springboot的tutorial来运行 https://spring.io/guides/gs/spring-boot/ 会报出下面的错误: 2016-01-28 11:35:42.034 INFO 3732 --- [ main] Applicati...

    Anchorer 评论0 收藏0
  • java-study-springboot-基础学习-02-Springboot helloworl

    摘要:自动配置会根据项目中的包依赖,自动做出配置,支持的自动配置如下非常多甚至包含了备注如果我们不需要自动配置,想关闭某一项的自动配置,该如何设置呢比如我们不想自动配置,想手动配置。 SpringBoot 入门 1、SpringBoot 入口类说明 1.1注解说明 showImg(https://segmentfault.com/img/bVbjvc9?w=707&h=232);@Sprin...

    Amos 评论0 收藏0
  • SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程

    摘要:简介它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个工程。它采用的是建立生产就绪的应用程序观点,优先于配置的惯例。另,本系列教程全部用的作为开发工具。 简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程。它采用的是建立生产就绪的应用程序观...

    Cheriselalala 评论0 收藏0
  • SpringBoot

    摘要:基于,支持后面的版本二基本使用网页生成访问生成项目并下载。这里的配置项可以自动提示。数据配置文件配置四整合映射跟之前一样注册注册拦截器五整合基本整合使用映射文件映射文件和注解同时存在注解引入配置文件增加事务六自动配置自动原理配置项 一、SpringBoot简介 1. 使用SSM开发项目的时候有什么不足之处 创建ssm项目比较麻烦 配置比较麻烦 依赖的配置比较多比较麻烦 手动集成第三方...

    mykurisu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<