1

框架技术: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层

image.png

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

Service层

image.png

ServiceImpl层

image.png

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

Mapper层

image.png

注意:需要扫描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...

chaohen
1 声望1 粉丝

与其期望,不然行动~


下一篇 »
zookeeper的安装