参考文献:
MyBatis 中使用 Collection 嵌套查询
子查询写法_其一
对应TastCommonEntity
<resultMap id="BaseResultMap" type="com.aima.service.taskmanager.entity.TastCommonEntity">
<id column="stationId" jdbcType="VARCHAR" property="stationId"/>
<id column="lineid" jdbcType="VARCHAR" property="lineid"/>
<result column="taskcode" jdbcType="VARCHAR" property="taskcode"/>
<result column="createtime" jdbcType="TIMESTAMP" property="createtime"/>
...
<collection column="taskcode" property="casePro" ofType="com.aima.service.taskmanager.entity.MissionCaseEntity"
select="selectConstable" javaType="java.util.ArrayList">
</collection>
<collection column="taskcode" property="staffPro" ofType="com.aima.service.taskmanager.entity.MissionStaffEntity"
select="selectStaff" javaType="java.util.ArrayList">
</collection>
</resultMap>
主要是 select=" " 这块必须跟下面查询的id对应,要不然找不到
对应MissionStaffEntity
<resultMap id="BaseResultMapStaff" type="com.aima.service.taskmanager.entity.MissionStaffEntity">
<id column="staffid" jdbcType="VARCHAR" property="id"/>
<result column="staffid" jdbcType="VARCHAR" property="id"/>
...
</resultMap>
对应MissionCaseEntity
<resultMap id="BaseResultMapCase" type="com.aima.service.taskmanager.entity.MissionCaseEntity">
<result column="caseid" jdbcType="VARCHAR" property="id"/>
<result column="casecode" jdbcType="VARCHAR" property="code"/>
<result column="casename" jdbcType="VARCHAR" property="name"/>
<result column="casesex" jdbcType="VARCHAR" property="sex"/>
....
</resultMap>
第一个查询
<select id="selectStationPreview" parameterType="com.aima.service.taskmanager.entity.TastCommonEntity" resultType="com.aima.service.taskmanager.entity.TastCommonEntity"
resultMap="BaseResultMap">
select
mission_sta t
where t.taskcode = #{taskcode,jdbcType=VARCHAR}
</select>
<select id="selectStaff" resultMap="BaseResultMapStaff" resultType="com.aima.service.taskmanager.entity.MissionStaffEntity"
parameterType="com.aima.service.taskmanager.entity.MissionStaffEntity">
select
missionStaff ms
where ms.taskcode = #{taskcode,jdbcType=VARCHAR}
</select>
<select id="selectCase" resultMap="BaseResultMapCase" resultType="com.aima.service.taskmanager.entity.MissionCaseEntity"
parameterType="com.aima.service.taskmanager.entity.MissionCaseEntity">
select
missionCase mc
where mc.taskcode = #{taskcode,jdbcType=VARCHAR}
</select>
查询过程是主要查询,子查询一,子查询二
- taskcode是前面传过来的,用于主要查询的where条件使用
- 子查询一中 column=“taskcode” 将值带过来 用于 子查询的where条件使用
- 子查询二中 column=“taskcode” 将值带过来 用于 子查询的where条件使用
自己理解的,本人使用正常,如有错误欢迎指正,如有问题欢迎留言!! by java小白
查询XML节点value:通过nodes指定到节点通过Value属性取出值
Declare@Xmlxml
set@Xml='<Employee><ID>1</ID><ID>2</ID></Employee>'
SELECTID.value('.','Nvarchar(500)')...
上一篇博客中针对spring boot中整合mybatis框架的步骤弄了一个实例,传送门:spring boot简单集成mybatis。通常来讲一个框架的使用都会支持XML和注解两种配置情况,本篇博客就在上一篇博客的基础上总结MyBatis的XML用法。
数据库、数据表以及Java实体的准备
先准备相关脚本,并执行
CREATE DATABASE mybatisLearn;
US...
<if test="resultCode == 'FAIL'">
(t2.result_code !='SUCCESS' OR t2.result_code IS NULL)
from T_DICTIONARY t left outer join T_DICTIONARY d on t.p_id = d.ID (t为子表,d为父表)
页面添加 input
父类名称:<input id="pDName" name="pName" type="text" placeholder="字典名称" class="form-control input-medi...
mybatis/mybatisplus的xml文件子查询案例前言业务对象三层设计ControllerServiceDao层测试
之前学习mybatis的时候,有各种one2many,many2many的连表查询业务。最近公司项目采用的是mybatis plus持久层框架,其实大差不差。在网上百度的有关子查询的xml文件sql语句也有,感觉看起来吃力,只是贴个代码。
由于我这边结合业务走通了,...
首先,你需要在数据库中建立一张表,包含 id 和 parentid 两列。在 Java 中使用 MyBatisPlus,你需要定义一个实体类,对应这张表,并在 Mapper 接口中编写 SQL 语句来实现查询。
如果你希望查询某一条数据的所有父数据,你可以使用以下代码:
```java
// 在 Mapper 接口中定义方法
List<Entity> selectParentsById(@Param("id") long id);
// 在 Mapper.xml 文件中编写对应的 SQL 语句
<select id="selectParentsById" resultType="Entity">
SELECT * FROM table WHERE id IN (
SELECT parentid FROM table WHERE id = #{id}
UNION
SELECT id FROM table WHERE id = #{id}
</select>
如果你希望查询某一条数据的所有子数据,你可以使用以下代码:
```java
// 在 Mapper 接口中定义方法
List<Entity> selectChildrenById(@Param("id") long id);
// 在 Mapper.xml 文件中编写对应的 SQL 语句
<select id="selectChildrenById" resultType="Entity">
SELECT * FROM table WHERE parentid = #{id}
</select>
希望这些信息能帮到你!