您的位置:首页 > 编程语言 > Java开发

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

2017-12-07 15:21 609 查看
一、运行 springboot-mybatis-annotation
工程

由于这篇文章和 《Springboot
整合 Mybatis 的完整 Web 案例》 类似,所以运行这块环境配置大家参考另外一篇兄弟文章。

然后Application 应用启动类的 main 函数,然后在浏览器访问:
http://localhost:8080/api/city?cityName=温岭市
可以看到返回的 JSON 结果:

{

"id": 1,

"provinceId": 1,

"cityName": "温岭市",

"description": "我的家在温岭。"

}

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

1.pom 添加 Mybatis 依赖

<!-- Spring Boot Mybatis 依赖 -->

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>${mybatis-spring-boot}</version>

</dependency>

2.在 CityDao 城市数据操作层接口类添加注解 @Mapper、@Select 和 @Results

/**

* 城市 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 包提供的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: