Mybatis动态sql

一、动态sql的概念

MyBatis中动态SQL是一种可以根据不同条件生成不同SQL语句的技术,可以让我们根据具体的业务逻辑来拼接不同的SQL语句。

二、动态sql的作用

1.可以根据不同的条件生成不同的SQL语句,条件灵活

2.通过使用参数化查询或者绑定变量的方式来构建动态SQL,防止sql注入

3.动态SQL可以根据运行时的条件动态调整查询语句,优化查询

4.可以利用动态SQL来动态构建表名和字段名,实现灵活性和扩展性。

三、动态sql语句的元素及作用

1.if

用于在sql语句中添加条件语句 

2.choose(相当于switch)

根据不同的条件选择不同的查询语句

3.where

用于添加条件语句

4.foreach

用于对集合或数组进行循环操作

四、代码实例

    <select id="findStudentByStudent" resultType="Student" parameterType="student">

        select * from Student
        <!--where1=1使语句完整-->
        <!--where
        1=1-->
        <where>
            <sql>
        <if test="id!=0">
            AND id=#{id}
        </if>
        <if test="studentName!=null and studentName!=''">
            AND studentName LIKE '%${studentName}%'
        </if>
        <if test="gender!=null and gender!=''">
            AND gender=#{gender}
        </if>
    </sql>
        </where>


    </select>

        将where条件抽取出来,使用include引用

 <sql id="where_if">
        <if test="id!=0">
            AND id=#{id}
        </if>
        <if test="studentName!=null and studentName!=''">
            AND studentName LIKE '%${studentName}%'
        </if>
        <if test="gender!=null and gender!=''">
            AND gender=#{gender}
        </if>
    </sql>
    <select id="findStudentByStudent" resultType="Student" parameterType="student">

        select * from Student
        <!--where
        1=1-->
        <where>
            <include refid="where_if"></include><!--refid跟sql的id名-->
        </where>


    </select>

        代码测试

public class TestDemo {
    SqlSessionFactory ssf = null;
    @Before
    public void creatFactory(){
        InputStream input = null;
        try {
            input = Resources.getResourceAsStream("SqlMapConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        ssf = new SqlSessionFactoryBuilder().build(input);
    }
@Test
    public void testMapper4(){
        SqlSession sqlSession = ssf.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student1 = new Student();
        //student1.setId(1);
        student1.setStudentName("123");
        //student1.setGender("男");
        List<Student> students = mapper.findStudentByStudent(student1);
        for (Student student : students) {
            System.out.println(student.getId()+","+student.getStudentName()+","+student.getGender());
        }
    }

相关推荐

  1. MyBatis动态SQL

    2024-01-05 22:20:01       41 阅读
  2. Mybatis 动态 SQL - foreach

    2024-01-05 22:20:01       40 阅读
  3. MyBatis动态Sql

    2024-01-05 22:20:01       38 阅读
  4. MyBatis动态SQL语句

    2024-01-05 22:20:01       42 阅读
  5. MyBatis动态SQL

    2024-01-05 22:20:01       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-05 22:20:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-05 22:20:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-05 22:20:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-05 22:20:01       20 阅读

热门阅读

  1. openJDK下找不到jar命令(jar command is not found)

    2024-01-05 22:20:01       37 阅读
  2. Oracle

    Oracle

    2024-01-05 22:20:01      36 阅读
  3. git merge origin master 和 git merge origin/master 的区别

    2024-01-05 22:20:01       37 阅读
  4. Leetcode-105.从前序与中序遍历序列构造二叉树

    2024-01-05 22:20:01       40 阅读
  5. Mysql面试题

    2024-01-05 22:20:01       29 阅读
  6. atoi函数的模拟实现

    2024-01-05 22:20:01       29 阅读
  7. 76 BFS解单词接龙

    2024-01-05 22:20:01       36 阅读
  8. c# 设置文件夹隐藏

    2024-01-05 22:20:01       41 阅读
  9. LeetCode解法汇总1276. 不浪费原料的汉堡制作方案

    2024-01-05 22:20:01       39 阅读
  10. React Grid Layout基础使用

    2024-01-05 22:20:01       35 阅读