栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

快速搭建SpringBoot项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

快速搭建SpringBoot项目

文章目录
  • 前言
  • 一、springboot的pom文件配置
  • 二、配置文件application.properties
  • 三.thymeleaf
  • 四、异常
  • 五、扫描
  • 六、单元测试
  • 七、日志文件logback.xml
  • 八 xml配置
  • 总结


前言

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


一、springboot的pom文件配置


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.15.RELEASE
         
    
    com.gupaoedu
    Demo3
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
            1.0.14
        

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    

二、配置文件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/gp?serverTimezone=UTC
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



    
    Title



错误页面1



四、异常
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



    
    
    

    ${log.context.name}

    
        
            ${log.pattern}
        
    

    
        
            ./log/${log.context.name}%d{yyyyMMdd}.log
            5
        

        
            ${log.pattern}
        
    


    
    
    
    
    


    
        
        
        
    


八 xml配置



    
        select * from users
    

总结

时常学习,时常记录

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号