SpringBoot

19次阅读

共计 1397 个字符,预计需要花费 4 分钟才能阅读完成。

一、SpringBoot 简介

1. 使用 SSM 开发项目的时候有什么不足之处

  • 创建 ssm 项目比较麻烦
  • 配置比较麻烦
  • 依赖的配置比较多比较麻烦
  • 手动集成第三方的组件及框架比较麻烦

2. SpringBoot 的优点

  • 构建项目非常方便
  • 配置比较简单,约定大于配置
  • 无缝集成主流第三方框架
  • SpringBoot 不需要额外配置 JavaEE 容器:可以通过 jar 包的形式来运行

3. 缺点

  • 资料很少
  • 资料大都是英文

4. 版本

  • 目前是 Springboot2.1.X 时代,当前最新版本为 2.1.7。
  • 基于 Java8,支持后面的 Java 版本

二、SpringBoot 基本使用

1. Web 网页生成

  • 访问:https://start.spring.io/ 生成项目并下载。

  • 导入 spring-web.jar 包
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  • 编写 controller 用于测试
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "Hello")
public class HelloController {@RequestMapping(value = "SpringBoot")
    public String demo(){return "Hello SpringBoot";}

}
  • 在 application.properties 中编辑注意等号前面的空格会默认去掉,但是后面的不会,所以后面不能加空格
server.port=9090
server.servlet.context-path=/demo
  • 访问:http://localhost:9090/demo/Hello/SpringBoot,

  • 结果

2. IDEA 生成

  • 新建 project 或 module

三、SpringBoot 的配置

1. XXApplication 类

2. SpringBoot 去掉 XML 配置,增加 property 配置

  • 如”SpringBoot 的简单使用“中的”通过网页开始项目“所展示的一样。

3. SpringBoot 的配置文件

3.1 Properteies 配置

  • 配置 context-path

server.servlet.context-path= /demo

等号前面的空格会默认去掉,但是后面的不会,所以后面不能加空格。

  • 修改端口号

server.port=9090
server.servlet.context-path=/demo

以后的 Spring 的诸多配置都会使用这种来配置,而没有了各种 xml 的配置文件。这里的配置项可以自动提示。

  • 数据配置

3.2 YML 文件配置

3.3

四、整合 Web

1. URL 映射跟之前一样

2. @RestController

3. 注册 Servlet,Filter,Listener

4. 注册拦截器

五、整合 Mybaits

1. 基本整合 demo

2. 使用 Mapper 映射文件

3. Mapper 映射文件和注解同时存在

4. @Param 注解

5. 引入 mybatis 配置文件

6. 增加事务

六、自动配置

1. 自动原理

2. 配置项

正文完
 0

SpringBoot

19次阅读

共计 3309 个字符,预计需要花费 9 分钟才能阅读完成。

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

正文完
 0