mybatis 怎么写sql语句
1个回答
展开全部
mybatis的sql和你在数据库客户端执行的sql是一样的,但是在mybatis中调用的sql一般都是动态的,所以用到了参数传递。这个mybatis有对应的标签以及相应的变量来实现。你可以搜索下mybatis标签。同时给你一个参考的你看看,这个是一个查询用户的
<select
id="queryUsers"
parameterType="map"
resultType="xx.xx.xx.bean.UserBean">
<![CDATA[
select
ID,
LOGIN_NAME
AS
loginName,
PASSWORD,
REAL_NAME
AS
realName,
POSITION,
(SELECT
D.POSITION_NAME
FROM
UNIT_POSITION
D
WHERE
D.POSITION_CODE=T.POSITION)
POSITIONNAME,
USER_TYPE
AS
userType,
SEX,
PID,
TO_CHAR(T.BIRTHDAY,'YYYY-MM-DD')
BIRTHDAY,
EMAIL,
CONTACT_TEL
AS
contactTel,
CONTACT_MOBILE
AS
contactMobile,
CONTACT_FAX
AS
contactFax,
CONTACT_ZIP
AS
contactZip,
CONTACT_ADDR
AS
contactAddr,
STATUS,
EDUCATION,
(SELECT
D.EDUCATION_NAME
FROM
UNIT_EDUCATION
D
WHERE
D.EDUCATION_CODE=T.EDUCATION
AND
D.STATUS=0)
EDUCATIONNAME,
NATION,
POLITICAL,
REMARK,
TO_CHAR(T.CREATE_DATE,'YYYY-MM-DD
HH24:MI:SS')
createDate,
(SELECT
D.REAL_NAME
FROM
UNIT_USER
D
WHERE
D.ID=
T.CREATE_USER_ID)
createUserId,
TO_CHAR(T.UPDATE_DATE,'YYYY-MM-DD
HH24:MI:SS')
updateDate,
(SELECT
D.REAL_NAME
FROM
UNIT_USER
D
WHERE
D.ID=
T.UPDATE_USER_ID)
updateUserId
from
UNIT_USER
T
]]>
<where>
T.STATUS='1'
<if
test="realName
!=null
and
realName
!=''">
and
T.REAL_NAME
like
'%${realName}%'
</if>
<if
test="nexusDpartment
!=null">
AND
T.ID
IN
(SELECT
DISTINCT
D.USER_ID
FROM
UNIT_USER_DEPT
D
WHERE
D.DEPT_CODE
IN
(${nexusDpartment}))
</if>
<if
test="deptCode
!=null
and
deptCode
!=''">
AND
T.ID
IN
(SELECT
DISTINCT
D.USER_ID
FROM
UNIT_USER_DEPT
D
WHERE
D.DEPT_CODE
=
#{deptCode})
</if>
</where>
<if
test="sort
!=
null
and
sort
!=
''">
order
by
${sort}
<if
test="direction
!=
null
and
direction
!=
''">
${direction}
</if>
</if>
</select>
<select
id="queryUsers"
parameterType="map"
resultType="xx.xx.xx.bean.UserBean">
<![CDATA[
select
ID,
LOGIN_NAME
AS
loginName,
PASSWORD,
REAL_NAME
AS
realName,
POSITION,
(SELECT
D.POSITION_NAME
FROM
UNIT_POSITION
D
WHERE
D.POSITION_CODE=T.POSITION)
POSITIONNAME,
USER_TYPE
AS
userType,
SEX,
PID,
TO_CHAR(T.BIRTHDAY,'YYYY-MM-DD')
BIRTHDAY,
EMAIL,
CONTACT_TEL
AS
contactTel,
CONTACT_MOBILE
AS
contactMobile,
CONTACT_FAX
AS
contactFax,
CONTACT_ZIP
AS
contactZip,
CONTACT_ADDR
AS
contactAddr,
STATUS,
EDUCATION,
(SELECT
D.EDUCATION_NAME
FROM
UNIT_EDUCATION
D
WHERE
D.EDUCATION_CODE=T.EDUCATION
AND
D.STATUS=0)
EDUCATIONNAME,
NATION,
POLITICAL,
REMARK,
TO_CHAR(T.CREATE_DATE,'YYYY-MM-DD
HH24:MI:SS')
createDate,
(SELECT
D.REAL_NAME
FROM
UNIT_USER
D
WHERE
D.ID=
T.CREATE_USER_ID)
createUserId,
TO_CHAR(T.UPDATE_DATE,'YYYY-MM-DD
HH24:MI:SS')
updateDate,
(SELECT
D.REAL_NAME
FROM
UNIT_USER
D
WHERE
D.ID=
T.UPDATE_USER_ID)
updateUserId
from
UNIT_USER
T
]]>
<where>
T.STATUS='1'
<if
test="realName
!=null
and
realName
!=''">
and
T.REAL_NAME
like
'%${realName}%'
</if>
<if
test="nexusDpartment
!=null">
AND
T.ID
IN
(SELECT
DISTINCT
D.USER_ID
FROM
UNIT_USER_DEPT
D
WHERE
D.DEPT_CODE
IN
(${nexusDpartment}))
</if>
<if
test="deptCode
!=null
and
deptCode
!=''">
AND
T.ID
IN
(SELECT
DISTINCT
D.USER_ID
FROM
UNIT_USER_DEPT
D
WHERE
D.DEPT_CODE
=
#{deptCode})
</if>
</where>
<if
test="sort
!=
null
and
sort
!=
''">
order
by
${sort}
<if
test="direction
!=
null
and
direction
!=
''">
${direction}
</if>
</if>
</select>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询