Mybatis笔记(3)
一、多表查询
1.1 一对一查询
订单和用户(一个订单属于一个)
Order实体类有user属性
配置resultMap(OrderMap)
<select id="findAll" resultMap="orderMap">
SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
</select>
<resultMap id="orderMap" type="order">
<!--手动指定字段与实体属性的映射关系
column: 数据表的字段名称
property:实体的属性名称
-->
<id column="oid" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<!--<result column="uid" property="user.id"></result>
<result column="username" property="user.username"></result>
<result column="password" property="user.password"></result>
<result column="birthday" property="user.birthday"></result>-->
<!--
property: 当前实体(order)中的属性名称(private User user)
javaType: 当前实体(order)中的属性的类型(User)
-->
<association property="user" javaType="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
查询结果表:
1.2 一对多查询
订单和用户(一个用户下达多个订单)
User实体类有List属性
配置resultMap(UserMap)
<select id="findAll" resultMap="userMap">
SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
</select>
<resultMap id="userMap" type="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--配置集合信息
property:集合名称
ofType:当前集合中的数据类型
-->
<collection property="orderList" ofType="order">
<!--封装order的数据-->
<id column="oid" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
</collection>
</resultMap>
1.3 多对多查询
比一对多查询多一张表
<select id="findUserAndRoleAll" resultMap="userRoleMap">
SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
</select>
<resultMap id="userRoleMap" type="user">
<!--user的信息-->
<id column="userId" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--user内部的roleList信息-->
<collection property="roleList" ofType="role">
<id column="roleId" property="id"></id>
<result column="roleName" property="roleName"></result>
<result column="roleDesc" property="roleDesc"></result>
</collection>
</resultMap>
小结
MyBatis多表配置方式:
一对一配置:使用做配置
一对多配置:使用+做配置
多对多配置:使用+做配置
二、MyBatis注解开发
2.1 常用注解
注解 | 目标 | 对应的XML标签 |
---|---|---|
@CacheNamespace | 类 | |
@CacheNamespaceRef | 类 | |
@Results | 方法 | |
@Result | 方法 | |
@One | 方法 | |
@Many | 方法 | |
@Insert@Update@Delete | 方法 | |
@InsertProvider@UpdateProvider@DeleteProvider@SelectProvider | 方法 | 允许创建动态SQL |
@Param | 参数 | N/A |
@Options | 方法 | 映射语句的属性 |
@select | 方法 |
2.1 简单查询
@Insert 简单插入
@Insert(" insert into user(name,sex,age) values(#{name},#{sex},#{age} " )
int saveUser(User user);
@Update 简单更新
@Update("update user set name= #{name} ,sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
@Delete 简单删除
@Delete("delete from user where id =#{id} ")
void deleteById(Integer id);
@Select 简单查询
(一对一第一种方法)
@Select(" Select * from user ")
@Results({
//id = true代表主键
@Result(id = true, column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "tel", property = "tel"),
@Result(column = "birth", property = "birth"),
@Result(column = "address", property = "address")
})
List<User> queryAllUser()
@One
(一对一第二种方法)
@Select(" select * from orders ")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "ordertime",property = "ordertime",
@Result(
property = "user", //要封装的属性名
column="uid",// 根据哪个字段去查询user表数据
javaType = User.class,//要封装的实体类型
//select属性 代表查询哪个接口的方法获得数据
//one指示我们,查询出来的结果只有一个。
one = @One(select="gyb.UserMapper.findById")
)
})
public List<Order> findAll();
@Many
一对多
User内含有orderList
@Select(" select * from dept")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "password", property = "password"),
@Result(
//封装user内的userlist属性
property = "orderList",
//数据库内字段
column = "id",
//结果类型
javaType = "List.class",
//使用方法,代表查询多个结果
many = @Many(select = "gyb.OrderMapper.findById")
)
})
public List<User> findUserAndOrderAll();
Mybatis笔记(3)的更多相关文章
- Mybatis笔记二:接口式编程
目录 旧方法的弊端 接口式编程 接口式编程的好处 接口式编程的增删改查 旧方法的弊端 在Mybatis笔记一中,我们使用命名空间+id的方式实现了Mybatis的执行,不过这里的命名空间是我们随便写的 ...
- MyBatis笔记二:配置
MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...
- MyBatis笔记一:GettingStart
MyBatis笔记一:GettingStart 1.MyBatis优点 我们的工具和各种框架的作用就是为了我们操作数据库简洁,对于一些数据库的工具能帮我们少写一些处理异常等等的代码,但是他们并不是自动 ...
- 【MyBatis笔记】mapper文件的配置以及说明
<!doctype html>[MyBatis笔记]mapper文件的配置以及说明 figure:last-child { margin-bottom: 0.5rem; } #write ...
- mybatis笔记2 基础理论准备
之前发了一篇mybatis的crud入门笔记,算是入门了,为了让功力加深一级,来研究下mybatis的理论知识,哈哈,以后好拿来跟技术经理吹吹牛- 按照问题来吧!个人觉得有自主意识,带着自己的问题来研 ...
- mybatis笔记<二> 整合spring
mybatis与spring整合需要添加几个jar包,mybatis-spring, spring-context, spring-jdbc 1. spring ioc只要一个jar包就ok 2. 我 ...
- mybatis笔记<一> Demo
mybatis作为一个orm互联网公司基本都在用,今天写个笔记.记录一下mybatis使用 参考官网:http://www.mybatis.org/mybatis-3/getting-started. ...
- 【狂神说】JAVA Mybatis 笔记+源码
简介 自学的[狂神JAVA]MyBatis GitHub源码: https://github.com/Donkequan/Mybatis-Study 分享自写源码和笔记 配置用的 jdk13.0.2 ...
- mybatis笔记3 一些原理的理解
1,mybatis流程跟踪,原理理解 基本思路: 从SqlSessionFactory的初始化出发,观察资源的准备和环境的准备,以及实现持久层的一些过程: 进入SqlSessionFactoryBea ...
- mybatis笔记1 基本的配置和操作
mybatis比较轻量,适合开发比较小型的或者业务比较复杂的系统: 相对于hibernate来说可以灵活的写sql,更灵活的处理遇到的业务逻辑: 可以说hibernate是pojo实体对db的orm映 ...
随机推荐
- 深入jvm虚拟机--第一篇 void TemplateInterpreterGenerator::generate_and_dispatch(Template* t, TosState tos_out) 函数
今天第一次使用虚拟姐打断点,断点设置在了void TemplateInterpreterGenerator::generate_and_dispatch(Template* t, TosState t ...
- 云平台制作(1)-OPC Client取数模块的制作
近来由于工程需要,基于OPC DA 2.0搭建通用的取数模块,与远程webscoket服务端连接,并传输数据.在网上找了些资料,修改相应网友公开的源代码,基本达到要求,特供大家参考. 1.实体类 us ...
- Intouch/ifix关于语音报警的一种设置思路
工控项目最近升级改造,需要使用Intouch/ifix提供一个语音报警功能.这个不像先前提供的单一的声音报警,业主方要求能详细的提供某某水泵或者是某某设备故障报警,这就要求我们这边对语音解析或者基础控 ...
- vue中this.$router.push()路由传值和获取的两种常见方法
1.路由传值 this.$router.push() (1) 路由跳转使用router.push()方法,这个方法会向history栈添加一个新纪录,所以,当用户点击浏览器后退按钮时,会回到之前的 ...
- 移动APP我们需要关注什么
移动APP关注的点比web或者PC上的程序更多 1.测试用例的设计 移动互联网的快节奏,要放弃传统的测试用例编写方式,不需要写详细的测试用例,采用罗列测试点的方式如思维导图,这样既节省时间又能够直观清 ...
- TypeScript学习笔记(四)装饰器
目录 一.装饰器的作用 二.类装饰器 1. 普通装饰器 为类扩展属性和方法 使用装饰器修改属性和重写方法 2. 装饰器工厂 三.属性装饰器 四.方法装饰器 使用方法装饰器对方法进行扩展 五.方法参数装 ...
- pikachu 目录遍历 敏感信息泄露
目录遍历漏洞概述在web功能设计中,很多时候我们会要将需要访问的文件定义成变量,从而让前端的功能变的更加灵活. 当用户发起一个前端的请求时,便会将请求的这个文件的值(比如文件名称)传递到后台,后台再执 ...
- CVE-2020-17523:Apache Shiro身份认证绕过漏洞分析
0x01 Apache Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 0x02 漏洞简介 2021年2月1日,Apache Shiro官 ...
- 我的微服务之路,看我搭建dapr趟过的坑
前言 自从上周看了一个Dapr的视频,知道原来自己离微服务很近,简直触手可及. 心痒痒好久了,不动手实践验证一下简直是寝食难安.先是看官网的文档,可能是因为被墙了,有些网址是不能访问的,那安装搭建环境 ...
- NOIP 模拟 9 考试总结
T1 一道推规律的题,没想出来,暴力打得常数还太大了,挂了不少 题解 T2 这是一道二分题,很巧妙,但是对于想我一样懒得人,那个数据结构就水过去了 (裸的分块加强大的卡长和合适的块的大小可以卡过去) ...