一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例

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

2020-09-29 14:19Java之家 Java教程

這篇文章主要介紹了Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例,需要的朋友可以參考下

前言

距離第一篇 Spring Boot 系列的博文 3 個(gè)月了。雖然 XML 形式是我比較推薦的,但是注解形式也是方便的。尤其一些小系統(tǒng),快速的 CRUD 輕量級(jí)的系統(tǒng)。

這里感謝曉春 http://xchunzhao.tk/ 的 Pull Request,提供了 springboot-mybatis-annotation 的實(shí)現(xiàn)。

一、運(yùn)行 springboot-mybatis-annotation 工程

然后Application 應(yīng)用啟動(dòng)類的 main 函數(shù),然后在瀏覽器訪問:

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

可以看到返回的 JSON 結(jié)果:

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

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

1.pom 添加 Mybatis 依賴

?
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
<?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 啟動(dòng)父依賴 -->
 <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 連接驅(qū)動(dòng)依賴 -->
 <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 城市數(shù)據(jù)操作層接口類添加注解 @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
/**
* 城市 DAO 接口類
*
* Created by xchunzhao on 02/05/2017.
*/
@Mapper // 標(biāo)志為 Mybatis 的 Mapper
public interface CityDao {
/**
* 根據(jù)城市名稱,查詢城市信息
*
* @param cityName 城市名
*/
@Select("SELECT * FROM city")
// 返回 Map 結(jié)果集
@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 標(biāo)志接口為 MyBatis Mapper 接口

@Select 是 Select 操作語句

@Results 標(biāo)志結(jié)果集,以及與庫表字段的映射關(guān)系

其他的注解可以看 org.apache.ibatis.annotations 包提供的,如圖:

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

可以 git clone 下載工程 springboot-learning-example ,springboot-mybatis-annotation 工程代碼注解很詳細(xì)。 https://github.com/JeffLi1993/springboot-learning-example

以上所述是小編給大家介紹的Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.tuicool.com/articles/q2eIzmF

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本一区二区三区久久 | 午夜国产精品福利在线观看 | 4444www免费看| 国产做a爰片久久毛片 | 日韩视频在线免费 | 国产精品免费精品自在线观看 | 四虎现在的网址入口2022 | 雪恋电影完整版免费观看 | 99精彩视频在线观看 | 98在线视频噜噜噜国产 | 欧美操屁股 | 男女一级特黄a大片 | 欧美特一级 | 国产亚洲精品精品国产亚洲综合 | 国产98在线| 成功精品影院 | 久久无码AV亚洲精品色午夜麻豆 | 免费看国产一级片 | 国产精品资源在线观看 | 5566中文字幕亚洲精品 | 喷奶水榨乳ova动漫无修 | 俄罗斯freeoo性另类 | 波多野结衣在线中文字幕 | 国产精品九九热 | 亚洲狠狠婷婷综合久久久久网站 | 天天夜夜草草久久伊人天堂 | 校园纯肉H教室第一次 | 天天视频国产精品 | 国产成人影院 | 国产成人手机在线好好热 | 久久99热狠狠色AV蜜臀 | 私人影院在线免费观看 | 呜呜别塞了啊抽插 | 成人国产在线播放 | 夫妻性生活在线 | 亚洲卡一卡2卡三卡4卡无卡三 | 日韩在线1 | 精品国产理论在线观看不卡 | 999热这里全都是精品 | 2020年新四虎免费 | 亚州精品永久观看视频 |