springboot之关于springboot与传统spring项目简单区别

前言

旧时咱们开发一个普通spring的项目时,会存在很难麻烦的xml配置过程java

  1. 准备jar包,包括spring、springmvc、redis、mybaits、log4j、mysql-connector-java 等等相关jar ...
  2. 配置web.xml文件,Listener配置, Filter配置,servlet配置,log4j配置, error配置...
  3. 配置数据库连接,配置spring事务
  4. 配置视图解析器
  5. 开启注解,自动扫描功能
  6. 配置完成后, 部署tomcat,启动调试

如今,有了springboot,大大减小配置时间,一键启动, 方便又快捷mysql

  1. 起步依赖
  2. 配置数据源
  3. 建立入口 application

没了 就是这么简单web

下面咱们具体说说redis

springboot配置过程

起步依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<!--mybatis 开发包-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<!--springboot web模块支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<!--druid 的数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.31</version>
</dependency>

spring-boot-starter-web包自动帮咱们引入了web模块开发须要的相关jar包,spring

mybatis-spring-boot-starter帮咱们引入了dao开发相关的jar包。sql

spring-boot-starter-xxx是官方提供的starter,xxx-spring-boot-starter是第三方提供的starter。数据库

配置数据源

spring:
  datasource:
     url: jdbc:mysql://127.0.0.1:3306/mybatis_test
     username: root
     password: root
     driver-class-name: com.mysql.jdbc.Driver
     type: com.alibaba.druid.pool.DruidDataSource
     dbcp2:
       min-idle: 5
       initial-size: 5
       max-total: 5
       max-wait-millis: 200

启动入口

@SpringBootApplication
public class SpringbootMybatisDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisDemoApplication.class, args); 
    }
}

springboot经常使用注解说明

@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。tomcat

@Configuration 等同于spring的XML配置文件;使用Java代码能够检查类型安全。安全

@EnableAutoConfiguration 自动配置。springboot

@ComponentScan 组件扫描,可自动发现和装配一些Bean。

@Component可配合CommandLineRunner使用,在程序启动后执行一些基础任务。

@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,而且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。

@Autowired自动导入。

@PathVariable获取参数。

@JsonBackReference解决嵌套外链问题。

@RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。

相关文章
相关标签/搜索