Mybatis 的输入参数学习

mybatis 的输入参数:

    指得就是parameterType的参数 这个参数就代表的sql语句中的输入参数

    

      sql语句中的参数使用  有两种方式 :

       使用#{} 获取:  8个基本数据类型 + String 类型 都可以随意起名 比如 #{aaa}  当然不推荐 最好写的规范一些

                select * from where stuno = #{id}  这个id只是随意起的名字

              引用数据类型 就要写 实体类中的名字 比如 我实体类中有一个属性 private Sting stuno; 那么应该写 #{stuno} 不能瞎写

                select * from where stuno= #{stuno}

       使用${} 获取   8个基本数据类型 + String 类型 必须写${value} 必须是这个 不能更改  (普通的查询不推荐使用$()形式)

                select * from where stuno = ${value}    必须这样写

              引用数据类型跟#{} 的使用一样 必须写实体类中规定的属性

                select * from where stuno= ${stuno}  

        

                模糊查询,方式一: 测试类
              select stuno,stuname,stuage from student where stuage= #{stuAge} or stuname like #{stuName}
              Student student = new Student();
              student.setStuAge(24);
              student.setStuName("%w%");
              List<Student> students = studentMapper.queryStudentBystuageOrstuName(student) ;//接口的方法->SQL

                模糊查询,方式二: xml中
              
              select stuno,stuname,stuage from student where stuage= #{stuAge} or stuname like '%${stuName}%'

                

          #{}自动给String类型加上'' (自动类型转换)

 

          ${} 原样输出,但是适合于 动态排序(动态字段)

     简单介绍下parameterType"复杂"的输入参数:  

     i: hasMap 类型  :parameterType的属性值填写HashMap  sql语句中可以的名字没有要求

        

              

              测试的时候 key的值需要跟sql语句中的对应

     ii:嵌套对象的查询;

          我的数据中Studen表有两个地址,分别是学校地址跟家庭地址  我的实体类Student类 只有一个        private  Address address;  我的Address类中有 两个属性 学校地址跟家庭地址

          看图:

            Student类

              

                    Student表:

              

            Address类:

              

                    StudentMapper.xml   我的输入参数为对象类型 我就可以使用对象属性.另一个属性

                address.stu_shcooladdress

                (Student内的属性). (Address中的属性)

              

                     测试类:

                       

      

原文地址:https://www.cnblogs.com/gu-bin/p/10502662.html