• <select id="findUser" parameterType="Map" resultType="User">
            Select * from User where 1=1 
            <if test="id>0">
            and id = #{id}
            </if>
            <if test="name!=null">
             and name like #{name}
            </if>
            <if test="pwd!=null">
            and pwd = #{pwd}
            </if>
            <if test="address_id>0">
            and address_id = #{address_id}
            </if>
        </select>
  • private SqlSession sqlSession ;
        
        private PersonDao persondao;
        
        private Logger log = Logger.getLogger(JunitTest.class);
        
        @Before
        public void setUp() throws Exception {
            sqlSession = SqlSessionFactoryUtil.openSession();
            persondao = sqlSession.getMapper(PersonDao.class);
        }
     
        @After
        public void tearDown() throws Exception {
            sqlSession.close();
        }
     
        @Test
        public void findUser() {
            Map<String, Object> map = new HashMap<String, Object>();
    //        map.put("id", 1);
            map.put("name", "张%");
    //        map.put("pwd", "111111");
    //        map.put("address_id", 1);
             List<User> list =    persondao.findUser(map);
             for (User user : list) {
                 log.info(user);
    //             System.out.println(user);
            }
     

    }

  • 增删改 返回的是影响的行号
  • /**
         * by 用法
         */
        @Test
        public void findUser2() {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("by", "name");
            map.put("id", 1);
            map.put("name", "张%");
            map.put("pwd", "111111");
            map.put("address_id", 1);
            List<User> list = persondao.findUser2(map);
            for (User user : list) {
                // log.info(user);
                System.out.println(user);
     
            }

    }

  • <select id="findUser2" parameterType="Map" resultType="User">
            select * from User where 1=1
            <choose>
                <when test="by=='id'">
                    and id = #{id}
                </when>
                <when test="by=='name'">
                    and name like #{name}
                </when>
                <when test="by=='pwd'">
                    and pwd = #{pwd}
                </when>
                <otherwise>
                    and address_id = #{address_id}
                </otherwise>
            </choose>

    </select>

  • Where 用法
  • select id="findUser3" parameterType="Map" resultType="User">
            select * from user
            <where>
                <if test="id>0">
                    and id = #{id}
                </if>
     
                <if test="name!=null">
                    and name like #{name}
                </if>
     
                <if test="pwd!=null">
                    and pwd = #{pwd}
                </if>
            </where>

    </select>

  • Trim 用法
  • <select id="findUser4" parameterType="Map" resultType="User">
            select * from User
            <trim prefix="where" prefixOverrides="and|or">
                <if test="id>0">
                    and id = #{id}
                </if>
                <if test="name!=null">
                    and name like #{name}
                </if>
                <if test="pwd!=null">
                    and pwd = #{pwd}
                </if>

    </trim>

  • foreach 用法
  • <select id="findUser5" parameterType="Map" resultType="User">
            select * from user
            <where>
                <if test="ids!=null">
                    and id in
                    <!-- 循环遍历的集合名称 ids -->
                    <foreach collection="ids" item="id" open="(" close=")"
                        separator=",">
                        #{id}
                    </foreach>
                </if>
            </where>
     

    </select>

  • @Test
        public void findUser5() {
            Map< String,Object> map = new HashMap<String, Object>();
            List<Integer> list = Arrays.asList(1,2,3,4,5);
            map.put("ids", list);
            List<User> list1 = persondao.findUser5(map);
            for (User user : list1) {
                // log.info(user);
                System.out.println(user);
            }

    }

  • limit 用法
  • SELECT * from `user` LIMIT start,size
  • /**
         * 逻辑分页处理
         */
        @Test
        public void test() {
            //传递分页参数,offset : 开始位置 limite: 分页大小
            RowBounds bounds = new RowBounds(0,10);
            List<User> list = pagedao.findUserByPage(bounds);
            for (User user : list) {
                System.out.println(user);
            }

    }

  • /**
         * 物理分页处理
         */
        @Test
        public void test2() {
            //传递分页参数,offset : 开始位置 limite: 分页大小
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("offset", 1);
            map.put("limit", 10);
            List<User> list = pagedao.findUserByPage2(map);
            for (User user : list) {
                System.out.println(user);
            }

    }

  • <!-- 物理分页 查询表中所有的数据 -->
        <select id="findUserByPage2" resultType="User" parameterType="Map">
            select * from user
            <if test="offset!=null and limit!=null">
                limit #{offset} , #{limit}
            </if>

    </select>

动态SQL 学习的更多相关文章

  1. mybatis原理分析学习记录,mybatis动态sql学习记录

    以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...

  2. 动态SQL(学习笔记)

    动态SQL EXECUTE IMMEDIATE 动态SQL字符串 [BUCK COLLECT] INTO 自定义的变量,,|记录类型 USING [IN |OUT|IN OUT]绑定的参数] [RET ...

  3. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

  4. mybatis学习笔记四(动态sql)

    直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...

  6. Java数据持久层框架 MyBatis之API学习七(动态 SQL详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  7. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

  8. Ibatis.Net 动态SQL语句学习(六)

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数吧. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&qu ...

  9. MyBatis学习(一)---配置文件,Mapper接口和动态SQL

    MyBatis MyBatis官方学习网站 http://www.mybatis.org/mybatis-3/zh/index.html 为什么需要MyBatis? Jdbc操作数据库的不足之处 1. ...

随机推荐

  1. [Exchange 2013]创建约会和会议

    简介 会议和约会之间的重要区别是,会议有与会者,并且没有约会.约会和会议可以是单实例或属于重复序列,但与会者. 房间或资源中不包括约会,因为它们不需要发送一条消息.在内部,Exchange 使用相同的 ...

  2. 基础知识(05) -- Java中的类

    Java中的类 1.类的概念 2.类中的封装 3.对象的三大特征 4.对象状态 5.类与类之间的关系 ------------------------------------------------- ...

  3. 在本地调试移动设备上的页面——神器weinre介绍

    平时写代码,最喜欢用chrome的developer Tool调试页面了,基本是离不了的工具.但是当页面需要在移动设备上使用,尤其是被嵌入到Hybird APP中时,由于移动版的chrome没有dev ...

  4. Postgresql 简单配置 (ubuntu server 14.04.3)

    安装和配置 ubuntu server 已经自动安装了progresql,故安装步骤就省略 初始postgresql没有密码,不能使用,需要先设置密码,命令(从网上随意找的)如下: sudo su p ...

  5. [Bash Shell] Shell学习笔记

    1. Shell简介 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命 ...

  6. 关于JavaScript设计模式(一)

    以后都在简书写文章了,所以这个转载我在简书中写的.地址这里 http://www.jianshu.com/p/c7b3c2c148c5

  7. 高程(4):执行环境、作用域、上下文执行过程、垃圾收集、try...catch...

    高程三 4.2.4.3 一.执行环境 1.全局执行环境是最外层的执行环境. 2.每个函数都有自己的执行环境,执行函数时,函数环境就会被推入一个当前环境栈中,执行完毕,栈将其环境弹出,把控制器返回给之前 ...

  8. Rails sanitize

    The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. T ...

  9. Java transient 关键字

    1)一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问. 2)transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被trans ...

  10. overload、overwrite、override

    1.重载 overload 函数名一样,参数不同(类型.顺序,与返回值类型无关),重载的函数一般在同一个类中 class A { public: void test() {} void test(in ...