SpringBoot的基本配置

1.有网络创建SpringBoot工程

2.无网络 通过Maven来创建SpringBoot工程

通过Maven创建springBoot工程

①创建Maven项目

②继承父工程


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.1</version>
 </parent>

③添加web依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

④创建项目启动类

 


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

}

⑤创建配置文件

配置文件有两种格式:

application.properties 以properties结尾的

application.yml 以yml 结尾的

其中properties文件的优先级高于yml。

3.读取配置文件的属性值


<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

通过使用@ConfifigurationProperties(prefifix = "student")来读取值


@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private String name;
    private Integer age;
    private String  sex;
 
}

 调用 验证是否成功


@RestController
public class hello {
    @Autowired
    Student student;
    @GetMapping("one")
    public Student getStudent(){
        System.out.println(student);
        return student;
    }

}

 调用成功。

通过 @Value 注解来读取

public class Student {
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private Integer age;
    @Value("${student.sex}")
    private String  sex;
 
}

4.SpringBoot如何注册Web三大组件

什么是web三大组件:

1. Servlet:

2. Filter:过滤器

3. Listener:监听器

 

1.注册Servlet

1)创建servlet类


public class Myservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("经过doGet方法");
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("经过doPost方法");
    }
}

2)配置类


@Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new MyServlet());
        registrationBean.setName("myServlet");
        registrationBean.addUrlMappings("one");
        return registrationBean;
    }

 2.注册filter

1)创建过滤器


package com.cbb.filter;
 
 
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
public class MyFilter implements Filter {
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
 
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
 
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
 
        System.out.println("经过过滤器");
        filterChain.doFilter(request , response);
 
    }
}

2)注册该过滤器到SpringBoot容器中


package com.cbb.config;
 
import com.cbb.filter.MyFilter;
import com.cbb.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class Myconfig {
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new MyFilter());
        bean.setName("myFilter");
        bean.addUrlPatterns("/*");
        return bean;
    }

3.注册监听器

通过@Component注册


package com.cbb.listener;
 
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener implements ApplicationListener<ApplicationStartedEvent> {
 
    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
        System.out.println("监听器!~!");
    }
}

5.SpringBoot整合数据源

1.添加Maven依赖


<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

2.配置application文件

spring.datasource.username=root 
spring.datasource.password=123456 
spring.datasource.url=jdbc:mysql://localhost:3306/stu?serverTimezone=Asia/Shanghai 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

 6.springboot整合mybatis

1.引入相关依赖


<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

# 配置映射文件所在的路径

mybatis.mapper-locations=classpath:/mapper/*.xml

    2.创建相应的entity以及mapper 和 从和创建在resources下创建映射文件


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<!--命名空间  接口的包名 +接口的名字-->
<mapper namespace="com.cbb.Dao.StuMapper">
 
    <select id="getStu" resultType="com.cbb.entity.Student">
        select * from student
    </select>
 
</mapper>

 3.在主启动类上扫描mapper接口所在的包

4.controller调用mapper的方法


@RestController
public class StuController {
 
    @Autowired
    StuMapper mapper;
 
    @GetMapping("getStu")
    public List<Student> getALl(){
        return mapper.getStu();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值