zoukankan      html  css  js  c++  java
  • mybatis学习笔记五(映射)

    通过parameterType指定输入参数的类型,类型可以是

    • 简单类型
    • hashmap
    • pojo的包装类型

    输出映射有两种方式

    • resultType
    • resultMap

    传递pojo的包装对象

    • 定义包装类型pojo
    package com.iot.mybatis.po;
    
    /**
     * Created by Brian on 2016/2/24.
     */
    public class UserQueryVo {
    
        //在这里包装所需要的查询条件
    
        //用户查询条件
        private UserCustom userCustom;
    
        public UserCustom getUserCustom() {
            return userCustom;
        }
    
        public void setUserCustom(UserCustom userCustom) {
            this.userCustom = userCustom;
        }
    
        //可以包装其它的查询条件,订单、商品
        //....
    
    }

    其中,UserCustom类继承User

    public class UserCustom extends User{
    }
    • mapper.xml

    在UserMapper.xml中定义用户信息综合查询(查询条件复杂,通过高级查询进行复杂关联查询)。

        <!-- 用户信息综合查询
            #{userCustom.sex}:取出pojo包装对象中性别值
            ${userCustom.username}:取出pojo包装对象中用户名称
         -->
        <select id="findUserList" parameterType="com.iot.mybatis.po.UserQueryVo"
                resultType="com.iot.mybatis.po.UserCustom">
            SELECT * FROM user WHERE user.sex=#{userCustom.sex} AND user.username LIKE '%${userCustom.username}%'
        </select>

    注意不要将#{userCustom.sex}中的userCustom写成UserCustom,前者指属性名(由于使用IDE提示自动补全,所以只是把类型名首字母小写了),后者指类型名,这里是UserQueryVo类中的userCustom属性,是属性名。写错会报如下异常:

    org.apache.ibatis.exceptions.PersistenceException: 
    ### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'UserCustom' in 'class com.iot.mybatis.po.UserQueryVo'
    ### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'UserCustom' in 'class com.iot.mybatis.po.UserQueryVo'
    • mapper.java
    //用户信息综合查询
    public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;
    • 测试代码
    //用户信息的综合 查询
        @Test
        public void testFindUserList() throws Exception {
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //创建UserMapper对象,mybatis自动生成mapper代理对象
            UserMapper userMapper  sqlSession.getMapper(UserMapper.class);
    
            //创建包装对象,设置查询条件
            UserQueryVo userQueryVo = new UserQueryVo();
            UserCustom userCustom = new UserCustom();
            //由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
            userCustom.setSex("1");
            userCustom.setUsername("张三");
            userQueryVo.setUserCustom(userCustom);
            //调用userMapper的方法
    
            List<UserCustom> list = userMapper.findUserList(userQueryVo);
    
            System.out.println(list);
    
    
        }

    resultType

    • 使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
    • 如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。
    • 只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。

    输出简单类型

    • mapper.xml
     <!-- 用户信息综合查询总数
            parameterType:指定输入类型和findUserList一样
            resultType:输出结果类型
        -->
        <select id="findUserCount" parameterType="com.iot.mybatis.po.UserQueryVo" resultType="int">
            SELECT count(*) FROM user WHERE user.sex=#{userCustom.sex} AND user.username LIKE '%${userCustom.username}%'
        </select>
    • mapper.java
        //用户信息综合查询总数
        @Test
        public void testFindUserCount() throws Exception {
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //创建UserMapper对象,mybatis自动生成mapper代理对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
            //创建包装对象,设置查询条件
            UserQueryVo userQueryVo = new UserQueryVo();
            UserCustom userCustom = new UserCustom();
            //由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
            userCustom.setSex("1");
            userCustom.setUsername("小");
            userQueryVo.setUserCustom(userCustom);
            //调用userMapper的方法
    
            int count = userMapper.findUserCount(userQueryVo);
    
            System.out.println(count);
    
    
        }
    • 小结

    查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射。

    输出pojo对象和pojo列表

    不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。

    在mapper.java指定的方法返回值类型不一样:

    • 输出单个pojo对象,方法返回值是单个对象类型
    //根据id查询用户信息
    public User findUserById(int id) throws Exception;
    • 输出pojo对象list,方法返回值是List
    //根据用户名列查询用户列表
    public List<User> findUserByName(String name) throws Exception;

    生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList (返回集合对象调用 ).

    resultMap

    mybatis中使用resultMap完成高级输出结果映射。(一对多,多对多)

    resultMap使用方法

    如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

    1.定义resultMap

    2.使用resultMap作为statement的输出映射类型

    • 定义reusltMap
    <!-- 定义resultMap
        将SELECT id id_,username username_ FROM USER 和User类中的属性作一个映射关系
    
        type:resultMap最终映射的java对象类型,可以使用别名
        id:对resultMap的唯一标识
         -->
         <resultMap type="user" id="userResultMap">
            <!-- id表示查询结果集中唯一标识 
            column:查询出来的列名
            property:type指定的pojo类型中的属性名
            最终resultMap对column和property作一个映射关系 (对应关系)
            -->
            <id column="id_" property="id"/>
            <!-- 
            result:对普通名映射定义
            column:查询出来的列名
            property:type指定的pojo类型中的属性名
            最终resultMap对column和property作一个映射关系 (对应关系)
             -->
            <result column="username_" property="username"/>
    
         </resultMap>
    • 使用resultMap作为statement的输出映射类型
    <!-- 使用resultMap进行输出映射
            resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前边需要加namespace
            -->
        <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
            SELECT id id_,username username_ FROM USER WHERE id=#{value}
        </select>
    
    • mapper.java
    //根据id查询用户信息,使用resultMap输出
    public User findUserByIdResultMap(int id) throws Exception;
    • 测试代码
    @Test
    public void testFindUserByIdResultMap() throws Exception {
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
    
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
        //调用userMapper的方法
    
        User user = userMapper.findUserByIdResultMap(1);
    
        System.out.println(user);
    
    
    }

    小结

    使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。

    如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

     
  • 相关阅读:
    mac 使用brew 安装php-redis
    thinkphp6 使用redis 实现消息队列
    Redis 桌面管理器:Another Redis Desktop Manager
    linux 查看并关闭shell脚本执行
    MySQL教程之concat以及group_concat的用法
    PHP redis 使用
    thinkphp6 command(自定义指令)
    git 使用
    linux shell中 "2>&1"含义
    linux crontab 定时任务
  • 原文地址:https://www.cnblogs.com/osghong/p/9554726.html
Copyright © 2011-2022 走看看