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文件存放的位置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM