springboot整合mybatis,springmvc

下面將介紹的是springboot和mybatis,springmvc整合相關步驟,在這裏做個記錄,方便以後查看以及更改錯誤的問題,也歡迎大家指出問題的所在,共同進步:

一,創建簡單的springboot應用

首先創建一個maven項目(使用eclipse創建,如果使用idea創建項目需要創建指定的springboot應用,我在idea中創建maven項目啓動失敗)

在pom.xml中添加以下依賴:

①設置spring boot的parent

<parent>

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

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

      <version>1.5.2.RELEASE</version>

   </parent>

說明:Spring boot的項目必須要將parent設置爲spring boot的parent,該parent包含了大量默認的配置,大大簡化了我們的開發。

spring 官方的解釋叫什麼 stater poms,它可以提供 dependency management,也就是說依賴管理,引入以後在申明其它 dependency 的時候就不需要 version 了。

②導入spring boot的web支持

<dependency>

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

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

      </dependency>

說明:因爲我們開發的是 web 工程,所以需要在 pom.xml 中引入 spring-boot-starter-web,spring 官方解釋說 spring-boot-start-web 包含了 spring webmvc 和 tomcat 等 web 開發的特性。

③  添加Spring boot的插件

<plugin>

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

            <artifactId>spring-boot-maven-plugin</artifactId>

         </plugin>

說明:如果我們要直接 Main 啓動 spring,那麼以下 plugin 必須要添加,否則是無法啓動的。如果使用 maven 的 spring-boot:run 的話是不需要此配置的。(我在測試的時候,如果不配置上面的 plugin 也可以直接在 Main 中運行的。)

 

測試:編寫第一個Spring Boot的應用

@Controller
@SpringBootApplication
@Configuration
public class TaotaoApplication{

    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
      return "hello sunhongbing!";
    }

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

說明:代碼說明:

1、@SpringBootApplication:Spring Boot項目的核心註解,主要目的是開啓自動配置。;

2、@Configuration:這是一個配置Spring的配置類;

3、@Controller:標明這是一個SpringMVC的Controller控制器;

4、main方法:在main方法中啓動一個應用,即:這個應用的入口;

 

啓動應用:

在Spring Boot項目中,啓動的方式有兩種,一種是直接run Java Application另外一種是通過Spring Boot的Maven插件運行。

第一種:

 

第二種:

 

啓動效果:

看到如下信息就說明啓動成功了:

INFO 6188 --- [           main] c.i.springboot.demo.HelloApplication     : Started HelloApplication in 3.281 seconds (JVM running for 3.601)

測試

打開瀏覽器,輸入地址,效果:

 

二,和mybatis整合

①  設置tomcat端口

application.properties:

② 讀取外部的配置文件

@Configuration

@PropertySource(value = { "classpath:jdbc.properties", "classpath:env.properties",

        "classpath:httpclient.properties", "classpath:redis.properties", "classpath:rabbitmq.properties" }, ignoreResourceNotFound = true)

public class TaotaoApplication {

}

 

③ 設置掃描包

 

④ 定義數據源

 

⑤進入mybatis和springboot的整合

Mybatis和Spring Boot的整合有兩種方式:

第一種:使用mybatis官方提供的Spring Boot整合包實現,地址:https://github.com/mybatis/spring-boot-starter

第二種:使用mybatis-spring整合的方式,也就是我們傳統的方式

 

這裏我們推薦使用第二種,因爲這樣我們可以很方便的控制Mybatis的各種配置。

 

首先,創建一個Mybatis的配置類:

代碼:

然後,創建Mapper接口的掃描類MapperScannerConfig:

代碼:

 

⑥ 設置事務管理

在Spring Boot中推薦使用@Transactional註解來申明事務。

首先需要導入依賴:

<dependency>

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

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

      </dependency>

當引入jdbc依賴之後,Spring Boot會自動默認分別注入DataSourceTransactionManager或JpaTransactionManager,所以我們不需要任何額外配置就可以用@Transactional註解進行事務的使用。

在Service中添加@Transactional註解:

 

 

三,整合SpringMVC的配置

原有配置:

具體實現:

視圖解析器配置:

自定義攔截器:

 

 

實現自定義攔截器只需要 3 步:

1、創建我們自己的攔截器類並實現 HandlerInterceptor 接口。

2、創建一個 Java 類繼承 WebMvcConfigurerAdapter,並重寫 addInterceptors 方法。

3、實例化我們自定義的攔截器,然後將對像手動添加到攔截器鏈中(在 addInterceptors 方法中添加)。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章