Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
来源:互联网 发布:2001太空漫游小说知乎 编辑:程序博客网 时间:2024/05/16 12:15
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!
『 公司需要人、产品、业务和方向,方向又要人、产品、业务和方向,方向... 循环』
原文:https://www.bysocket.com/?p=1811
本文提纲
一、前言
二、运行 springboot-mybatis-annotation 工程
三、springboot-mybatis-annotation 工程配置详解
四、小结
运行环境:JDK 7 或 8、Maven 3.0+
技术栈:SpringBoot 1.5+、SpringBoot Mybatis Starter 1.2+ 、MyBatis 3.4+
前言
距离第一篇 Spring Boot 系列的博文 3 个月了。《Springboot 整合 Mybatis 的完整 Web 案例》第一篇出来是 XML 配置 SQL 的形式。虽然 XML 形式是我比较推荐的,但是注解形式也是方便的。尤其一些小系统,快速的 CRUD 轻量级的系统。
这里感谢晓春 http://xchunzhao.tk/ 的 Pull Request,提供了 springboot-mybatis-annotation 的实现。
一、运行 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 依赖
<?
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 启动父依赖 -->
<
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 连接驱动依赖 -->
<
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 城市数据操作层接口类添加注解 @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。
四、小结
注解不涉及到配置,更近贴近 0 配置。再次感谢晓春 http://xchunzhao.tk/ 的 Pull Request~
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- Spring Boot 整合 Thymeleaf 完整 Web 案例
- Spring Boot 整合 Thymeleaf 完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- spring整合mybatis完整案例(注解配置方法)
- spring boot 整合mybatis 中用注解实现的查询
- Spring boot入门,整合mybatis开发案例
- Spring boot Mybatis 整合(注解版)
- WEB开发----spring boot与mybatis整合
- spring整合mybatis完整案例(配置文件方式)
- Spring Boot注解(annotation)列表
- OpenCV中Adaboost训练的经验总结
- 11月8日学习心得及笔记
- win10企业版安装sqlserver2008数据库引擎失败解决方法
- 大端和小端的判断方法
- JDBC和JDBCtemplate区别
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- spring boot devtools 配置无效
- 三步搭建Python可视化开发环境
- 完美解决微信video视频隐藏控件和内联播放问题
- SpringBoot开发Junit单元测试方法
- 为什么要使用 BigDecimal
- 简单实现手机号验证码注册功能
- Net-snmp总结(六)-net-snmp源码分析
- 树状数组 详解