Spring Boot 整合 Mybatis Annotation 註解的完整 Web 案例

摘要: 原創出處 www.bysocket.com 「泥瓦匠BYSocket 」歡迎轉載,保留摘要,謝謝!java

『 公司須要人、產品、業務和方向,方向又要人、產品、業務和方向,方向… 循環』mysql

本文提綱
1、前言
2、運行 springboot-mybatis-annotation 工程
3、springboot-mybatis-annotation 工程配置詳解
4、小結git

運行環境:JDK 7 或 八、Maven 3.0+
技術棧:SpringBoot 1.5+、SpringBoot Mybatis Starter 1.2+ 、MyBatis 3.4+github

前言

距離第一篇 Spring Boot 系列的博文 3 個月了。《Springboot 整合 Mybatis 的完整 Web 案例》第一篇出來是 XML 配置 SQL 的形式。雖然 XML 形式是我比較推薦的,可是註解形式也是方便的。尤爲一些小系統,快速的 CRUD 輕量級的系統。web

這裏感謝曉春 http://xchunzhao.tk/ 的 Pull Request,提供了 springboot-mybatis-annotation 的實現。spring

1、運行 springboot-mybatis-annotation 工程

因爲這篇文章和 《Springboot 整合 Mybatis 的完整 Web 案例》 相似,因此運行這塊環境配置你們參考另一篇兄弟文章。sql

而後Application 應用啓動類的 main 函數,而後在瀏覽器訪問:shell

1
http: //localhost:8080/api/city?cityName=溫嶺市

能夠看到返回的 JSON 結果:apache

1
2
3
4
5
6
{
"id" : 1,
"provinceId" : 1,
"cityName" : "溫嶺市" ,
"description" : "個人家在溫嶺。"
}

 

3、springboot-mybatis-annotation 工程配置詳解

1.pom 添加 Mybatis 依賴api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<? xml version = "1.0" encoding = "UTF-8" ?>
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
     < modelVersion >4.0.0</ modelVersion >
 
     < groupId >springboot</ groupId >
     < artifactId >springboot-mybatis-annotation</ artifactId >
     < version >0.0.1-SNAPSHOT</ version >
     < packaging >jar</ packaging >
 
     < name >springboot-mybatis-annotation</ name >
     < description >Springboot-mybatis :: 整合Mybatis Annotation Demo</ description >
 
     <!-- Spring Boot 啓動父依賴 -->
     < parent >
         < groupId >org.springframework.boot</ groupId >
         < artifactId >spring-boot-starter-parent</ artifactId >
         < version >1.5.1.RELEASE</ version >
     </ parent >
 
     < properties >
         < mybatis-spring-boot >1.2.0</ mybatis-spring-boot >
         < mysql-connector >5.1.39</ mysql-connector >
     </ properties >
 
     < dependencies >
 
         <!-- Spring Boot Web 依賴 -->
         < dependency >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-web</ artifactId >
         </ dependency >
 
         <!-- Spring Boot Test 依賴 -->
         < dependency >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-test</ artifactId >
             < scope >test</ scope >
         </ dependency >
 
         <!-- Spring Boot Mybatis 依賴 -->
         < dependency >
             < groupId >org.mybatis.spring.boot</ groupId >
             < artifactId >mybatis-spring-boot-starter</ artifactId >
             < version >${mybatis-spring-boot}</ version >
         </ dependency >
 
         <!-- MySQL 鏈接驅動依賴 -->
         < dependency >
             < groupId >mysql</ groupId >
             < artifactId >mysql-connector-java</ artifactId >
             < version >${mysql-connector}</ version >
         </ dependency >
 
         <!-- Junit -->
         < dependency >
             < groupId >junit</ groupId >
             < artifactId >junit</ artifactId >
             < version >4.12</ version >
         </ dependency >
     </ dependencies >
 
 
</ project >

2.在 CityDao 城市數據操做層接口類添加註解 @Mapper、@Select 和 @Results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 城市 DAO 接口類
*
* Created by xchunzhao on 02/05/2017.
*/
@Mapper // 標誌爲 Mybatis 的 Mapper
public interface CityDao {
 
/**
* 根據城市名稱,查詢城市信息
*
* @param cityName 城市名
*/
@Select ( "SELECT * FROM city" )
// 返回 Map 結果集
@Results ({
@Result (property = "id" , column = "id" ),
@Result (property = "provinceId" , column = "province_id" ),
@Result (property = "cityName" , column = "city_name" ),
@Result (property = "description" , column = "description" ),
})
City findByName( @Param ( "cityName" ) String cityName);
}

@Mapper 標誌接口爲 MyBatis Mapper 接口
@Select 是 Select 操做語句
@Results 標誌結果集,以及與庫表字段的映射關係

其餘的註解能夠看 org.apache.ibatis.annotations 包提供的,如圖:

 

能夠 git clone 下載工程 springboot-learning-example ,springboot-mybatis-annotation 工程代碼註解很詳細。 https://github.com/JeffLi1993/springboot-learning-example

4、小結

註解不涉及到配置,更近貼近 0 配置。再次感謝曉春 http://xchunzhao.tk/ 的 Pull Request~

歡迎掃一掃個人公衆號關注 — 及時獲得博客訂閱哦!
— http://www.bysocket.com/ —
— https://github.com/JeffLi1993 —

相關文章
相關標籤/搜索