SpringBoot开发

SpringBoot

介绍

  1. springboot是简化spring项目搭建和开发的框架
  2. 特点
    1. 简化maven,管理了大量的基础依赖
    2. 基于注解配置,自动装配简化配置
    3. 内嵌tomcat,启动便捷
    4. 方便与其他框架集成-mybatis
    5. 可进行其他外部的配置

创建项目

  1. 导入依赖(父项目和子module)
    1. 父项目 spring-boot-starter-parent
    2. 子项目 导入具体需要的包如:spring-boot-starter-web,spring-boot-starter-jdbc
  2. 写启动类 一般以App或Start结尾,使用SpringBootApplication注解
  3. 写测试类

maven打包

  1. 导入依赖

    <plugin>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    
  2. 使用maven的package进行打包

    在这里插入图片描述

  3. 运行jar包,从项目文件夹进入cmd,运行命令 java -jar xx.jar

配置文件

分类

3种: application.properties application.yml bootstrap.yml(优先级最高,多用于中心化配置)

获取配置参数

  1. 读取yml

    user:
      username: root
      password: 123
    
    1. 使用@Value注解

      @Value("${user.username}")
          private String username;
      
    2. 使用对象接收

      将对象交给spring管理(@component),使用@ConfigurationProperties(prefix=“”),然后自动注入获取值

      @Component
      @ConfigurationProperties(prefix = "user")
      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      /**
       * 读取配置文件参数方式二: 使用对象接收,需要有构造方法和get,set方法
       *      将对象交给spring管理(@component),使用@ConfigurationProperties(prefix="")  然后自动注入获取值
       */
      public class User {
      
          private String username;
          private String password;
          private List<String> hobby;
      }
      
  2. 读取properties文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql///young
    jdbc.username=root
    jdbc.password=root
    

    使用对象接收: 使用PropertySource和ConfigurationPropertis注解,使用@Autowired注入值

    @Component
    @Data
    @PropertySource(value = "classpath:jdbc.properties")
    @ConfigurationProperties(prefix = "jdbc")
    public class JdbcProperty {
    
        private String driver;
        private String url;
        private String username;
        private String password;
    }
    

3.读取xml文件
新建类,使用@ConfigurationPropertis(prefix=“”),@ImportResource(“classpath:application-context.xml”)注解

多环境切换

1.在yml中用active指定环境
  1. 在一个文件中配置

    多环境之间用 - - - 分隔,使用active激活指定环境

    spring:
      profiles:
        active: test #激活环境
    
    #多环境之间用 --- 分隔
    #开发环境 dev
    ---
    server:
      port: 8081
    spring:
      profiles: dev #环境名称
      application:
        name: spring-boot-dev
    
    #测试环境 test
    ---
    server:
      port: 8082
    spring:
      profiles: test
      application:
        name: spring-boot-test
    
  2. 使用多文件配置

application.yml

spring:
  profiles:
    active: dev #激活环境

application-dev.yml

server:
  port: 8081
spring:
  profiles: dev #环境名称
  application:
    name: spring-boot-dev

application-test.yml

#测试环境 test
server:
  port: 8082
spring:
  profiles: test
  application:
    name: spring-boot-test
在idea中配置

-Dspring.profiles.active=dev

在运行时指定

java -jar xx.jar --spring.profiles.active=prod

测试

  1. springboot2.4及之前,spring-boot-starter-test中是Junit4

    测试类上注解:@RunWith(SpringRunner.class),@SpringBootTest(classes = 启动类.class)

  2. springboot2.4之后,为Junit5

    注解为:@ExtendWith(SpringExtension.class),@SpringBootTest(classes = 启动类…class)

lombok

@Data

包含了@Setter @Getter @EqualsAndHashCode @ToString

@Slf4j

日志注解,使用 log.info(“info打印”);

Thymeleaf

thymeleaf是springboot提供的前端页面模板技术,其实是templates下的html,需要引入头文件,用th:text=${}获取值

<!DOCTYPE html>
<!--引入头文件-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>接收数据</title>
</head>
<body>
    <!--使用thymeleaf语法 th:text=${} 将div中的innerText为获取的值:${}-->
    <div th:text="${msg}">显示默认数据</div>
</body>
</html>

springboot默认配置了视图解析器 前缀 /templates/ 后缀 .html

可以在yml中写配置并在ThymeleafProperties中配合修改

拦截器

  1. 编写类实现HandlerInterceptor接口,实现对应的方法

    @Component
    public class MyInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println(request.getRequestURI()+"  通过了拦截器检查...");
            return true;
        }
    }
    
  2. 写配置类(@Configuration)实现WebMvcConfigurer接口,并重写addInterceptors方法

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Autowired
        private MyInterceptor interceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(interceptor)
                    .addPathPatterns("/**")
                    .excludePathPatterns("/login", "/logout");
        }
    }
    

整合Mybatis

  1. 导包

    <dependency>
    	<groupId>org.mybatis.spring.boot</groupId>
    	<artifactId>mybatis-spring-boot-starter</artifactId>
    	<version>2.2.1</version>
    </dependency>
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    </dependency>
    
  2. 配置数据源

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql:///young
        username: root
        password: root
    
  3. Mapper映射文件路径和别名配置

    mybatis:
      mapper-locations: classpath:com/zhc/mapper/*Mapper.xml
      type-aliases-package: com.zhc.domain
    
  4. 编写Mapper和Service(@Service)

  5. 启动类添加映射器扫描注解 @MapperScan(“com.zhc.mapper”)

    @SpringBootApplication
    @MapperScan("com.zhc.mapper")
    public class MybatisApp {
        public static void main(String[] args) {
            SpringApplication.run(MybatisApp.class, args);
        }
    }
    

Mapper多模块开发

在这里插入图片描述

  1. 别名配置,多个包路径之间有,隔开

    mybatis:
      # Mapper.java 和 Mapper.xml在相同对应的包下(编译之后在同一个包),可以不配置
    #  mapper-locations: classpath:com/zhc/mapper/*Mapper.xml
      type-aliases-package: com.zhc.auth.server.domain,com.zhc.org.server.domain
    
  2. MapperScan中的不同包用 * 表示,一个 * 表示一个层级

    @MapperScan("com.zhc.*.*.mapper")
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值