mybatis怎么写sql语句
发布网友
发布时间:2022-04-08 05:48
我来回答
共2个回答
懂视网
时间:2022-04-08 10:09
User findUser(String usernumber) {
return (User) sqlSessionTemplate.selectOne("findUserByUsernumber", usernumber);
}
xml代码
//对应paramterType="string"
<select id="findUserByUsernumber" parameterType="string" resultType="User">
select * from user where usernumber = #{usernumber}
</select>
2.Java实体类型参数
java实体类型参数:将parameterType的值设为对应的 Java实体类,然后用#{类的属性名}来获取
java代码
//User实体类
public void updateUser(User user) {
sqlSessionTemplate.update("updateUser", user);
}
xml代码
//将parameterType类型指向对应的类的具体地址(包名+类名),也可指向typeAlias中的别名(在mybatis.xml)文件中
<update id="updateUser" parameterType="User" >
update user set username=#{username},loginname=#{loginname},loginpassword=#{loginpassword},sex=#{sex},birthday=#{birthday} where usernumber=#{usernumber}
</update>
3.多个基本类型参数
在具体业务关系中,往往需要传多个参数,比如:登录----->用户名+密码
public User findUser(String loginname, String loginpassword) {
// TODO Auto-generated method stub
Map<String,String> map=new HashMap<String, String>();
map.put("loginname", loginname);
map.put("loginpassword", loginpassword);
return (User) sqlSessionTemplate.selectOne("loginUser", map);
}
<select id="loginUser" parameterType="map" resultType="User">
select * from user where loginname=#{loginname} and loginpassword=#{loginpassword}
</select>
利用map知识多参数传递的一种方式,还有其他方式可以实现
MyBatis的SQL语句映射文件详解(三)----参数传递
标签:
热心网友
时间:2022-04-08 07:17
mybatis的sql和你在数据库客户端执行的sql是一样的,但是在mybatis中调用的sql一般都是动态的,所以用到了参数传递。这个mybatis有对应的标签以及相应的变量来实现。你可以搜索下mybatis标签。同时给你一个参考的你看看,这个是一个查询用户的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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>