快速搭建SpringBoot项目

本文介绍了一个基于 Spring Boot 的应用配置与实现细节,包括 Maven 配置、Thymeleaf 使用、异常处理、扫描配置、单元测试、日志配置及 XML 映射文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

有些配置容易忘记,新建工程不容易,可以随时查找。


一、springboot的pom文件配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.15.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gupaoedu</groupId>
    <artifactId>Demo3</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.14</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

二、配置文件application.properties

#tomact配置修改
spring.profiles.active=dev
#server.port=8080
#server.servlet.context-path=/

#乱码解决方案设置
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8


#日志
#logging.file=d:/log.log
#logging.level.org.springframework=debug

#静态资源访问
# 表示所有的访问都经过静态资源路径
spring.webflux.static-path-pattern=/**

# 覆盖默认的配置,所有需要将默认的static public等这些路径将不能作为静态资源的访问
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/custom


#文件上传配置相关
spring.servlet.multipart.enabled=true
# 设置单个文件上传的大小
spring.servlet.multipart.max-file-size=200MB
# 设置一次上传文件总的大小
spring.servlet.multipart.max-request-size=200MB

# jdbc的相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/enfei?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

# 连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

## mybatis的package别名
mybatis.type-aliases-package=com.gupaoedu.pojo

# 指定MyBatis的映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml


#自定义配置
user.username = bobo
user.password = 123


三.thymeleaf

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

错误页面1
<span th:text="${errorMsg}"></span>
</body>
</html>

四、异常

package com.gupaoedu.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        System.out.println("全局的自定义异常出现。。。");
        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof NullPointerException){
            modelAndView.setViewName("error1");
            modelAndView.addObject("errorMsg","空指针异常");
        }else if(e instanceof IndexOutOfBoundsException){
            modelAndView.setViewName("error2");
            modelAndView.addObject("errorMsg","数组越界异常");
        }else{
            modelAndView.setViewName("error3");
            modelAndView.addObject("errorMsg","其他异常");
        }
        return modelAndView;
    }
}

五、扫描

package com.gupaoedu;

import com.gupaoedu.filter.FirstFilter;
import com.gupaoedu.filter.SecondFilter;
import com.gupaoedu.listener.FirstListener;
import com.gupaoedu.listener.SecondListener;
import com.gupaoedu.servet.SecondServert;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.jws.WebService;

@SpringBootApplication
@ServletComponentScan
@MapperScan("com.gupaoedu.mapper")
public class SpringbootAppBoot {

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


    @Bean
    public ServletRegistrationBean getRegistrationBean(){
        // 将要添加的Servlet封装为一个ServletRegistrationBean对象
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new SecondServert());
        // 设置映射信息
        registrationBean.addUrlMappings("/second");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean getRegistractionBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        bean.addUrlPatterns("/second");
        return bean;
    }

    @Bean
    public ServletListenerRegistrationBean getListenerBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(new SecondListener());
        return servletListenerRegistrationBean;
    }



}

六、单元测试

在这里插入图片描述

七、日志文件logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="log.context.name" value="gupao"/>
    <property name="log.charset" value="UTF-8"/>
    <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}[%p][%c][%M][%L] %msg%n "/>

    <contextName>${log.context.name}</contextName>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="${log.charset}">
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>./log/${log.context.name}%d{yyyyMMdd}.log</fileNamePattern>
            <maxHistory>5</maxHistory>
        </rollingPolicy>

        <encoder charset="${log.charset}">
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>


    <logger name="org.apache" level="ERROR" />
    <logger name="org.apache.kafka" level="INFO" />
    <logger name="org.springframework" level="ERROR" />
    <logger name="org.springframework.data.elasticsearch.client.WIRE" level="trace"/>
    <logger name="org.elasticsearch.client" level="ERROR"/>


    <root>
        <level value="debug"/>
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>

</configuration>

八 xml配置

<?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.gupaoedu.mapper.UserMapper">
    <select id="query" resultType="User">
        select * from users
    </select>
</mapper>

总结

时常学习,时常记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值