码迷,mamicode.com
首页 > 数据库 > 详细

mybatis随意sql语句

时间:2014-10-28 02:00:17      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   使用   for   sp   

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.ldrc.srm.jczx.dal.dataobject.construction.technology.SmTaskStatus@5de5bb3c, smBaseInfo=com.ldrc.srm.jczx.dal.dataobject.construction.projectinfo.BaseInfo@1567904d, smTaskPlan=com.ldrc.srm.jczx.dal.dataobject.construction.technology.TaskPlan@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.ldrc.srm.jczx.dal.dataobject.construction.technology.SmTaskStatus@105f0f87, smBaseInfo=com.ldrc.srm.jczx.dal.dataobject.construction.projectinfo.BaseInfo@50f47076, smTaskPlan=com.ldrc.srm.jczx.dal.dataobject.construction.technology.TaskPlan@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.ldrc.srm.jczx.dal.dataobject.construction.technology.SmTaskStatus@4e2a718, smBaseInfo=com.ldrc.srm.jczx.dal.dataobject.construction.projectinfo.BaseInfo@3c481b29, smTaskPlan=com.ldrc.srm.jczx.dal.dataobject.construction.technology.TaskPlan@21ef3faa]]
3 数量
明明 Mon Oct 27 00:00:00 CST 2014
丽丽 Tue Oct 21 00:00:00 CST 2014
null 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>

mybatis随意sql语句

标签:style   blog   http   io   color   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/YingYue/p/4055650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!