mybatis中虽然有外键但是查询的字段就是主键表的字段,没有必要JOIN表查询
sql语句
<!-- 根据用户id查询某一个人的寻宝 -->
<select id="selectTreasureLogListByUseId" parameterType="string"
resultMap="huntTreasureLogResult">
SELECT h.id,h.userId,h.treasureId,h.huntedCount FROM
hunt_treasure_log h
WHERE userId=#{userId}
</select>
resultMap
<!-- hunt_treasure_log查询HuntTreasureLog对象时的huntTreasureLogResult -->
<resultMap type="user" id="treasureLogUserResult">
<id property="id" column="userId" />
</resultMap>
<resultMap type="huntTreasureLog" id="huntTreasureLogResult">
<id property="id" column="id" />
<result property="huntedCount" column="huntedCount" />
<association property="treasure" column="id" javaType="treasure"
resultMap="treasureResult">
</association>
<association property="userByUserId" column="id" javaType="user"
resultMap="treasureLogUserResult">
</association>
</resultMap>
值得注意的就是列名要对应统一
使用resultMap关联4个表查询
<!-- ++++++++++++宝贝交换记录exchange_record表CRUD部分++++++++++++ -->
<!-- start: exchage_record查询ExchageRecord对象时的exchangeRecordResult -->
<resultMap type="user" id="recordHostUserResult">
<id property="id" column="hostUserId" />
<result property="userName" column="hostUserName" />
<result property="headImagePath" column="hostUserHeadImg" />
</resultMap>
<resultMap type="user" id="recordFriendUserResult">
<id property="id" column="friendUserId" />
<result property="userName" column="friendUserName" />
<result property="headImagePath" column="friendUserHeadImg" />
</resultMap>
<resultMap type="chip" id="recordHostChipResult">
<id property="id" column="hostChipId" />
</resultMap>
<resultMap type="chip" id="recordFriendChipResult">
<id property="id" column="friendChipId" />
</resultMap>
<resultMap type="huntTreasureLog" id="exchangeRecordResult">
<id property="id" column="id" />
<result property="huntedCount" column="huntedCount" />
<association property="hostUserId" column="id" javaType="user"
resultMap="recordHostUserResult">
</association>
<association property="friendUserId" column="id" javaType="user"
resultMap="recordFriendUserResult">
</association>
<association property="hostChipId" column="id" javaType="chip"
resultMap="recordHostChipResult">
</association>
<association property="friendChipId" column="id" javaType="chip"
resultMap="recordFriendChipResult">
</association>
</resultMap>
<!-- end: exchage_record查询ExchageRecord对象时的exchangeRecordResult -->
<!-- 插入一条交换记录 -->
<insert id="insertExchangeRecord" parameterType="exchangeRecord">
INSERT
exchange_record
(hostUserId,hostChipId,friendUserId,friendChipId,createDate)
VALUES(#{hostUserId.id},#{hostChipId.id},#{friendUserId.id},#{friendChipId.id},NOW())
</insert>
<!-- 根据id删除一条记录 -->
<delete id="deleteExchangeRecord" parameterType="string">
DELETE FROM
exchange_record WHERE id=#{exchangeRecordId}
</delete>
<!-- 获取交换记录作为提示信息给用户 -->
<select id="selectExchangeRecordList" parameterType="string"
resultMap="exchangeRecordResult">
SELECT e.id,
hostUser.id AS hostUserId,host.headImagePath AS hostUserHeadImg,
friendUser.id AS friendUserId,friendUser.headImagePath AS friendUserHeadImg,
hostChip.id AS hostChipId,friendChip.id AS friendChipId
FROM exchange_record e
LEFT
OUTER JOIN user hostUser
ON
e.houstUserId=hostUser.id
LEFT
OUTER JOIN user friendUser
ON
e.friendUserId=friendUser.id
LEFT
OUTER JOIN chip hostChip
ON
e.hostChipId=hostChip.id
LEFT
OUTER JOIN chip friendChip
ON
e.friendChipId=friendChip.id
WHERE
friendUserId=#{friendUserId}
</select>
.
分享到:
相关推荐
mybatis-generator插件生成带表字段注释的实体类,避免了手动来回复制的问题。 生成的实体自动包含数据表对应的注释。
MybatisGenerator本身是没有提供查询指定字段的,例如数据库有5个字段,我想查询其中3个字段的全部记录,这是做不到的。经过研究MybatisGenerator实现原理,在保证原有功能的基础上,实现了生成查询指定字段的相关...
主要给大家介绍了关于mybatis拦截器实现通用权限字段添加的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mybatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
修改mybatis-generator源码实现生成后的实体类字段上方加上数据库中填写字段的注释,有使用方法,可以在配置文件generatorConfig.xml中根据自己的需要进行修改
主要介绍了mybatis使用char类型字段查询oracle数据库时结果返回null问题的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
mybatis多条件查询处理方案(查询条件为数据库字段非传递参数)
修改 mybatis-generate ...搜了出来都是需要修改mybatis-generator-core 的源码,有方法在,但是就是没有现成的修改完成的文件,然后就根据教程改了一个,大神的教程再这,如果想动手的话可以按照他的教程修改一下...
Mybatis框架下,数据库更新操作时只更新set过的字段,原1.0版本由于CSDN不支持修改已经删除了。
mybatis-plus的多表插件完全按照mybats plus的用法来做,支持一对一返回映射,多对多返回映射,自定义返回Vo,自定义表别名,自定义查询字段,你只要会mp,那你就会用mpj,无感引入,不会对之前的业务产生一点点影响...
主要实现向mybatis传入一个表名、map集合、页数、行数,然后实现将map的key作为查询字段名,value作为相应字段值的通用分页查询操作。
支持连表查询的mybatis-plus,mybatis-plus风格的连表操作提供 wrapper.leftJoin() wrapper.rightJoin()等操作
Mybatis-generator自动生成代码时候提取数据库的字段注释作为实体类字段的jar包
Mybatis多参数查询与列表查询不同方式实现,效果看博文 http://blog.csdn.net/evankaka/article/details/45671473
mybatis一对多的查询方法详解! mybatis一对多的查询方法详解! mybatis一对多的查询方法详解! mybatis一对多的查询方法详解!
mybatis进行插入操作时,如果表的主键是自增的,...基本上经常会遇到的就是 Oracle Sequece 和 Mysql 自增主键,至于其他的手动生成唯一主键的问题在这里就不讨论了,这里主要说明下在mybatis中对于自增主键的配置。
今天用到了mybatis的逆向...搜了出来都是需要修改mybatis-generator-core 的源码,有方法在,但是就是没有现成的修改完成的文件,然后就根据教程改了一个,大神的教程再这,如果想动手的话可以按照他的教程修改一下...
主要介绍了Mybatis基于注解实现多表查询功能,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
mybatis中操作json类型数据(csdn)————程序
Mybatis框架(子查询)
今天在项目开发中有个业务是需要限制各个用户对某些表里的字段查询以及某些字段是否显示,这种情况下,就需要构建sql来动态传入表名、字段名了,下面给大家介绍mybatis动态调用表名和字段名的解决方法,一起看看吧