最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴}

 更新時間:2021年08月25日 10:18:00   作者:MichaelChansn  
這篇文章主要介紹了淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴},具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

mybatis返回單一對象或?qū)ο罅斜?/h2>

一、說明

  • 返回數(shù)據(jù)類型由dao中的接口和map.xml文件共同決定。另外,不論是返回單一對象還是對象列表,***map.xml中的配置都是一樣的,都是resultMap=”***Map”或resultType=“* .* .*”類型.
  • 每一次mybatis從數(shù)據(jù)庫中select數(shù)據(jù)之后,都會檢查數(shù)據(jù)條數(shù)和dao中定義的返回值是否匹配。
  • 若返回一條數(shù)據(jù),dao中定義的返回值是一個對象或?qū)ο蟮腖ist列表,則可以正常匹配,將查詢的數(shù)據(jù)按照dao中定義的返回值存放。
  • 若返回多條數(shù)據(jù),dao中定義的返回值是一個對象,則無法將多條數(shù)據(jù)映射為一個對象,此時mybatis報錯。

二、代碼測試

UserMap.xml映射文件

<resultMap id="BaseResultMap" type="com.ks.ssm.domain.User" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="qq" property="qq" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="gender" property="gender" jdbcType="BIT" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
    <result column="city" property="city" jdbcType="VARCHAR" />
    <result column="mood" property="mood" jdbcType="VARCHAR" />
    <result column="single" property="single" jdbcType="BIT" />
    <result column="enrolltime" property="enrolltime" jdbcType="TIMESTAMP" />
    <result column="level" property="level" jdbcType="TINYINT" />
    <result column="status" property="status" jdbcType="BIT" />
    <result column="titlepic" property="titlepic" jdbcType="VARCHAR" />
    <result column="job" property="job" jdbcType="VARCHAR" />
    <result column="logintime" property="logintime" jdbcType="TIMESTAMP" />
    <result column="loginip" property="loginip" jdbcType="VARCHAR" />
    <result column="token" property="token" jdbcType="VARCHAR" />
    <result column="modifytime" property="modifytime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password, email, qq, phone, gender, birthday, city, mood, single, enrolltime, 
    level, status, titlepic, job, logintime, loginip, token, modifytime
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where id = #{id,jdbcType=BIGINT}
  </select>
  <!-- add by ks -->
    <select id="selectByUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where username = #{username,jdbcType=VARCHAR}
   </select>
   <!-- mybatis 非常的智能,返回值統(tǒng)一使用 resultMap="BaseResultMap",mybatis會根據(jù)查詢到的條目數(shù)量自動進行判斷,如果是一條就返回對象,如果是多條就返回List對象列表-->
  <select id="selectByEmail" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where email = #{email,jdbcType=VARCHAR}
   </select>
   <!-- end by ks -->

dao文件UserMap.java

public interface UserMapper {
    User selectByPrimaryKey(Long id);
    User selectByUserName(String username );
    /**關(guān)于mybatis返回單一對象或?qū)ο罅斜淼膯栴}:
     * 1.返回數(shù)據(jù)類型由dao中的接口和*map.xml文件共同決定。另外,不論是返回單一對象還是對象列表,*map.xml中的配置都是一樣的,都是resultMap="*Map"*或resultType=“* .* .*”類型.
     * 2.每一次mybatis從數(shù)據(jù)庫中select數(shù)據(jù)之后,都會檢查數(shù)據(jù)條數(shù)和dao中定義的返回值是否匹配。
     * 3.若返回一條數(shù)據(jù),dao中定義的返回值是一個對象或?qū)ο蟮腖ist列表,則可以正常匹配,將查詢的數(shù)據(jù)按照dao中定義的返回值存放。
     * 4.若返回多條數(shù)據(jù),dao中定義的返回值是一個對象,則無法將多條數(shù)據(jù)映射為一個對象,此時mybatis報錯。
     * */
    List<User> selectByEmail(String email );
}

測試代碼和結(jié)果文件

@RunWith(SpringJUnit4ClassRunner.class)     //表示繼承了SpringJUnit4ClassRunner類
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TestMyBatis {
  private static Logger logger = Logger.getLogger(TestMyBatis.class);
  @Resource
  private UserMapper userDao;
  @Test
  public void testMybatis() {
    User user = userDao.selectByUserName("ks");
    logger.info("user.........................");
    logger.info(JSON.toJSONString(user));
    List<User> users=userDao.selectByEmail("ks");
    logger.info("list.........................");
    for(User userTemp : users)
    {
        logger.info(JSON.toJSONString(userTemp));
    }
  }
}

測試結(jié)果

mybatis 返回的對象包含集合

DeviceQuestionInstruction.java

import com.hikari.cloud.data.entity.TbInstruction;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
public class DeviceQuestionInstruction {//tb_instruction  使用說明表
    private String dvqsTitle;
    private List<TbInstruction> instructionList;
}

TbInstruction.java

import lombok.Data;
import java.util.Date;
@Data
public class TbInstruction {//tb_instruction  使用說明表
    private Long id;
    private Long userId;
    private String title;
    private String detail;
    private String url;
    private Integer type;
    private Integer suffix;
    private String deviceCategory;
    private String deviceTypeName;
    private String deviceTypeNum;
    private Integer views;
    private Long dvqsId;
    private Integer dvqsLevel;
    private Date gmtCreate;
}

TbDeviceQuestionMapper.java

import com.hikari.cloud.data.bean.DeviceQuestionInstruction;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface TbDeviceQuestionMapper {
    List<DeviceQuestionInstruction> findByNo(@Param("deviceTypeNo") String deviceTypeNo);
}

TbDeviceQuestionMapper.xml

<?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 namespace="com.hikari.cloud.data.mapper.TbDeviceQuestionMapper">
    <resultMap id="dataMap" type="com.hikari.cloud.data.bean.DeviceQuestionInstruction">
        <result column="dvqs_title" property="dvqsTitle"/>
        <collection property="instructionList" resultMap="insResultMap"/>
    </resultMap>
    <resultMap id="insResultMap" type="com.hikari.cloud.data.entity.TbInstruction">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="title" property="title"/>
        <result column="detail" property="detail"/>
        <result column="url" property="url"/>
        <result column="type" property="type"/>
        <result column="suffix" property="suffix"/>
        <result column="device_category" property="deviceCategory"/>
        <result column="device_type_name" property="deviceTypeName"/>
        <result column="device_type_num" property="deviceTypeNum"/>
        <result column="views" property="views"/>
        <result column="dvqs_id" property="dvqsId"/>
        <result column="dvqs_level" property="dvqsLevel"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>
    <select id="findByNo" resultType="com.hikari.cloud.data.bean.DeviceQuestionInstruction" resultMap="dataMap">
        SELECT tb_device_question.title AS dvqs_title,tb_instruction.* FROM tb_device_question
        LEFT JOIN tb_instruction
        ON tb_device_question.id=tb_instruction.dvqs_id
        WHERE tb_device_question.device_type_no='HSAT-K5'
        ORDER BY tb_instruction.dvqs_level ASC
    </select>
</mapper>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實現(xiàn)簡單的郵件發(fā)送功能

    Java實現(xiàn)簡單的郵件發(fā)送功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單的郵件發(fā)送功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Java中正則表達式的語法以及matches方法的使用方法

    Java中正則表達式的語法以及matches方法的使用方法

    正則表達式(Regular Expression)是一門簡單語言的語法規(guī)范,是強大、便捷、高效的文本處理工具,這篇文章主要給大家介紹了關(guān)于Java中正則表達式的語法以及matches方法的使用方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-05-05
  • java streamfilter list 過濾的實現(xiàn)

    java streamfilter list 過濾的實現(xiàn)

    Java Stream API中的filter方法是過濾List集合中元素的一個強大工具,可以輕松地根據(jù)自定義條件篩選出符合要求的元素,本文就來介紹一下java streamfilter list 過濾的實現(xiàn),感興趣的可以了解一下
    2025-03-03
  • Java多線程生產(chǎn)者消費者模式實現(xiàn)過程解析

    Java多線程生產(chǎn)者消費者模式實現(xiàn)過程解析

    這篇文章主要介紹了Java多線程生產(chǎn)者消費者模式實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Java數(shù)字圖像處理之圖像灰度處理

    Java數(shù)字圖像處理之圖像灰度處理

    這篇文章主要為大家詳細介紹了Java數(shù)字圖像處理之圖像灰度處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Spring中事務用法示例及實現(xiàn)原理詳解

    Spring中事務用法示例及實現(xiàn)原理詳解

    這篇文章主要給大家介紹了關(guān)于Spring中事務用法示例及實現(xiàn)原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • Java實現(xiàn)批量修改txt文件名稱的方法示例

    Java實現(xiàn)批量修改txt文件名稱的方法示例

    這篇文章主要介紹了Java實現(xiàn)批量修改txt文件名稱的方法,結(jié)合實例形式分析了Java針對目錄文件遍歷及文件讀寫、屬性操作等相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • Java并發(fā)LinkedBlockingQueue源碼分析

    Java并發(fā)LinkedBlockingQueue源碼分析

    這篇文章主要為大家介紹了Java并發(fā)LinkedBlockingQueue源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)

    詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)

    這篇文章主要介紹了詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Java中字節(jié)流和字符流的區(qū)別與聯(lián)系

    Java中字節(jié)流和字符流的區(qū)別與聯(lián)系

    Java中的字節(jié)流和字符流是用于處理輸入和輸出的兩種不同的流,本文主要介紹了Java中字節(jié)流和字符流的區(qū)別與聯(lián)系,字節(jié)流以字節(jié)為單位進行讀寫,適用于處理二進制數(shù)據(jù),本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2024-12-12

最新評論

砚山县| 游戏| 东台市| 沐川县| 荣昌县| 德昌县| 永定县| 新河县| 耒阳市| 乃东县| 沈阳市| 潼南县| 永德县| 临洮县| 长子县| 洞口县| 隆子县| 尼勒克县| 杭州市| 中方县| 营山县| 潮安县| 潮州市| 邵阳市| 南通市| 宁海县| 襄垣县| 宁蒗| 嘉峪关市| 二连浩特市| 多伦县| 开原市| 弥勒县| 咸丰县| 阿尔山市| 龙江县| 吉木萨尔县| 务川| 保山市| 资中县| 苍南县|