mybatis自动映射和手动映射
2024-08-29 14:26:56
一对一查询
第一种方法:
<!-- 查询所有订单信息 -->
<select id="findOrdersList" resultType="cn.itcast.mybatis.po.OrdersCustom">
SELECT
orders.*,
user.username,
user.address
FROM
orders, user
WHERE orders.user_id = user.id
</select>
第二种方法:
<!-- 查询订单关联用户信息使用resultmap -->
<resultMap type="cn.itheima.po.Orders" id="orderUserResultMap">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 一对一关联映射 -->
<!--
property:Orders对象的user属性
javaType:user属性对应 的类型
-->
<association property="user" javaType="cn.itcast.po.User">
<!-- column:user表的主键对应的列 property:user对象中id属性-->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findOrdersWithUserResultMap" resultMap="orderUserResultMap">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
o.note,
u.username,
u.address
FROM
orders o
JOIN `user` u ON u.id = o.user_id
</select>
一对多查询
<resultMap type="cn.itheima.po.user" id="userOrderResultMap">
<!-- 用户信息映射 -->
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<!-- 一对多关联映射 -->
<collection property="orders" ofType="cn.itheima.po.Orders">
<id property="id" column="oid"/>
<!--用户id已经在user对象中存在,此处可以不设置-->
<!-- <result property="userId" column="id"/> -->
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</collection>
</resultMap>
<select id="getUserOrderList" resultMap="userOrderResultMap">
SELECT
u.*, o.id oid,
o.number,
o.createtime,
o.note
FROM
`user` u
LEFT JOIN orders o ON u.id = o.user_id
</select>
最新文章
- Twitter面试题蓄水池蓄水量算法(原创 JS版,以后可能会补上C#的)
- 如何使用JS来检测游览器是什么类型,或android是什么版本号- 转载
- svn提交报e200007错误
- 改变Android ProgressBar样式颜色
- 使用dig查询dns解析
- iOS开发代码规范
- Codewars编辑题--今天升到了7段
- 新型信用卡MasterPass
- ZOJ(ZJU) 1002 Fire Net(深搜)
- Linux下MySQL 5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)
- Yii的URL助手
- 初探linux子系统集之timer子系统(二)
- 聊聊 Spring Boot 2.x 那些事儿
- ios 开发视图界面动态渲染
- spring boot 整合js css 静态文件
- 部署KVM
- Python pickle模块
- 洛谷P4147 玉蟾宫 单调栈/悬线法
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
- CodeIgniter 安装指导