• <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. 强有力的Linux历史命令 你还记得几个

    列出所有出现到的命令:(所有一下信息都可以通过man history得到,而且还更多) history:列出历史中执行过的命令(-c清除所有的命令历史) !N:执行编号为N的历史命令 !-N:执行倒数 ...

  2. thinkphp疑难解决4

    关于文件上传所涉及到的php.ini 中的一些配置: (以当前要设置的关键字开头...) 是每个上传文件所允许的大小, 默认的 upload_max_filesize = 2M, 如果超过了2M,_P ...

  3. python模块(json和pickle模块)

    json和pickle模块,两个都是用于序列化的模块 • json模块,用于字符串与python数据类型之间的转换 • pickle模块,用于python特有类型与python数据类型之间的转换 两个 ...

  4. 通过JAVA程序测试闰年

    首先简要介绍一下公历上规定的闰年:四年一闰,百年不闰,四百年再闰. 针对这一规则,简要的设计部分测试用例: 附(测试截图): 以下为该程序代码段: import javafx.application. ...

  5. Block使用

    1.对block的理解 >  block是iOS4.0之后出现的,是仿照java中匿名函数所创造的,它是c级别的语法,效率比协议-代理高 >  block的是一个匿名函数(没有名字的函数) ...

  6. Ubuntu下安装QQ22013

    近期闲来无事,把退役的笔记本系统换成了Ubuntu. 系统安装异常的顺利,神速的安装完成.玩弄一会发现总是缺少了点什么,呆了半天发现缺少了企鹅. 由于对Ubuntu系统不了解,安装QQ着实让我头疼了半 ...

  7. Sql Server创建函数

    在使用数据库的过程中,往往我们需要对有的数据先进行计算,然后再查询出来,所以我们就需要创建函数来完成这项任务,在数据库的Programmability(如图1)下面的Function中创建函数(如图2 ...

  8. easyui Datagrid查询报错Uncaught TypeError:Cannot read property 'length' of undefined

    1.问题描述 easyui中datagrid执行loadData方法出现如下异常:Cannot read property 'length' of undefined 2.一开始怀疑是js或者页面的问 ...

  9. JavaScript的模块化之AMD&CMD规范

    前端开发常常会遇到的问题: 1.恼人的命名冲突: 2.繁琐的文件依赖: 模块化开发的优势: 1.解决命名冲突和依赖管理: 2.模块的版本管理: 3.提高代码的可维护性: 4.前端性能优化: JavaS ...

  10. Qt for Android开发环境搭建及测试过程记录

    最近学习了Qt的QML编程技术,感觉相较于以前的QtGUI来说更方便一些,使用QML可以将界面与业务逻辑解耦,便于开发. QML支持跨平台,包括支持Android平台,因此可以使用Qt的QML进行An ...