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

网友投稿 221 2023-05-14


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

前言

距离第一篇 Spring Boot 系列的博文 3 个月了。虽然 XML 形式是我比较推荐的,但是注解形式也是方便的。尤其一些小系统,快速的 CRUD 轻量级的系统。

这里感谢晓春 http://xchunzhao.tk/ 的 Pull Request,提供了 springboot-mybatis-annotation 的实现。

一、运行 springboot-mybatis-annotation 工程

然后Application 应用启动类的 main 函数,然后在浏览器访问:

http://localhost:8080/api/city?cityName=温岭市

可以看到返回的 jsON 结果:

{

"iCiAKhuJd": 1,

"provinceId": 1,

"cityName": "温岭市",

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

}

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

1.pom 添加 Mybatis 依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

springboot

springboot-mybatis-annotation

0.0.1-SNAPSHOT

jar

springboot-mybatis-annotation

Springboot-mybatis :: 整合Mybatis Annotation Demo

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

1.2.0

5.1.39

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

CiAKhuJ ${mybatis-spring-boot}

mysql

mysql-connector-java

${mysql-connector}

junit

junit

4.12

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

springboot

springboot-mybatis-annotation

0.0.1-SNAPSHOT

jar

springboot-mybatis-annotation

Springboot-mybatis :: 整合Mybatis Annotation Demo

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

1.2.0

5.1.39

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

CiAKhuJ ${mybatis-spring-boot}

mysql

mysql-connector-java

${mysql-connector}

junit

junit

4.12

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 包提供的,如图:

可以 git clone 下载工程 springboot-learning-example ,springboot-mybatis-annotation 工程代码注解很详细。 https://github.com/JeffLi1993/springboot-learning-example 。

以上所述是给大家介绍的Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解React开发中使用require.ensure()按需加载ES6组件
下一篇:ES6入门教程之Class和Module详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~