传统项目springboot化---springmvc框架jsp项目转化

目标

对于一些还在使用springmvc的老项目需要springboot化时的一些改造点梳理,附上demo代码,spring-boot版本1.5.9

demo代码

[email protected]:kaiyang_taichi/springmvc2boot.git
备注:此代码本文的第8和第9的改动,所以以java -jar的形式运行会有问题,请参考自行修改

改造点

  1. 创建项目,支持jsp
    springboot支持jsp需要加入如下依赖
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        

        
        
            javax.servlet
            javax.servlet-api
            provided
        
        
            javax.servlet
            jstl
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        

同时application.yml中添加view解析配置,根目录放入webapp目录,然后WEB-INF/page都放在webapp下

spring:
  mvc:
    view:
      prefix: /WEB-INF/page/
      suffix: .jsp
#关闭模版引擎
  thymeleaf:
    cache: false
    enabled: false
  application:
    name: power-boss
  1. 添加过滤器

过滤器添加有注解和代码配置两种,注解方法如下

@WebFilter(filterName = "testFilter",urlPatterns = {"/app/*"})
public class TestFilter implements Filter {

    private Logger LOGGER= LoggerFactory.getLogger(getClass());


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        LOGGER.info("请求filer");
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}

同时在springboot入口添加注解ServletComponentScan

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ServletComponentScan
public class BossStartApplication {

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

  1. 配置静态资源

静态资源配置通过继承WebMvcConfigurerAdapter配置

package com.yeepay.power.configuration;

import com.yeepay.power.interceptor.TestInterceptor;
import com.yeepay.power.interceptor.TestInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/app/*");
        registry.addInterceptor(new TestInterceptor2()).addPathPatterns("/app/*");
        super.addInterceptors(registry);

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        super.addResourceHandlers(registry);

    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //配置根目录欢迎页
        registry.addViewController("/").setViewName("demo");
        super.addViewControllers(registry);
    }
}


  1. 配置springmvc拦截器

    同3的方法配置,其中添加addInterceptors即可
  2. 配置上传资源限制

在yml中添加配置

spring:
  mvc:
    view:
      prefix: /WEB-INF/page/
      suffix: .jsp
#关闭模版引擎
  thymeleaf:
    cache: false
    enabled: false
  application:
    name: power-boss
  http:
    multipart:
#配置上传大小
      max-file-size: 10Mb
      max-request-size: 10Mb
  1. mybatis-generator集成
    一. 添加pom配置
  
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    
                        mysql
                        mysql-connector-java
                        5.1.39
                    
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.5
                    
                
                
                    
                        Generate MyBatis Artifacts
                        package
                        
                            generate
                        
                    
                
                
                    
                    true
                    
                    true
                    
                    
                        src/main/resources/mybatis-generator.xml
                
            

        
    

添加好plugin,下面添加generator的配置文件src/main/resources/mybatis-generator.xml,其中具体类名、包名根据需要自己修改





    
        
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
        
            
        
        
        

maven执行命令mybatis-generator:generate即可


传统项目springboot化---springmvc框架jsp项目转化_第1张图片
maven.gif

切记:每次最好重新生成文件,否则generator可能会有追加mapper的情况,造成文件报错,如Result Maps collection already contains value.....

  1. springboot集成mybatis+c3p0
    一. 添加pom
  
            mysql
            mysql-connector-java
            runtime
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.2
        

        
            c3p0
            c3p0
            0.9.1.2
        

二. application.yml添加配置

spring: 
  datasource:
    url: jdbc:mysql://local.p2.cn:3306/demotp?useSSL=true
    username: root
    password: yeepay.com
    driver-class-name: com.mysql.jdbc.Driver
    type: com.mchange.v2.c3p0.ComboPooledDataSource
mybatis:
  mapper-locations: classpath:mybatis/*Mapper.xml
  type-aliases-package: com.yeepay.power.entity
  configuration:
##开启驼峰
    map-underscore-to-camel-case: true
    default-fetch-size: 500
    default-statement-timeout: 80

三. 入口加注解

package com.yeepay.power;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@SpringBootApplication
@ServletComponentScan
//事务相关
@EnableTransactionManagement
//dao 类 扫描
@MapperScan(basePackages = "com.yeepay.power.dao")
public class BossStartApplication {

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

注意:DataSourceAutoConfiguration.class千万不要execule,否则数据库无法加载
到此mybatis集成完了,网上有很多说sqlsessionFactory报错的,要自己写配置类注入,有人说是高版本问题(这个我没验证,我这个版本是没问题),一开始我也报过这个错是因为DataSourceAutoConfiguration.class被execule了。
四. 对于调试看数据库sql,只需要配置dao层的包未DEBUG即可,底层生成了代理类,如

        

  1. 打包配置,springboot项目正常打包资源包默认只会打包resources,webapp不会打包,所以pom需要添加如下配置,指定打包时添加webapp目录
  
        power-boss
        
            
                src/main/resources
            
            
                src/main/webapp
            
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
  
  1. 并且webapp目录要配套项目必须打包成war文件,否则也找不到jsp页面
    4.0.0
    power-boss
    ${power.version}
    war
    power-boss
    power-boss


好了,springmvc转springboot的常用涉及点基本够用了。

你可能感兴趣的:(传统项目springboot化---springmvc框架jsp项目转化)