SpringBoot

1 了解SpringBoot

  • 帮我们快速构建项目,无需过多配置。
  • 并非没有配置,而是通过starter提供默认配置。
  • starter其实就是一个jar包,里面提供了properties文件。

2 快速入门

2.1 修改Maven配置

  • 注意:使用本地Maven仓库,否则Maven没有代码提示,并且更新一下本地库。

2.2 修改父工程

  • spring-boot-starter-parent

2.3 添加web启动器

  • spring-boot-starter-web

2.4 启动类

  • 在main函数里添加一行代码
@SpringBootApplication
public class BootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootDemoApplication.class);
    }
}

2.5 编写controller

-注意:如果写成Rest风格,就不要写@ResponseBody

@Controller
public class HelloController {

    @GetMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello, spring boot !";
    }
}

2.6 运行main函数

3 Java配置

3.1 回顾历史

3.1.1 Spring 1.0时代

  • 那时候一切配置都是xml,细思极恐啊

3.1.2 Spring 2.0时代

  • xml与注解结合

3.1.3 Spring 3.0及以后

  • Servlet3 不需要web.xml

3.2 尝试java配置

  • @Configuration:声明一个类作为配置类,代替xml文件
  • @Bean:声明在方法上,将方法的返回值加入Bean容器,代替标签
  • @Value:属性注入
  • @PropertySource:指定外部属性文件

3.3 SpringBoot的属性注入

  • 默认的文件名:application.properties

3.3.1 第一种方式

  • @ConfigurationProperties(prefix = "jdbc")注解声明当前类为属性读取类
  • @EnableConfigurationProperties(JdbcProperties.class)让当前类可以使用JdbcProperties.class的对象
  • @Bean,参数注入

3.3.2 第二种方式

  • 如果一段属性只有一个Bean需要使用,那无需注入到一个类,而是直接声明即可。
  • 直接把@ConfigurationProperties(prefix = "jdbc")声明在需要读取properties方法上。

3.4 yml配置文件

  • 使用“:”进行分隔

4 自动配置原理

  • 启动器:starter
  • 全局配置:application.properties

5 SpringBoot实践

5.1 整合SpringMVC

  • 添加自定义配置,例如拦截器,让一个类实现WebMvcConfigure,并添加@Configuration注解。

5.2 整合jdbc和事务

  • jdbc自带事务,添加@Transactional注解即可

5.3 整合连接池

  • starter内置极光连接池

5.4 整合mybatis

  • 使用通用mapper
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值