springboot---->集成mybatis开发(二)
这里面我们介绍一下springboot集成mybatis完成一对多数据和一对一数据的功能。任何一个人离开你 都并非突然做的决定 人心是慢慢变冷 树叶是渐渐变黄 故事是缓缓写到结局 而爱是因为失望太多 才变成不爱。
springboot集成mybatis
我们创建两张表分别为:person表和comment表。我们的建表语句和插入脚本如下:对应关系是一个用户可以有多个评论
create TABLE person(
person_id int(11) PRIMARY KEY ,
person_name VARCHAR(45) not NULL
);
INSERT INTO person(person_id, person_name) VALUES (1, "huhx"); create table comment(
comment_id int(11) PRIMARY key,
person_id int(11) not null,
content VARCHAR(45) not null,
comment_time DATETIME not NULL
);
INSERT INTO comment(comment_id, person_id, content, comment_time) VALUES (1, 1, "content huhx", now());
INSERT INTO comment(comment_id, person_id, content, comment_time) VALUES (2, 1, "content java", now());
INSERT INTO comment(comment_id, person_id, content, comment_time) VALUES (3, 1, "content golang", now());
现在我们开始我们的代码测试,首先定义我们的实体bean类。
一、对应数据库表的实体类
表的设计可能不是特别的合理,这里面主要是学习mybatis一对多关系的知识。
- Comment:评论表
public class Comment {
private int commentId;
private String content;
private Date commentTime;
}
- Person:用户表
public class Person {
private int personId;
private String personName;
}
- PersonComment:用户的评论表
public class PersonComment {
private Person person;
List<Comment> comments;
}
二、我们的控制器测试类
@RequestMapping("/mybatis")
@RestController
public class MyBatisBeanAction {
private static Logger logger = LoggerFactory.getLogger(MyBatisBeanAction.class);
@Resource
private SqlSession sqlSession;
@GetMapping("getComment")
public ResponseBean<PersonComment> getPersonComments(@RequestParam("personId") int personId) {
PersonComment personComment = this.sqlSession.selectOne("user.queryPersonComments", personId);
return ResultUtil.success(personComment);
}
}
user.queryPersonComments的mybatis语句如下:
<resultMap id="personCommentMap" type="PersonComment">
<id column="person_id"/><!--这条语句不能省略,否则查询出三条。会electOne在返回处报错..-->
<association property="person" column="person_id" javaType="Person">
<id property="personId" column="person_id"/>
<result property="personName" column="person_name"/>
</association>
<collection property="comments" ofType="Comment">
<id property="commentId" column="comment_id"/>
<result property="content" column="content"/>
<result property="commentTime" column="comment_time"/>
</collection>
</resultMap>
<!-- 一对多的bean映射的写法 -->
<select id="queryPersonComments" parameterType="int" resultMap="personCommentMap">
SELECT
a.person_id,
a.person_name,
b.comment_id,
b.content,
b.comment_time
FROM
person a, comment b
WHERE
a.person_id = b.person_id AND
a.person_id = #{personId}
ORDER BY
a.person_id DESC
</select>
我们在浏览器发送get请求:http://localhost:9998/mybatis/getComment?personId=1。控制台发送的sql语句日志如下:
::19.281 [http-nio--exec-] DEBUG user.queryAllUserInfo_COUNT - ==> Preparing: SELECT count() FROM puser
::19.282 [http-nio--exec-] DEBUG user.queryAllUserInfo_COUNT - ==> Parameters:
::19.285 [http-nio--exec-] DEBUG user.queryAllUserInfo_COUNT - <== Total:
::19.286 [http-nio--exec-] DEBUG user.queryAllUserInfo - ==> Preparing: SELECT userId, username, password, address, phoneNumber, birthday, sex FROM puser ORDER BY userId DESC LIMIT ?, ?
::19.286 [http-nio--exec-] DEBUG user.queryAllUserInfo - ==> Parameters: (Integer), (Integer)
::19.290 [http-nio--exec-] DEBUG user.queryAllUserInfo - <== Total:
可以看到查询出三条数据,但是封闭到一个实体bean里面。这个对于之前总是用map接收sql查询返回的数据来说,确实有一些想法。之前用map的话,没有用Bean这样很好的表现数据模型。其次类似于这种的查询,返回的就是一个list列表。最后感觉用Bean的话,可以使用@Valid去对数据进行校验,这个挺不错的。这种方式返回的数据:
{
"returnCode": ,
"returnMessage": "成功",
"response": {
"person": {
"personId": ,
"personName": "huhx"
},
"comments": [
{
"commentId": ,
"content": "content huhx",
"commentTime":
},
{
"commentId": ,
"content": "content java",
"commentTime":
},
{
"commentId": ,
"content": "content golang",
"commentTime":
}
]
}
}
我们修改为List的返回格式,具体的修改内容如下:
@GetMapping("getCommentMap")
public ResponseBean<List<Map<String, Object>>> getPersonCommentsMap(@RequestParam("personId") int personId) {
List<Map<String, Object>> lists = this.sqlSession.selectList("user.queryPersonCommentsMap", personId);
return ResultUtil.success(lists);
}
<select id="queryPersonCommentsMap" parameterType="int" resultType="map">
SELECT
a.person_id,
a.person_name,
b.comment_id,
b.content,
b.comment_time
FROM
person a, comment b
WHERE
a.person_id = b.person_id AND
a.person_id = #{personId}
ORDER BY
a.person_id DESC
</select>
修改之后返回的结果如下:可以看到返回的数据中comment_time和person_name重复了多次,而且数据list越多的时候,重复的次数就越大。最后对比这两种的返回,可以得到用map的确不能很好的反应数据模型的关系。以后还是用Bean去映射返回的数据吧,之前一直认为用map特别的方便和灵活。
{
"returnCode": ,
"returnMessage": "成功",
"response": [
{
"comment_time": ,
"person_name": "huhx",
"comment_id": ,
"content": "content huhx",
"person_id":
},
{
"comment_time": ,
"person_name": "huhx",
"comment_id": ,
"content": "content java",
"person_id":
},
{
"comment_time": ,
"person_name": "huhx",
"comment_id": ,
"content": "content golang",
"person_id":
}
]
}
友情链接
springboot---->集成mybatis开发(二)的更多相关文章
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- 在springboot中集成mybatis开发
在springboot中利用mybatis框架进行开发需要集成mybatis才能进行开发,那么如何在springboot中集成mybatis呢?按照以下几个步骤就可以实现springboot集成myb ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...
- SpringBoot集成MyBatis的Bean配置方式
SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- SpringBoot集成MyBatis底层原理及简易实现
MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...
- SpringBoot集成MyBatis小记
SpringBoot集成MyBatis小记 参考MyBatis官网 1. 添加maven依赖 添加到pom.xml <dependency> <groupId>org.myba ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
随机推荐
- Unity-------------------------关于GUI绘制的编程
转载:在这篇文章中我将给读者介绍Unity中的图形用户界面(GUI)编程.Unity有一个非常强大的GUI脚本API.它允许你使用脚本快速创建简单的菜单和GUI. 简介 Unity提供了使用脚本创建G ...
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过注解@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 ...
- com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: 为队列管理器提供的安全性认证无效
com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: 为队列管理器“zm_queue_manager”提供的安全性认证无效, ...
- Jenkins的安装配置[转]
Jenkins的安装配置 一.Jenkins简介 Jenkins 是一个可扩展的持续集成引擎.Jenkins可以帮我们将代码进行统一的编译打包.还可以放到tomcat容器中进行发布.简单来说就是我们通 ...
- 图像处理、计算机视觉与模式识别“SCI期刊和顶级会议”总结
期刊: best (1) IEEE Transactions on Pattern Analysis and Machine Intelligence,IEEE模式分析与机器智能汇刊,简称PAMI,是 ...
- myeclipse中项目上传到cvs注意事项
上传工程时检查cvsignore文件,如果有该文件直接删掉再上传.这样才能保证下载下来的文件保持原上传的目录结构.
- POI简易帮助文档系列--读取Excel文件
上篇博客通过简单的几行代码就学会了POI新建Excel文档的使用,本篇博客也从简单出发,通过查看POI的官网文档和一个简单的代码实例,学习怎么遍历出一个Excel文档的内容. package com. ...
- 非抢占式RCU中的一些概念
该记录着重介绍下:2.6.34版本中非抢占式RCU的基本概念. RCU保护的是指针,因为指针的赋值可以使用原子操作完成: 在非抢占式RCU中: 对于读者,RCU仅需要抢占失效,因此获得读锁和释放读锁分 ...
- windows xp\2003 之上的操作系统多启动(多系统)引导
概要技术: 微软自windows vista以来的操作系统引导bootmgr是真的很强大,只是因为其全底层的命令操作,且不友好的命令帮助让人望而却步! 基本技术概要提点: boot.ini 支持:xp ...
- 解决计算机改名无法连接TFS的问题
闲着没事改了下计算机名字,结果造成TFS无法连接. 报错讯息如下: ---------------------------Microsoft Visual Studio---------------- ...