mybatis的mapper.xml随意sql语句, 不管表之间存不存在关系, 都可以使用, 但注意resultMap中一定要指定查询数据返回的列 或 对象(其实就是多列封装到一个对象中)
<?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="SmTaskMapper" > <resultMap id="BaseResultMap" type="com.ldrc.srm.jczx.dal.dataobject.construction.projectinfo.SmTask" > <id column="STASK_ID" property="staskId" jdbcType="INTEGER" /> <result column="SBI_ID" property="sbiId" jdbcType="INTEGER" /> <result column="STASK_NUMBER" property="staskNumber" jdbcType="VARCHAR" /> <result column="STASK_NAME" property="staskName" jdbcType="VARCHAR" /> <result column="STASK_SHORT_NAME" property="staskShortName" jdbcType="VARCHAR" /> <result column="STASK_ORDERBY" property="staskOrderby" jdbcType="INTEGER" /> <result column="STASK_TYPE" property="staskType" jdbcType="INTEGER" /> <result column="STASK_CHANGE_DATE" property="staskChangeDate" jdbcType="DATE" /> <result column="STASK_ISCHANGE_STATE" property="staskIschangeState" jdbcType="INTEGER" /> <result column="PCCI_ID" property="pcciId" jdbcType="INTEGER" /> <result column="STASK_PARENT_ID" property="staskParentId" jdbcType="INTEGER" /> <result column="IS_INVALID" property="isInvalid" jdbcType="INTEGER" /> <result column="spart_name" property="spartName" jdbcType="VARCHAR"/> <!-- 这个就是我指定的研究人员的列 --> <association property="smTaskPlan" resultMap="SmTaskPlanMapper.BaseResultMap"/> <!-- association中没有select就不需要column, column给select的方法传参 , 我这里的方式随意查, 表随意些就像正常的sql语句一样, 可以吧association看做封装多列数据--> <association property="smTaskStatus" resultMap="SmTaskStatusMapper.BaseResultMap"/> </resultMap> <sql id="Base_Column_List" > STASK_ID, SBI_ID, STASK_NUMBER, STASK_NAME, STASK_SHORT_NAME, STASK_ORDERBY, STASK_TYPE, STASK_CHANGE_DATE, STASK_ISCHANGE_STATE, PCCI_ID, STASK_PARENT_ID, IS_INVALID </sql> <select id="aa" resultMap="BaseResultMap" parameterType="com.ldrc.srm.jczx.dal.dataobject.construction.projectinfo.SmTask"> select t1.*, t2.*, t3.spart_name from sm_s_task t1 left join sm_s_task_plan t2 on t1.STASK_ID = t2.STASK_ID left join SM_S_PARTICIPA t3 on t3.SPART_ID = t2.SPART_ID where t1.sbi_id = #{sbiId} AND t1.STASK_ISCHANGE_STATE = #{staskIschangeState} </select> </mapper> @Test public void aa(){ SmTask st = new SmTask(); st.setSbiId(1);st.setStaskIschangeState(2); List<SmTask> list = this.sqlSession.selectList("SmTaskMapper.aa", st); System.out.println(list); System.out.println(list.size()+" 数量"); for (SmTask smTask : list) { System.out.println(smTask.getSpartName()+ " "+smTask.getSmTaskPlan().getSstpEndDate()); } }控制台输出00:39:16.571 [main] DEBUG SmTaskMapper.aa - ooo Using Connection [conn2: url=jdbc:h2:~/srmcydw_v01 user=SA]00:39:16.571 [main] DEBUG SmTaskMapper.aa - ==> Preparing: select t1.*, t2.*, t3.spart_name from sm_s_task t1 left join sm_s_task_plan t2 on t1.STASK_ID = t2.STASK_ID left join SM_S_PARTICIPA t3 on t3.SPART_ID = t2.SPART_ID where t1.sbi_id = ? AND t1.STASK_ISCHANGE_STATE = ? 00:39:16.618 [main] DEBUG SmTaskMapper.aa - ==> Parameters: 1(Integer), 2(Integer)[SmTask [spartName=明明, staskId=1, sbiId=1, staskNumber=2014BH001, staskName=参与单位课题一的任务一, staskShortName=任务一, staskOrderby=1, staskType=1, staskChangeDate=Sat Oct 25 00:00:00 CST 2014, staskChangeDate1=null, staskIschangeState=2, pcciId=1, staskParentId=1, isInvalid=2, smTaskStatus=com.ld[email protected]5de5bb3c, smBaseInfo=com[email protected]1567904d, smTaskPlan=co[email protected]4232ba35], SmTask [spartName=丽丽, staskId=2, sbiId=1, staskNumber=2014BH020, staskName=参与单位课题一的自定义任务一, staskShortName=任务二, staskOrderby=2, staskType=2, staskChangeDate=Sat Oct 25 00:00:00 CST 2014, staskChangeDate1=null, staskIschangeState=2, pcciId=1, staskParentId=1, isInvalid=2, smTaskStatus=com.ld[email protected]105f0f87, smBaseInfo=com[email protected]50f47076, smTaskPlan=co[email protected]60037c7f], SmTask [spartName=null, staskId=3, sbiId=1, staskNumber=2014BH021, staskName=参与单位课题一的自定义任务二, staskShortName=任务三, staskOrderby=3, staskType=2, staskChangeDate=Sat Oct 25 00:00:00 CST 2014, staskChangeDate1=null, staskIschangeState=2, pcciId=1, staskParentId=1, isInvalid=1, smTaskStatus=com.ld[email protected]4e2a718, smBaseInfo=com[email protected]3c481b29, smTaskPlan=co[email protected]21ef3faa]]3 数量明明 Mon Oct 27 00:00:00 CST 2014丽丽 Tue Oct 21 00:00:00 CST 2014null null
<select id="aa" resultMap="BaseResultMap" parameterType="com.ldrc.srm.jczx.dal.dataobject.construction.projectinfo.SmTask">
select
t1.*, t2.*, t3.spart_name
from sm_s_task t1
left join sm_s_task_plan t2
on t1.STASK_ID = t2.STASK_ID
left join SM_S_PARTICIPA t3
on t3.SPART_ID = t2.SPART_ID
where t1.sbi_id = #{sbiId} AND t1.STASK_ISCHANGE_STATE = #{staskIschangeState}
</select>
时间: 2024-10-03 20:17:11