一、多表查询

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();

最新文章

  1. Sharing A Powerful Tool For Application Auto Monitor
  2. Eclipse SVN图标等设置
  3. 在ubuntu/deepin/mint等系统中使用命令删除文件或文件夹
  4. java源码分析:Arrays.sort
  5. Android应用层View绘制流程与源码分析
  6. springmvc 文件下传、上载、预览。以二进制形式存放到数据库(转载)
  7. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
  8. SQL2008安装后激活方式以及提示评估期已过解决方法(转)
  9. 基于Node.js的实时推送 juggernaut
  10. javaweb学习总结七(XML语言作用、语法)
  11. iOS_城市定位
  12. C#中经常使用的几种读取XML文件的方法
  13. Acdreamoj1115(数学思维称号)
  14. Maximum Depth of Binary Tree leetcode
  15. CentOS7搭建SVN服务器
  16. Spark(四十五):Schema Registry
  17. React对比Vue(02 绑定属性,图片引入,数组循环等对比)
  18. MySQL实现中文拼音排序
  19. GPS定位基本原理浅析
  20. Spring Cloud Zuul性能调整

热门文章

  1. python程序开机自启动
  2. 犀牛Rhino 7.0中文版安装破解教程
  3. SQL SERVER 雨量计累计雨量(小时)的统计思路
  4. Mariadb常用管理操作
  5. CentOS 7 安装虚拟机
  6. 手写Pascal解释器(三)
  7. Virustotal工具产品初研
  8. RHCSA_DAY02
  9. Android程序员如何通过跳槽薪资翻倍?
  10. 用传纸条讲 HTTPS