• <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. 再谈 Mysql解决中文乱码

    一是要把数据库服务器的字符集设置为 utf8. 数据库的字符集会跟服务器的字符集一起变化, 也会变成 utf8: 在/etc/my.cnf中, 的 [mysqld]中, 设置 character-se ...

  2. MySQL自动化运维之用mysqldump和mysqlbinlog实现某一数据库的每周全备和每天差异备份,并添加到执行计划【热备】

    案例: 线上有一数据库,需要每周全备一次,每天差备一次[安全起见还是差备吧,不要增备,不要吝啬磁盘哦,而且差备恢复还很快] 1.每周对数据库hellodb做完全备份 crontab任务计划: * * ...

  3. 在布局中使用android.support.v4.app.Fragment的注意事项

    1.Activity必须继承android.support.v4.app.FragmentActivity 2.fragment标签的name属性必须是完全限定包名,如下: <LinearLay ...

  4. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  5. 【Android自学日记】【转】Android Fragment 真正的完全解析(上)

    自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~ 本篇博客力求为大家说明Fragment如何产 ...

  6. 引用类型的转换问题和instanceof

    基本数据类型:  

  7. Spark基础知识汇总

    2,wordcount: val wordcount = sc.textFile()).reduceByKey(_ + _).sortByKey().collect val wordcount = s ...

  8. spring拦截器 实现应用之性能监控

    package cn.ximi.erp.web.common.interceptors; import cn.ximi.core.common.utils.string.StringUtil; imp ...

  9. php mysqli mysqli_query() mysqli_real_query()

    2016年11月26日 15:22:27 星期六 场景: PHP从mysql中读取数据 1. 一次性读取所有数据返给PHP 2. 每次循环只读取一掉记录 数据量小的时候可以使用第一种方法, 数据量很大 ...

  10. Visual Studio将std::cout输出到Output窗口

    在debug的时候,输出到Output需要使用OutputDebugString函数,但部分库的log是采用std::cout输出的,需要用控制台(黑窗)程序来查看输出.有没有一种使用GUI和Outp ...