zoukankan      html  css  js  c++  java
  • Mybatis之动态SQL&OGNL表达式

    1.接口

    public interface MemberMapperDynamicSQL {
    
        public List<Members> selectMembersByIf(Members member);
    
    }

    2.配置文件

    <mapper namespace="com.dao.MemberMapperDynamicSQL">
    
        <!-- 
            OGNL表达式
            &的转义字符为 &amp;
            '的转义字符为 &quot;
         -->
        <select id="selectMembersByIf" resultType="members">
        
            select * from members where 1 = 1
            
            <if test="id != null &amp;&amp; id != &quot;&quot;">
                and id = #{id}
            </if>
            
            <if test="member_name != null &amp;&amp; member_name != &quot;&quot;">
                and member_name like #{member_name}
            </if>
            
        </select>
    
    </mapper>

    3.测试

        public static void main(String[] args) throws IOException {
            SqlSession session = getSqlSession();
            try {
                // 面向接口方式
                MemberMapperDynamicSQL mapper = session.getMapper(MemberMapperDynamicSQL.class);
                Members member = new Members();
    //            member.setId(1);
    //            member.setMember_name("%云%");
                List<Members> list = mapper.selectMembersByIf(member);
                System.out.println(list);
                session.commit();
            } finally {
                // 关闭session
                session.close();
            }
        }

    4.在全局配置文件中加入log打印日志

        <settings>  
            <setting name="logImpl" value="STDOUT_LOGGING"/>  
        </settings>

    5.打印结果

    ==>  Preparing: select * from members where 1 = 1 
    ==> Parameters: 
    <==    Columns: id, member_name, pass_word, age
    <==        Row: 1, 张三, 123456, 25
    <==        Row: 2, 李四, 123456, 25
    <==        Row: 3, 关云长, 123456, 54
    <==        Row: 4, 关云长, 123456, 54
    <==      Total: 4
    [Member [id=1, member_name=张三, password=123456, age=25], Member [id=2, member_name=李四, password=123456, age=25], Member [id=3, member_name=关云长, password=123456, age=54], Member [id=4, member_name=关云长, password=123456, age=54]]

    条件成立则拼接相应的条件,不成立则不拼接

  • 相关阅读:
    python之闭包,装饰器
    python之函数名称空间,作用域,嵌套函数
    python之函数基础
    Python之文件操作
    Linux之系统优化配置
    VMware安装CentOS操作系统详细步骤
    拷贝、浅拷贝、深拷贝解答
    python之字符串,列表,字典,元组,集合内置方法总结
    东方超环(EAST)世界纪录
    Vue通信、传值的多种方式,详解(都是干货)
  • 原文地址:https://www.cnblogs.com/zhangjianbing/p/8325745.html
Copyright © 2011-2022 走看看