摘要: 原创出处 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 —

Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例的更多相关文章

  1. Spring Boot整合MyBatis(非注解版)

    Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...

  2. spring boot整合mybatis基于注解开发以及动态sql的使用

    让我们回忆一下上篇博客中mybatis是怎样发挥它的作用的,主要是三类文件,第一mapper接口,第二xml文件,第三全局配置文件(application.properties),而今天我们就是来简化 ...

  3. Spring Boot整合Mybatis并完成CRUD操作

    MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...

  4. spring boot 整合 mybatis 以及原理

    同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...

  5. Spring Boot整合Mybatis完成级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  6. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  7. Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题

    现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...

  8. Spring Boot系列(三):Spring Boot整合Mybatis源码解析

    一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...

  9. 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?

    Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...

随机推荐

  1. myeclipse取消js校验

    最近玩一个新的项目,项目里面集成了别的项目,在从SVN上第一次荡下来的时候编译的时候老是校验jq文件,老是被卡主,设置myeclipse环境的时候我已经取消了所有的js校验了,但是还是不行.恼火之余, ...

  2. Asp.net core 2.0.1 Razor 的使用学习笔记(一)

    环境:vs2017 版本:15.5.6 一.新建项目 1.文件>新建>项目>Visual c#>.NET Core>ASP.NET Core Web应用程序(“.NET ...

  3. PHP 运行 php-fpm 报错

      报错如下: [27-Aug-2017 18:34:23] WARNING: Nothing matches the include pattern '/usr/local/php/etc/php- ...

  4. C# 多线程复习笔记

    编码的日子其实也有一段时间了,但是,作为一个客户端程序,因为自己是做游戏开发的,一直没有对线程这个概念比较模糊吧. 记录下线程的整理学习路线.原文:http://www.cnblogs.com/min ...

  5. 【转】GPS基线的精化处理

    影响基线解算结果的因素主要有: (1)基线解算时所设定的起点坐标不准确. 起点坐标不准确,会导致基线出现尺度和方向上的偏差,造成的影响目前还没有较容易的方法来加以判别,因此,在实际工作中,只有尽量提高 ...

  6. MySQL--REPEATABLE-READ隔离级别下读取到的“重复数据”

    在MySQL中,使用MVCC来实现REPEATABLE-READ隔离级别,由于SELECT操作不会对数据加锁,其他回话可以修改当前回话所读取过的数据而不会被阻塞,因此读写不冲突. 在MVCC并发控制中 ...

  7. C#中的out参数/ref参数/params可变参数

    out参数: out关键字 通过引用来传递参数,在定义方法和调用方法的时候都必须使用out关键字 简单来讲out可以用来返回多个参数类型. static void Main(string[] args ...

  8. css模板

    最近好多人问我博客的css模板.... 现在是高三,没多少时间,趁放假赶紧更一下 主体就是把博客园的一个模板改动了一点 上面的图片特效,也是从别人那里得到的代码,大致就是下面那些,下面的三个图片换成自 ...

  9. 11个phpstorm 快捷键介绍

    11个phpstorm 快捷键介绍 http://phpstorm.tips/tips/11-toggling-case

  10. ECS的配置与使用

    登录阿里云ECS,系统是centos7.2 在linux下通过useradd方式创建新用户,切换到该用户权限,发现-bash-4.2$ . 解决方法:先查看进程,关闭相关进程.然后使用useradd ...