• Mybatis笔记 – Po映射类型


    一、输入映射类型

            parameterType定义输入到sql中的映射类型,可以是  简单类型  、po类对象(可自动生成 或 手动定义)、 pojo包装对象(用于综合查询,UserCustom用户自定义对象 、UserQueryvo视图层对象包)、hashMap对象、集合对象以及数组(使用foreach拼接动态sql)等,建议使用pojo包装对象或map对象,以保证Dao的通用性。

    1、基本数据类型

    //基本类型
    <select id="findEmpByName" parameterType="int” >
    	   select * from emp where empno=#{empno}
    </select>
    //类型打包器
    <select id="findEmpByName" parameterType="java.lang.Integer” >
    	   select * from emp where empno=#{empno}
    </select>
    

    2、pojo类型

    //pojo类对象
    <select id="findEmpByName" parameterType="Po.Emp” >
    	select * from emp where empno=#{empno}
    </select>
    //pojo包装类对象
    <select id="findEmpByName" parameterType="Po.EmpVo” >
    	   select * from emp where otherMsg = #{Emp.otherMsg}
    </select>

    3、hashMap对象

    <select id="findUserByHashmap" parameterType="map" resultType="user">
    	select * from user where id=#{id} and username like '%${username}%'
    </select>
    
    //构造查询条件Hashmap对象
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("id", 1);
    map.put("username", "管理员");
    
    //传递Hashmap对象查询用户列表
    List<User>list = userMapper.findUserByHashmap(map);
    

           若传递的map中的key和sql中解析的key不一致。Eclipse不会报错,只是通过key获取值为空。

    4、占位字符

    (1)#{}占位符号

          SQLl语句中【 #{} 】示一个占位符即【?】,通过#{}可以实现向preparedStatement中的预处理语句中设置占位符号,mybatis底层通过ognlpojo中获取属性值,后再将输入变量id传到sql。

    <select id="findEmpById" parameterType="int" resultType="Emp">
    	select * from emp where empno = #{对象.属性值}
    </select>

         使用占位符#{}可以有效防止sql注入,在使用时不需要关心参数值的类型,mybatis会自动进行java类型和jdbc类型的转换。#{}可以接收简单类型、pojo、HashMap。如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称;如果${}接收pojo对象值,通过【属性.对象属性】获取对象属性值。特别的:String、Array、类型打包器都获取属性值用value

    (2)拼接字符

          【${}】表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换。${}可以接收简单类型值、pojo、HashMap。如果parameterType传输单个简单类型值,如果接收简单类型,${}括号中只能是value;如果${}接收pojo对象值,通过【属性.对象属性】获取对象属性值。

    ① 用于使用通配符进行模糊查找(可能会引起sql注入)

    <select id="selectEmpByName" parameterType="string" resultType="Emp">
    	select * from emp where ename like '%${value}%'
    </select>
    
    //如果使用占位符号则必须人为在传参数中加%,较为麻烦
    List<User> list = userMapper.selectUserByName("%管理员%");
    
    //如果使用${}拼接符号则不用人为在参数中加%,建议使用
    List<User>list = userMapper.selectUserByName("管理员");
    
    
    //如果输入的是 【OR 1=1 OR】,会返回表中的全部记录(SQL注入)
    //SQL语句为:select * from emp where ename like '%’ OR 1=1 OR '%’'
    

    ② 用于在SQL语句中拼接【列名 或 表名】

         如果将列名通过参数传入sql,根据传的列名进行排序,应该使用拼接字符,使用#{}将无法实现此功能。

    //计算记录数,拼接表名
    <select id="findTableCount" parameterType ="java.lang.String" resultType="int">
             select count(*) from ${value}
    </select>
    //可以根据传入的表名,返回不同表中的记录数
    int i = sqlSession.selectOne("EmpDao.findTableCount", "dept");
    
    //order by排序,拼接列名
    ORDER BY ${columnName}
    

    二、ResultType

           使用resultType可以将查询结果映射为pojo类型,但需要pojo的属性名和sql查询的列名一致方可映射成功。如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。

    1、基本数据类型

    <select id="findEmpCount" resultType="int">
    	select count(1) from emp
    </select>
    //原始Dao开发方式
    int i = sqlSession.selectOne("EmpDao.findEmpCount", null);
    //Mapper代理的方式
    int count = empMapper.findEmpCount(emp);

             输出简单类型必须查询出来的结果集只有一条记录,最终将第一个字段的值转换为输出类型。

    2、pojo类对象

    //输出pojo对象
    <select id="findUserById" parameterType="int" resultType="user">
    	select * from user where id = #{id}
    </select>
    
    //输出pojo列表
    <select id="findUserByUsername" parameterType="string" resultType="user">
    	select * from user where username like '%${value}%'
    </select>

           输出pojo对象和输出pojo列表在sql中定义的resultType是一样的,但在mapper.java指定的方法返回值类型不一样。生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList (返回集合对象调用 )。

           返回单个pojo对象,要保证sql查询出来的结果集为单条,内部使用session.selectOne方法调用,mapper接口使用pojo对象作为方法返回值。

           返回pojo列表,表示查询出来的结果集可能为多条,内部使用session.selectList方法,mapper接口使用List<pojo>对象作为方法返回值。

    3、hashMap

           输出pojo对象可以改用hashmap输出类型,将输出的字段名称作为map的key,value为字段值。

    <select id="findEmpById" parameterType="int" resultType="map">
    	select * from emp where empno = #{empno}
    </select>
    
    //查询记录
    HashMap<String, Object>  emp = sqlSession.selectOne("EmpDao.findEmpById", 7369);
    //获取键值对
    System.out.println(""+emp.get("EMPNO")+ emp.get("ename"));

    三、ResultMap结果集

            如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,但实质上还需要将查询结果映射到pojo对象中。

           resultMap还可以实现将查询结果映射为复杂类型的pojo,完成高级输出结果映射。比如在查询结果映射对象中包括pojo和list实现关联查询。

    //定义ResultMap
    <resultMap type="po.Emp" id="empResultMap">
    	<id column="empno" property="id"/>
    	<result column="ename" property="name"/>
    </resultMap>
    
    
    //引用ResultMap
    <select id="findEmpById" parameterType="int" resultType="empResultMap">
    	select * from emp where empno = #{empno}
    </select

    (1)定义resultMap的参数含义

                 type:resultMap最终映射的java对象类型的 全名 或 别名

                 id:对resultMap的唯一标识。

    (2) 定义映射关系的参数含义

                 <id />:查询结果集的唯一标识。如果是多个字段为复合唯一约束则定义多个<id/>。

                <result />:对普通名映射定义。

                Column:表示sql查询出来的字段名 或 别名。

                property:type指定的pojo类型中的属性名或 别名。

  • 相关阅读:
    ORA12560: TNS: 协议适配器错误
    eclipse无法识别Web项目的问题
    搭建eclipse+tomcat开发环境
    初探弹出层的实现原理
    样式可控的左右选择组件
    在TSQL中用队列来遍历层级数据
    复利计算工具 wpf
    浏览WPF中内置颜色名对应的颜色
    原创:通过VS 2010+SVN为SQL Server提供数据库版本管理
    原创:学习英语小助手(阅读粘贴的英文,使用MVVM)
  • 原文地址:https://www.cnblogs.com/sh086/p/8380539.html
Copyright © 2020-2023  润新知