<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.witwicky.mapper.UserMap"> <insert id="insertUser" parameterType="com.witwicky.pojo.User">
<!-- 将自动生成的编号放入user对象中的id属性中 -->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select
LAST_INSERT_ID()
</selectKey>
insert into user (username, sex, birthday, address)
values
(#{username}, #{sex}, #{birthday}, #{address})
</insert> <delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where
id = #{id}
</delete> <update id="updateUser" parameterType="com.witwicky.pojo.User">
update user set name=
#{name} where id=#{id}
</update> <select id="selectAllUser" resultType="com.witwicky.pojo.User">
select * from user
</select> <select id="selectUserByUserName" parameterType="java.lang.String"
resultType="com.witwicky.pojo.User"> <!-- List<User> list = sqlSession.selectList("test.selectUserByUserName",
"张"); -->
select * from user where username like '%${value}%' <!-- List<User> list = sqlSession.selectList("test.selectUserByUserName",
"%张%"); -->
<!-- select * from user where name like #{name} -->
</select> <!-- 查询订单信息,包含用户信息 -->
<resultMap type="com.witwicky.pojo.Orders" id="findOrdersResultMap">
<id property="id" column="order_id" />
<result property="userId" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" /> <association property="user" javaType="com.witwicky.pojo.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="sex" column="sex" />
<result property="birthday" column="birthday" />
<result property="address" column="address" />
</association>
</resultMap>
<select id="findOrders" resultMap="findOrdersResultMap">
select u.*, o.id as order_id,
o.user_id, o.number, o.createtime, o.note from user as u, orders as o
where u.id = o.user_id
</select> <!-- 查询用户信息,包含订单 -->
<resultMap type="com.witwicky.pojo.User" id="findUserContainOrdersResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="sex" column="sex" />
<result property="birthday" column="birthday" />
<result property="address" column="address" /> <collection property="orders" ofType="com.witwicky.pojo.Orders">
<id property="id" column="order_id" />
<result property="userId" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
<select id="findUserContainOrders" resultMap="findUserContainOrdersResultMap">
select u.*, o.id as
order_id,
o.user_id, o.number, o.createtime, o.note from user as u,
orders as o
where u.id = o.user_id
</select> </mapper>

mybatis 一对多关系的更多相关文章

  1. mybatis一对多关系的关联查询

    问题描述:实现两张表的关联查询 学生表: 班级表: 要实现学生管理信息中有所在班级的名称,即如下图所示 1.对应学生表的pojo类写全班级表中的字段(适用于要连接的表字段较少的情况) sql语句直接在 ...

  2. MyBatis之级联——一对多关系

    上次我们讲到了MyBatis的一对一关系的表示,简单回顾一下一对一关系就是一个学生只有一个学生证.那么什么是一对多关系呢?一个学生有多个课程这就是一对多的关系.我们结合上一章中的学生和学生证,在此基础 ...

  3. Mybatis框架中实现双向一对多关系映射

    学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢 ...

  4. mybatis之级联关系(一对一、一对多)

    0. 表结构 1. 准备工作 1.1 配置文件等信息,请参考  myBatis之入门示例 1.2 entity 1.2.1 TPersonInfo.java package com.blueStarW ...

  5. Java基础-SSM之mybatis一对多和多对一关系映射

    Java基础-SSM之mybatis一对多和多对一关系映射 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表)  1>.创建customers表: ...

  6. MyBatis日记(五):一对一关系,一对多关系

    一对一关系: 首先在数据库中新增地址表(t_address)用于存储人员的地址信息: CREATE TABLE `t_address` ( `id` ) NOT NULL AUTO_INCREMENT ...

  7. MyBatis加强(1)~myBatis对象关系映射(多对一关系、一对多关系)、延迟/懒加载

    一.myBatis对象关系映射(多对一关系.一对多关系) 1.多对一关系: ---例子:多个员工同属于一个部门. (1)myBatis发送 额外SQL: ■ 案例:员工表通过 dept_id 关联 部 ...

  8. MyBatis——一对多、一对一关系,collection、association

    实体类两个: user类: package com.pojo; /** *用户 */ public class User{ private int userId;//用户ID private Stri ...

  9. 0050 MyBatis关联映射--一对多关系

    一对多关系更加常见,比如用户和订单,一个用户可以有多个订单 DROP TABLE IF EXISTS customer; /*用户表*/ CREATE TABLE customer( `pk` INT ...

随机推荐

  1. MVC笔记 Controller相关技术

    一.Controller的责任 MVC的核心就是Controller(控制器),它负责处理浏览器传送过来的所有请求,并决定要将什么内容响应给浏览器.但Controller并不负责决定内容应该如何显示, ...

  2. iPhone手机屏幕的尺寸180330更新

    以下是 iPhone的型号和对应的屏幕宽高 英寸  宽 高  厚度 3.5   320 480 4s      ipad   系列   4   320 568 5   5s   4.7  375 66 ...

  3. 工作总结 ModelState.AddModelError("ShiYiObject", "对象不能为空!"); 小知识

    // // 摘要: // 获取包含模型状态和模型绑定验证状态的模型状态字典对象. // // 返回结果: // 模型状态字典. public ModelStateDictionary ModelSta ...

  4. python学习笔记——守护进程

    1 基本描述 守护进程:是系统中独立的后台服务进程, 特点:独立与终端并且周期性地执行某个任务,其生命周期长,一般随系统启动和终止. 缺点:进程的创建和销毁的时候需要消耗较多的计算机资源. 2 参考 ...

  5. Mosquitto ---学习笔记

    Mosquitto的安装,可以参见:https://mosquitto.org/download/ [基础配置]Mosquitto服务的配置文件为/etc/mosquitto/mosquitto.co ...

  6. Android 判断SD卡是否存在及容量查询

    Android 判断SD卡是否存在及容量查询的简单方法如下: 首先要在AndroidManifest.xml中增加SD卡访问权限 <!-- 在SDCard中创建与删除文件权限 --> &l ...

  7. php 7.1安装教程

    一.下载地址 http://php.net/downloads.php#v7.1.9 IIS如果你使用的是PHP的FastCGI IIS,你应该使用非线程安全(NTS)版本的PHP. Apache请使 ...

  8. mysql-5.7 Using Asynchronous I/O on Linux详解

    一.mysql - innodb 使用异步IO的场景 总的来说innodb 只会对数据文件采用异步IO,为了保存日志是真正被写入到磁盘,innodb不会对日志文件启用异步IO 更新细一步的说,inno ...

  9. mysql-5.7 innodb 的并行任务调度详解

    一.innodb并行任务调度是什么: 这里要“考古”一下了,不然问题说不清楚.上大学的时候老师和我们说最初的计算机只有一个核心,并且一次也只能做一件事, 如果你有两件事要用到计算机,在第一件事没有做完 ...

  10. Chipscope使用

    作者:桂. 时间:2017-08-07  06:47:31 链接:http://www.cnblogs.com/xingshansi/p/7297482.html 前言 Chipscope在FPGA调 ...