自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

企業(yè)動態(tài)
Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例希望對大家有幫助。

[[191070]]

本文提綱

一、前言

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

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

運(yùn)行環(huán)境:JDK 7 或 8、Maven 3.0+

技術(shù)棧:SpringBoot 1.5+、SpringBoot Mybatis Starter 1.2+ 、MyBatis 3.4+

「前言」

距離***篇 Spring Boot 系列的博文 3 個月了?!禨pringboot 整合 Mybatis 的完整 Web 案例》***篇出來是 XML 配置 SQL 的形式。雖然 XML 形式是我比較推薦的,但是注解形式也是方便的。尤其一些小系統(tǒng),快速的 CRUD 輕量級的系統(tǒng)。

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

由于這篇文章和 《Springboot 整合 Mybatis 的完整 Web 案例》 類似,所以運(yùn)行這塊環(huán)境配置,大家參考另外一篇兄弟文章。

然后,Application 應(yīng)用啟動類的 main 函數(shù),然后在瀏覽器訪問:http://localhost:8080/api/city?cityName=溫嶺市

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

  1.     "id": 1, 
  2.     "provinceId": 1, 
  3.     "cityName""溫嶺市"
  4.     "description""我的家在溫嶺。" 

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

1.pom 添加 Mybatis 依賴

  1. <!-- Spring Boot Mybatis 依賴 --> 
  2. <dependency> 
  3.     <groupId>org.mybatis.spring.boot</groupId> 
  4.     <artifactId>mybatis-spring-boot-starter</artifactId> 
  5.     <version>${mybatis-spring-boot}</version> 
  6. </dependency> 

2.在 CityDao 城市數(shù)據(jù)操作層接口類添加注解 @Mapper、@Select 和 @Results

  1. /** 
  2.  * 城市 DAO 接口類 
  3.  * 
  4.  * Created by xchunzhao on 02/05/2017. 
  5.  */ 
  6. @Mapper // 標(biāo)志為 Mybatis 的 Mapper 
  7. public interface CityDao { 
  8.     /** 
  9.      * 根據(jù)城市名稱,查詢城市信息 
  10.      * 
  11.      * @param cityName 城市名 
  12.      */ 
  13.     @Select("SELECT * FROM city"
  14.     // 返回 Map 結(jié)果集 
  15.     @Results({ 
  16.             @Result(property = "id"column = "id"), 
  17.             @Result(property = "provinceId"column = "province_id"), 
  18.             @Result(property = "cityName"column = "city_name"), 
  19.             @Result(property = "description"column = "description"), 
  20.     }) 
  21.     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。

【本文為51CTO專欄作者“李強(qiáng)強(qiáng)”的原創(chuàng)稿件,轉(zhuǎn)載請通過51CTO聯(lián)系作者獲取授權(quán)】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來源: 51CTO專欄
相關(guān)推薦

2017-10-17 15:14:33

Spring BooThymeleafWeb

2017-08-02 14:44:06

Spring Boot開發(fā)注解

2021-11-10 11:37:48

Spring整合 Mybatis

2020-11-09 10:16:41

Mybatis

2023-06-02 16:24:46

SpringBootSSM

2022-11-15 08:10:23

SpringMyBatis底層

2021-04-13 20:24:57

Spring Boot注解spring

2009-09-22 11:30:57

2022-12-23 08:28:42

策略模式算法

2021-03-01 07:55:41

Java annotation注解

2021-06-07 08:39:58

SpringBootMyBatisMapper

2017-04-17 10:35:40

Spring BooRedis 操作

2022-07-21 11:04:53

Swagger3Spring

2024-10-14 17:18:27

2011-07-19 09:38:41

一維數(shù)組Junit靜態(tài)成員

2021-09-08 10:23:08

讀寫分離Java數(shù)據(jù)庫

2023-04-17 23:49:09

開發(fā)代碼Java

2020-11-02 07:00:29

Spring Boo注解自動化

2020-12-31 07:55:33

spring bootMybatis數(shù)據(jù)庫

2025-02-26 08:03:17

SpringJPAMyBatis
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號