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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 本文提纲 一、前言 二、运行 springboot-mybatis-annotation 工程 三、springboot-mybatis-annotation 工程配置详解 四、小结 运行环境:JDK 7 或 8、Maven 3.

摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!

『 公司需要人、产品、业务和方向,方向又要人、产品、业务和方向,方向… 循环』

本文提纲
一、前言
二、运行 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 函数,然后在浏览器访问:

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

可以看到返回的 JSON 结果:

1
2
3
4
5
6
{
"id" : 1,
"provinceId" : 1,
"cityName" : "温岭市" ,
"description" : "我的家在温岭。"
}

 

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

1.pom 添加 Mybatis 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<? 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 城市 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~

欢迎扫一扫我的公众号关注 — 及时得到博客订阅哦!
— http://www.bysocket.com/ —
— https://github.com/JeffLi1993 —

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
ssm使用全注解实现增删改查案例——web.xml
ssm使用全注解实现增删改查案例——web.xml
9 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
40 1
|
14天前
|
SQL XML Java
【mybatis】第二篇:@Select注解中加入字段判断
【mybatis】第二篇:@Select注解中加入字段判断
|
5天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
15 0
|
14天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
25天前
|
Java 数据库连接 mybatis
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
16 0
|
26天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
59 0
|
1月前
|
Java 数据库连接 网络安全
mybatis使用全注解的方式案例(包含一对多关系映射)
mybatis使用全注解的方式案例(包含一对多关系映射)
12 0
|
1月前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
12 0
|
1月前
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
12 0