<?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. Linux命令-实时监测命令:watch

    watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行..在Linux下,watch是周期性 ...

  2. RabbitMQ概念及环境搭建(四)RabbitMQ High Availability

    #################################################### RabbitMQ High Availability #################### ...

  3. 如何在Access2007中使用日期类型查询数据

    select * from 表 where datediff("d",日期型的字段,#2008-09-01#)=0或select * from 表 where 日期型的字段 > ...

  4. OGG_GoldenGate数据库配置DDL同步(案例)

    2014-03-08 Created By BaoXinjian

  5. Objective-C中的@Property具体解释

    Objective-C中的@Property具体解释 @Property (属性) class vairs 这个属性有nonatomic. strong. weak, retain. copy等等 我 ...

  6. Ambari-stack介绍

    Ambari-stack总体介绍 Ambari-stack 表示hadoop某个发行版本号.比如HDP-1.0.0,在用ambari创建一个集群时,首先要通过调用restfulAPI设置stack版本 ...

  7. C#操作txt文件并清空添加操作

    C#操作txt文件,进行清空添加操作的例子.代码: //把txt清空 FileStream stream = File.Open(Adr,FileMode.OpenOrCreate,FileAcces ...

  8. Java NIO- Selector 使用示例

    Selector selector = Selector.open(); channel.configureBlocking(false); SelectionKey key = channel.re ...

  9. nyoj592 spiral grid

    spiral grid 时间限制:2000 ms  |  内存限制:65535 KB 难度:4   描述 Xiaod has recently discovered the grid named &q ...

  10. hdoj1010 Temperor of the bone

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...