zoukankan      html  css  js  c++  java
  • Mybatis的动态Sql

     mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。###

     1、动态sql

      映射文件:

      <!--
            会自动的去判断传入的id和username是否为空,
            如果id为空则SQL语句中不会拼接id = ?查询语句
            如果username为空则SQL语句中不会拼接username = ?查询语句
        -->
        <delete id="deleteUser" parameterType="User">
            DELETE FROM user
            <where>
    			<!--
    				test:里面写判断条件,如果该条件不满足,则不会拼接该if标签内的语句
    			-->
                <if test="id != null and id != ''">
                    and id=#{id}
                </if>
                <if test="username != null and username !=''">
                    and username=#{username}
                </if>
    
            </where>
        </delete>
    

     2、sql片段

      在Mapper文件中可以定义sql片段,定义后sql语句可以引用该片段,达到重复使用的效果。
      映射文件:

      <!--
             代码片段
             id:该代码片段的唯一标识
        -->
        <sql id="sql_1" >
    	<!--
            会自动的去判断传入的id和username是否为空,
            如果id为空则SQL语句中不会拼接id = ?查询语句
            如果username为空则SQL语句中不会拼接username = ?查询语句
        -->
            <if test="id != null and id != ''">
                and id=#{id}
            </if>
            <if test="username != null and username !=''">
                and username=#{username}
            </if>
        </sql>
    

      引用片段:

        <delete id="deleteUser" parameterType="User">
            DELETE FROM user
            <where>
                <!--
                    引用SQL片段
                    refid:要引用的sql片段id
                -->
                <include refid="sql_1" />
            </where>
        </delete>
    

     3、foreach

      向sql传递数组或List,mybatis使用foreach解析.

      POJO文件:

    public class UserPlus extends User{
    
        private List<Integer> list;
    
        public List<Integer> getList() {
            return list;
        }
    
        public void setList(List<Integer> list) {
            this.list = list;
        }
    }
    

      映射文件:

       <!--foreach-->
        <select id="selectUser" parameterType="UserPlus" resultType="User">
            SELECT * FROM user
            <where>
                <if test="list != null">
                    <!--
                        select * from user where (id=? or id=? or id=?)
                        collection:传入的list和数组的名称
                        item:遍历list或数组时的变量名称
                        open:开始遍历时拼接的sql语句部分
                        close:结束遍历时拼接的sql语句部分
                        separator:遍历的两个对象中间需要拼接的sql语句部分
                    -->
                    <foreach collection="list" item="user_id" open="and (" close=")" separator="or">
                      <!--每次遍历需要拼接的SQL语句部分-->
                        id=#{user_id}
                    </foreach>
                </if>
            </where>
        </select>
    

      接口文件:

    //根据多个id查询多个用户
        public List<User> selectUser(UserPlus userPlus) throws Exception;
    

      测试文件:

     public void selectUser(){
            try {
                sqlSession = sessionFactory.openSession();
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                UserPlus userPlus = new UserPlus();
                List<Integer> list = new ArrayList<Integer>();
                list.add(1);
                list.add(3);
                list.add(5);
                list.add(7);
                userPlus.setList(list);
               List<User> list1 =  userMapper.selectUser(userPlus);
                System.out.println("查询成功");
                for (User user:list1) {
                    System.out.println(user);
                }
            }catch (Exception e){
                System.out.println("查询失败");
            }finally {
                sqlSession.close();
            }
        }
  • 相关阅读:
    爬取网易云音乐歌手和id
    【兼容调试】cffi library '_openssl' has no function, constant or global variable named 'Cryptography_HAS
    python如何去掉字符串‘xa0’
    python 中json和字符串互相转换
    vip视频播放
    一行Python代码画心型
    Java语言编写MD5加密方法,Jmeter如何给字符串MD5加密
    转载:oracle RAC集群启动和关闭
    转载:oracle 11g ADG实施手册(亲测,已成功部署多次)
    如何上传本地文件到github又如何删除自己的github仓库
  • 原文地址:https://www.cnblogs.com/jack1995/p/7242860.html
Copyright © 2011-2022 走看看