Java教程

MyBatis学习06--resultType深入

本文主要是介绍MyBatis学习06--resultType深入,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package com.itheima.dao;

import com.itheima.pojo.User;
import com.itheima.pojo.UserInfo;

import java.util.List;
import java.util.Map;

/**
 * @author qiuji
 * @version 1.0
 * @date 2021-08-08 17:01
 *mybatis的映射配置文件的resultType表示将查询到的结果封装到什么里面
 * 1. 查询的结果是单个数据, 映射配置文件中的resultType属性的值就是这个数据的类型
 *
 * 2. 查询的结果是一行数据:
 *  2.1 将这一行数据存储到POJO对象中, 映射配置文件的resultType的值就是POJO的全限定名或者别名
 *      ,此时就要求查询结果的字段名和类型要和POJO的属性名和类型一致
 * 3. 查询的结果是多行数据:
 *      2.1 将多条数据存储到List<POJO>中,映射配置文件的resultType的值就是POJO的别名
 *      2.2 将多条数据存储到List<Map>中,映射配置文件的resultType的值就是map
 *4. 查询一行或者多行数据,将查询结果封装到POJO对象中,并且POJO的属性名和表的字段名不一致
 *     使用resultMap进行手动映射
 */
public interface UserDao {

    /**
     * 查询用户的总个数
     * @return
     */
    Long findTotal();

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    User findById(int id);

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    Map findByUsername(String username);


    /**
     * 根据关键词 模糊查询用户结果
     * @param username
     * @return
     */
    List<Map> findByAllUsername(String username);

    List<UserInfo> findAllUserInfo();
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
    根标签mapper,一个映射配置文件,就对应一个dao接口
    根标签的namespace属性的值就对应dao接口的全限定名
-->
<mapper namespace="com.itheima.dao.UserDao">

    <select id="findTotal" resultType="long">
        select count(*) from t_user
    </select>

    <select id="findById"  parameterType="int" resultType="User" >
        select * from t_user where uid= #{id}
    </select>

    <select id="findByUsername" parameterType="string" resultType="map">
        select * from t_user where username=#{username}
    </select>

    <select id="findByAllUsername" parameterType="string" resultType="map">
        select * from t_user where username like "%"#{username}"%"
    </select>


    <!--
        resultType属性会进行自动映射: 根据结果集的字段名和POJO的属性名的对应关系进行映射
        resultMap属性: 结果集映射(手动映射),我们要先使用resultMap标签编写一个手动映射规则,
        然后使用这个映射规则
    -->
    <!--
        id就是这个映射规则的唯一标识
        type就是要进行手动映射的类型:UserInfo
        autoMapping="true" 表示能自动映射的就会进行自动映射,
        不能自动映射的属性,才进行手动映射
    -->

    <resultMap id="userInfoMap" type="UserInfo" autoMapping="true">
        <!--
            id标签表示对主键进行映射
                column属性是要进行映射的主键的字段名(列名)
                property是要进行映射的POJO的属性名
        -->
        <id column="uid" property="userId"></id>
        <!--
            result标签就是对其它的非主键进行映射
        -->
        <result column="sex" property="userSex"></result>
        <result column="birthday" property="userBirthday"></result>
        <result column="address" property="userAddress"></result>
    </resultMap>

    <select id="findAllUserInfo" resultMap="userInfoMap">
        select * from t_user
    </select>
</mapper>

小结

1.输出简单类型 直接写 java类型名 eg: int
2.输出pojo对象 直接写 pojo类型名 eg: User
3.输出pojo列表类型 写 列表里面的泛型的类型 eg: List 写User
4.ResultMap
- 解决查询出来的结果的列名和javaBean属性不一致的请求

这篇关于MyBatis学习06--resultType深入的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!