久久96国产精品久久久-久久发布国产伦子伦精品-久久精品国产精品青草-久久天天躁夜夜躁狠狠85麻豆

技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

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

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-06-07 06:15┆點(diǎn)擊:

雖然 XML 形式是我比較推薦的,但是注解形式也是方便的。尤其一些小系統(tǒng),快速的 CRUD 輕量級(jí)的系統(tǒng)。

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

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

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

: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="" xmlns:xsi="" xsi:schemaLocation=" 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 包提供的,如圖:

Spring Boot整 Mybatis Annotation注解的完整Web案例 三聯(lián)

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