MyBatis动态SQL
MyBatis动态SQL
<select id = "findRoles" parameterType = "string" resultMap = "roleResultMap"> select role_no,role_name,note from t_role where 1 = 1 <if test = "roleName != null and roleName != ''" > and role_name like concat('%', #{roleName},'%') </if> </select>
<select id = "findRoles" parameterType = "role" resultMap = "roleResultMap"> select role_no,role_name,note from t_role where l = l <choose> <when test = "roleNo != null and roleNo != ''"> AND role_no = #{roleNo} </when> <when test = "role != null and roleName != ''" > AND role_name like concat('%',#{roleName},'%') </when> <otherwise> AND note is not null </otherwise> </choose> </select>
<select id = "findRoles" parameterType = "string" resultMap = "roleResultMap"> select role_no, role_name, note from t_role <where> <if test = "roleName != null and roleName != ''"> and role_name like concat ('%',#{roleName},'%') </if> </where> </select>
<select id = "findRoles" parameterType = "string" resultMap = "roleResultMap"> select role_no, role_name, note from t_role <trim prefix = "where" prefixOverrides = "and"> <if test = "roleName != null and roleName != ''"> and role_name like concat ('%',#{roleName},'%') </if> </trim> </select>
<update id = "updateRole" parameterType = "role"> update t_role <set> <if test = "roleName != null and roleName != ''"> role_name = #{roleName}, </if> <if test = "note != null and note != ''"> note = #{note} </if> </set> where role_no = #{roleNo} </update>
<select id = "findUserBySex" resultType = "user"> select * from t_user where sex in <foreach item = "sex" index = "index" collection = "sexList" open = "(" separator = "," close = ")"> #{sex} </foreach> </select>
<select id = "getRoleTest" parameterType = "string" resultMap = "roleResultMap"> select role_no, role_name,note from t_role <if test = "type == 'Y'"> where l = l </if> </select>
bind元素,其作用是通过OGNL表达式去自定义一个上下文变量,在进行模糊查询的时候,我们是concat用”%”和参数相连接,然而在Oracle数据库则是用连接符号”||”这样的话就需要两种方式去实现了,我们可以自定义一个;这就是bind元素的作用了;
<select id = "findRole" resultType = "com.learn.capter5.mybatis.bean.RoleBean"> <bind name = "pattern" vlaue = "'%' + _parameter + '%'"/> SELECT id ,role_name as roleName, create_date as createDate ,end_date as endFlag, end_flag as endFlag note FROM t_role where role_name like #{pattern} </select>