• 5. MyBatis 动态SQl语句 的使用


    学习之前 ,你可以吧log4j日志打开,级别调为DEBUG。

    动态SQL语句

    Mybatis 的映射文件中,有些时候业务逻辑复杂时,我们的 SQL是动态变化的, 此时在前面的学习中我们的 SQL 就不能满足要求了,

    主要有这几个标签实现动态SQL语句的编写:

    动态 SQL 之 if :

    我们根据实体类的不同取值,使用不同的 SQL语句来进行查询。

    查询条件是 3个都查询:

    接口:    

    映射文件:  

    实现:

        package com.bihu.Service;
    
        import com.bihu.Bean.User;
        import com.bihu.Dao.UserMapper;
        import org.apache.ibatis.io.Resources;
        import org.apache.ibatis.session.SqlSession;
        import org.apache.ibatis.session.SqlSessionFactory;
        import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
        import java.io.IOException;
        import java.io.InputStream;
        import java.util.List;
    
        public class UserService {
            public static void main(String[] args) throws IOException {
                User user = new User();//模拟有User数据【其实单单查一个ID推荐用Integer即可 但这里没有。】
                user.setId(99);
                user.setUsername("Plmm");
                user.setPassword("Plmm9999");
    
                InputStream sqlMapConfig = Resources.getResourceAsStream("SqlMapConfig.xml");
                SqlSessionFactory build = new SqlSessionFactoryBuilder().build(sqlMapConfig);
                SqlSession sqlSession = build.openSession();
    
                UserMapper mapper = sqlSession.getMapper(UserMapper.class); //获取动态对象
                List<User> userList = mapper.find(user);
                System.out.println(userList);
    
    
            }
        }
    Service 【模拟】

    我们运行可以发现 语句和参数都可以看到 :

    如果我们希望改变,那么就在映射文件中写映射语句,例如我是用户 如果我只知道我的id 和密码 或 账号和密码,那么你就可以在映射文件中这样写:

    <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
        <mapper namespace="com.bihu.Dao.UserMapper">
    
        <select id="find" parameterType="User" resultType="User" >    <!--这里的User前面有别名-->
         <!--原本语句:select * from user where id = #{id} and username = #{username} and password = #{password}-->
            select * from user
            <where>            <!--相当于where-->
                <if test="id!=null and id!= 0">
                   and id = #{id}                      <!--当id不是null id不是0的时候 这条语句生效-->
                </if>
    
                <if test="username!=null and username!= ''">
                   and username = #{username}                      <!--当username不是null username不为空的时候 这条语句生效-->
                </if>
    
                <if test="password!=null and password!= ''">
                   and password = #{password}                      <!--当password不是null password不为空的时候 这条语句生效-->
                </if>
            </where>
        </select>
    
    
    
        </mapper>
    映射文件

      查询对象

       语句

       参数

    我们写任何一个条件都可以查询到了,这个就是动态sql语句,其实这个案例不好,,,好的案例比如商城的筛选功能。

     这就是 IF 标签。

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15143816.html

  • 相关阅读:
    [译]在Python中如何使用额enumerate 和 zip 来迭代两个列表和它们的index?
    [译]如何去除Git的unstaged的文件提示“old mode 100755 new mode 100644”?
    [译]在SQL查询中如何映射(替换)查询的结果?
    [总结]《敏捷软件开发: 原则、模式与实践》一次编程实践
    [书摘]《敏捷软件开发: 原则、模式与实践》第一部分:敏捷开发
    [译]Python
    [问题解决]Python locale error: unsupported locale setting
    [持续补充]开发过程中常见bug查找思路
    [译]如何比较同一分支上的不同commit的代码区别?
    [整理]如何切换到远程分支
  • 原文地址:https://www.cnblogs.com/bi-hu/p/15143816.html
Copyright © 2020-2023  润新知