mybatis动态SQL-<if>标签详解

标签在mybatis的开发工作中主要用于where查询,insert插入和update更新三种操作中,接下来对每种操作中的标签做详细讲述.

案例使用代码参照.

where查询中使用标签:

通过判断参数值是否为空来决定是否使用某个条件,需要注意的是,此处where 1=1 条件不可省略,可以用标签题换,可读性更高,更佳优雅.

在SysUserMapper.xml中新增 select from sys_user where 1 = 1 and id = #{id} and user_name like concat('%', #{userName}, '%') and user_code = #{userCode} and user_info like concat('%', #{userInfo}, '%') and status = #{status}

insert新增中使用标签:

通过判断参数值是否为空来决定是否将SQL字段和对象加入到SQL语句中:

在SysUserMapper.xml中修改标签:


        
        insert into sys_user (
        
            user_name,
        
        
            user_code,
        
        
            user_info,
        
        status) values (
        
            #{userName},
        
        
            #{userCode},
        
        
            #{userInfo},
        
        #{status}
        )
    

update修改中使用标签:

通过判断参数值是否为空来决定是否将SQL字段和对象加入到SQL语句中:

在SysUserMapper.xml中修改标签:


        
        update sys_user
        
            
                user_name = #{userName},
            
            
                user_code = #{userCode},
            
            
                user_info = #{userInfo},
            
            
                status = #{status},
            
            id = #{id} where id = #{id}
        
    

你可能感兴趣的:(#,Mybatis,mybatis的if标签,mybatis动态sql语句,mybatis动态SQL,mybatis的set标签)