mybatis 查询之参数传递

299 阅读2分钟
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--致力于减少使用成本  让用户专注SQL文件-->
<!--
 mapper 根节点
 namespace  一般情况:一个mapper对应一个不同的命名空间
 如果使用接口绑定的方式  必须输入对应接口的完整限定名

-->
<mapper namespace="com.kdy.mapper.EmpMapper">
<!--


      一、获取参数的方式

    #{}   ==> JDBC 编译成  "select id,username from emp where id = ?"
        1、会经过JDBC当中 PreparedStatement 的预编译,会根据不同的数据类型变异成对应数据库所对应的数据
        2、能够有效防止SQL注入 ;推荐使用
        3、特殊用法:
                自带内置参数  通常不使用
                javaType jdbcType mode  numericScale  resultMap ....
                比如需要改变默认为null #{id,javaType=NULL}
                保留两位 #{id,numericScale=2}
    ${} ==> JDBC 编译成 "select id,username from emp where id ="+id
        1、不会经过预编译   直接拼接到SQL中
        2、特殊用法 : 调试的时候 能直观看到SQL
                     实现特殊功能  ①动态表   动态列 动态SQL 根据用户选择的表和列  前提一定保证数据的安全性,一定要进行过滤
    一、参数的传递
        1、单个参数 seletEmp(Integer id)
            #{可以输入任何字符串获取参数}
            ${也可以输入任何字符串获取参数}
        2、多个参数
            public Emp selectEmpMore(Integer id  ,String username);
            ① 会将传进来的参数封装成map
            id ==> {key:arg0,value:id的值}  {key:param1,value:id的值}
            username==>{key:arg1,value:username的值}  {key:param2,value:username的值}
            不推荐
            ②推荐使用  public Emp selectEmpMore(@Param("id") Integer id  , @Param("username") String username);
            此时  #{id}、#{username}与@Param("xxxx") 设定的一一对应
            id  username  会将key arg0  arg1... arg参数替换掉
            paramXXX 是保留的

        3、传入参数为 javaBean参数
         POJO 各种结尾 VO   DO 数据对象  TO 函数对象 DTO 数据传输对象

           ①   public Emp selectEmpObject(Emp emp);

            emp.id  ==>#{id}
            emp.username ==>#{username}

           ② public Emp selectEmpMoreObject(Integer id,Emp emp);
            可以用 id ==>param1
                  emp.id  =>param2.id
                  emp.username  =>param2.username

            也可以使用 @Param("xxx") 指定名字  替代 argxxx参数
        4、如果是集合或数组
            ①  List<Emp> selectEmpList(List<String> usernames);
            获取 usernames.get(0)  =>arg0[0] 或  list[0]
             List<Emp> selectEmpList(@Param("userlist") List<String> usernames);

             获取 usernames.get(0)  =>userlist[0] 或  param1[0]

            ② List<Emp> selectEmpArray(String[] usernames);
            获取  usernames[0]  ==>array[0] 或 arg0[0]

            使用参数同上

       5、map参数
       一般情况下 JavaBean 插入和修改参数和javaBean属性是对应的
       如果请求的参数没有和JavaBean对应 使用Map
       如果请求的参数没有和JavaBean对应 但是使用频率很高   就是用 TO 或 DTO  (就是单独为这些参数创建一个对应的JavaBean出来 使参数规范)




-->


    <select id="selectEmp" resultType="com.kdy.pojo.Emp">
        select id,username from emp where id = #{idssdd}
    </select>
<!--
 多个参数   public Emp selectEmpMore(Integer id  ,String username);   这个参数传递是错误的
 <select id="selectEmpMore" resultType="com.kdy.pojo.Emp">-->
<!--        select * from emp where id = #{id} and username = #{username}-->
<!--    </select>-->

    <select id="selectEmpMore" resultType="com.kdy.pojo.Emp">
<!--        select * from emp where id = #{arg0} and username = #{arg1}-->
        select * from emp where id = #{id} and username = #{param2}
     </select>


    <select id="selectEmpObject" resultType="com.kdy.pojo.Emp">
        select * from emp where id = #{id} and username = #{username}
    </select>
    <select id="selectEmpMoreObject" resultType="com.kdy.pojo.Emp">
        select * from emp where id = #{arg1.id} and username = #{arg1.username} and 1=#{goodid}
    </select>
    <select id="selectEmpList"  resultType="com.kdy.pojo.Emp" >
        select id,username as username from emp where username =  #{arg0[0]} or username = #{list[1]}
    </select>

    <select id="selectEmpArray"  resultType="com.kdy.pojo.Emp" >
        select id,username as username from emp where username =  #{arg0[0]} or username = #{array[1]}
    </select>



    <select id="selectUsername" resultType="string">
            select username as username from emp where id = #{id}
    </select>



        <!--    insert  update delete
          id          同一个命名空间中只能由唯一的ID 同一个接口中也只能有唯一的方法名  虽然在Java语法中没有问题  但是mybatis是不支持的
    parameterType     用来设置该SQL的参数类型 可以当它不存在  因为mybatis会根据接口方法的参数能够自动读取参数的类型
    flushCache        设置true后  只要语句被调用 都会导致本地缓存和二级缓存被清空
    statementType     statementType="CALLABLE|PREPARED|STATEMENT" 用来设置当前的statement
                       PREPARED    支持参数预解析  默认的
                       STATEMENT   不支持参数解析
                       CALLABLE    执行存储过程的    不利于维护及数据库迁移
    useGeneratedKeys  用于获取插入后自动增长的主键(Mysql和SQLserver)
    keyProperty        将自动增长的主键赋值到那个属性中去
    【useGeneratedKeys 和 keyProperty   一般配合使用】
    keyColumn          因为数据表存在组合主键的情况  可以使用keyColumn指定获取其中哪一个字段
    databaseId         数据库厂商ID

    增删改的返回值 除了int 还可以返回 boolean类型


        -->





    <insert id="insertEmp" useGeneratedKeys="true" keyProperty="id" >

        insert into Emp(username) values(#{username})
    </insert>




    <insert id="insertEmp2" >
<!--        如果数据库不支持自动增长列 可以使用下面方式 -->
        <selectKey keyProperty="id" resultType="int" order="BEFORE">
            select max(id)+1 as id from Emp
        </selectKey>
        insert into Emp(username) values(#{username})
    </insert>
    <update id="updateEmp">
        update Emp set username=#{username} where id=#{username}
    </update>
    <delete id="deleteEmp">
        delete from emp where id = #{id}
    </delete>



</mapper>