Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
前言
距離第一篇 Spring Boot 系列的博文 3 個(gè)月了。雖然 XML 形式是我比較推薦的,但是注解形式也是方便的。尤其一些小系統(tǒng),快速的 CRUD 輕量級的系統(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é)果:
{
"id": 1,
"provinceId": 1,
"cityName": "溫嶺市",
"description": "我的家在溫嶺。"
}
三、springboot-mybatis-annotation 工程配置詳解
1.pom 添加 Mybatis 依賴
<?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
/**
* 城市 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 包提供的,如圖:

可以 git clone 下載工程 springboot-learning-example ,springboot-mybatis-annotation 工程代碼注解很詳細(xì)。 https://github.com/JeffLi1993/springboot-learning-example 。
以上所述是小編給大家介紹的Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Spring整合Mybatis 掃描注解創(chuàng)建Bean報(bào)錯(cuò)的解決方案
- springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對一(一對多)查詢功能
- SpringBoot整合Mybatis注解開發(fā)的實(shí)現(xiàn)代碼
- Spring Boot整合mybatis使用注解實(shí)現(xiàn)動(dòng)態(tài)Sql、參數(shù)傳遞等常用操作(實(shí)現(xiàn)方法)
- 詳解SpringBoot 快速整合Mybatis(去XML化+注解進(jìn)階)
- Spring與Mybatis基于注解整合Redis的方法
- 純注解版spring與mybatis的整合過程
相關(guān)文章
java 實(shí)現(xiàn)約瑟夫環(huán)的實(shí)例代碼
這一次是借鑒模仿別人寫的代碼,以前覺得不好將數(shù)據(jù)結(jié)構(gòu)的鏈結(jié)構(gòu)什么的遷移到j(luò)ava上來使用,但這一次確實(shí)讓我感受到了可以自己構(gòu)造數(shù)據(jù)結(jié)構(gòu),然后使用類似鏈的方式來解決約瑟夫環(huán),有所頓悟。不多說,繼續(xù)上代碼2013-10-10
淺談SpringBoot2.4 配置文件加載機(jī)制大變化
這篇文章主要介紹了淺談SpringBoot2.4 配置文件加載機(jī)制大變化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Linux環(huán)境卸載Centos7自帶的OpenJDK和安裝JDK1.8圖文教程
CentOS系統(tǒng)是開發(fā)者常用的Linux操作系統(tǒng),安裝它時(shí)會(huì)默認(rèn)安裝自帶的舊版本的OpenJDK,但在開發(fā)者平時(shí)開發(fā)Java項(xiàng)目時(shí)還是需要完整的JDK,這篇文章主要給大家介紹了關(guān)于Linux環(huán)境卸載Centos7自帶的OpenJDK和安裝JDK1.8的相關(guān)資料,需要的朋友可以參考下2024-07-07
JavaWeb頁面中防止點(diǎn)擊Backspace網(wǎng)頁后退情況
當(dāng)鍵盤敲下后退鍵(Backspace)后怎么防止網(wǎng)頁后退情況呢?今天小編通過本文給大家詳細(xì)介紹下,感興趣的朋友一起看看吧2016-11-11
SpringBoot?替換?if?的參數(shù)校驗(yàn)示例代碼
Spring?Validation是對hibernate?validation的二次封裝,用于支持spring?mvc參數(shù)自動(dòng)校驗(yàn),接下來,我們以spring-boot項(xiàng)目為例,介紹Spring?Validation的使用,需要的朋友可以參考下2022-12-12
詳解SpringBoot的jar為什么可以直接運(yùn)行
SpringBoot提供了一個(gè)插件spring-boot-maven-plugin用于把程序打包成一個(gè)可執(zhí)行的jar包,本文給大家介紹了為什么SpringBoot的jar可以直接運(yùn)行,文中有相關(guān)的代碼示例供大家參考,感興趣的朋友可以參考下2024-02-02
詳解Spring FactoryBean靈活創(chuàng)建復(fù)雜對象的秘密武器
FactoryBean是Spring框架中用于創(chuàng)建復(fù)雜Bean的接口,通過編程方式控制Bean的創(chuàng)建過程,它允許開發(fā)者自定義Bean的創(chuàng)建邏輯,適用于集成第三方庫、延遲初始化、動(dòng)態(tài)代理和統(tǒng)一管理資源等場景,本文介紹Spring FactoryBean創(chuàng)建復(fù)雜對象的相關(guān)操作,感興趣的朋友一起看看吧2025-02-02
Java編程用兩個(gè)棧實(shí)現(xiàn)隊(duì)列代碼分享
這篇文章主要介紹了Java編程用兩個(gè)棧實(shí)現(xiàn)隊(duì)列代碼分享,具有一定參考價(jià)值,這里給大家分享下,供需要的朋友了解。2017-10-10

