前言
距离第一篇 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 结果:
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
|
<?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
|
/**
* 城市 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 案例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://www.tuicool.com/articles/q2eIzmF
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 71
-
2025-05-29 29
-
2025-05-29 89
-
2025-06-04 78
-
2025-05-25 54