spring mvc改造成spring boot

一、新增依赖:

         <dependency>

                            <groupId>org.springframework.boot</groupId>

                            <artifactId>spring-boot-dependencies</artifactId>

                            <version>2.0.5.RELEASE</version>

                            <type>pom</type>

                            <scope>import</scope>

                  </dependency>

二、新建启动类

@SpringBootApplication

@PropertySource(value={"*****/config.properties"})

@MapperScan(basePackages="***")

@ComponentScan(basePackages={"****"})

public class StartApplication {

    public static void main(String[] args) {

        SpringApplication.run(***.class, args);

    }

}

SpringBootApplication注解=@Configuration + @EnableAutoConfiguration + @ComponentScan

PropertySource指定加载指定的属性文件

MapperScan指定要扫描的Mapper类的包的路径,标识持久层mapper接口,用来找到mapper对象

ComponentScan定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中,标识业务层的类,用来找到业务层对象

3、新建SysConfig类继承WebMvcConfigurationSupport

WebMvcConfigurationSupport是用来全局定制化Spring Boot的MVC特性。如设置拦截器、跨域访问配置、格式化、URI到视图的映射或者其它全局定制接口。

4、属性文件config.properties配置端口号和上下文

server.port=7070

server.servlet.context-path=/**

5、将xml文件中定义的bean改造成java代码的形式申明注册

xml文件中定义:

     

java中定义:

@Configuration注解可以用java代码的形式实现spring中xml配置文件配置的效果。

@Configuration

public class ServiceConfig {

    @Value("${expo.config.path}")

    @Bean(name = "expo4Wrapper")

}

6、添加日志配置文件,在src/main/resources下创建logback-spring.xm。

7、静态资源放在src/main/resources/static下

8、动态资源放在src/main/resources/templates 下,支持themeleaf、jsp、freemarker等。

9、@RestController注解相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解,但使用@RestController这个注解,就不能返回jsp、html页面,视图解析器无法解析jsp、html页面。

如果需要返回数据到jsp或者html页面,则使用@Controller注解。这里推荐使用@Controller注解,因为需要直接返回数据的时候可以增加@ResponseBody注解

10、打包,修改web模块下的pom.xml文件,由war包改成jar包

<packaging>jar</packaging>

11、将默认的tomcat启动改成jetty启动

<dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</artifactId>

         <version>2.0.5.RELEASE</version>

         <exclusions>

                   <exclusion>

                            <groupId>org.springframework.boot</groupId>

                            <artifactId>spring-boot-starter-tomcat</artifactId>

                   </exclusion>

         </exclusions>

</dependency>

<dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-jetty</artifactId>

         <version>2.0.5.RELEASE</version>

</dependency>

12、整合mybatis

启动类添加@MapperScan(详见1)

mybatis.typeAliasesPackage=****  一般对应实体类所在的包

mybatis.mapperLocations=classpath:mapper/*.xml  表示Mapper文件存放的位置

原文地址:https://www.cnblogs.com/qcxdoit/p/10382623.html