欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

一、說明

  • 返回?cái)?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報(bào)錯。

二、代碼測試

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ù)量自動進(jìn)行判斷,如果是一條就返回對象,如果是多條就返回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.返回?cái)?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報(bào)錯。
     * */
    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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中集成日志的四種方式

    SpringBoot中集成日志的四種方式

    在開發(fā)中,日志記錄是保障應(yīng)用程序健壯性、可維護(hù)性的重要手段,通過日志,我們可以記錄系統(tǒng)的運(yùn)行狀態(tài)、捕獲異常并進(jìn)行調(diào)試,Spring Boot 默認(rèn)使用的是 Logback,但你也可以根據(jù)需求選擇其他框架,以下是幾種常用的日志集成方法,需要的朋友可以參考下
    2024-10-10
  • JDK10中的局部變量類型推斷var

    JDK10中的局部變量類型推斷var

    這篇文章主要介紹了JDK10中的局部變量類型推斷var,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java Scanner對象中hasNext()與next()方法的使用

    Java Scanner對象中hasNext()與next()方法的使用

    這篇文章主要介紹了Java Scanner對象中hasNext()與next()方法的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java多態(tài)實(shí)現(xiàn)電子寵物系統(tǒng)

    java多態(tài)實(shí)現(xiàn)電子寵物系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java多態(tài)實(shí)現(xiàn)電子寵物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • spring關(guān)于組件的注入及獲取流程場景分析

    spring關(guān)于組件的注入及獲取流程場景分析

    這篇文章主要介紹了spring關(guān)于組件的注入及獲取流程場景分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-07-07
  • java多線程創(chuàng)建及線程安全詳解

    java多線程創(chuàng)建及線程安全詳解

    多線程是日常開發(fā)中的常用知識,也是難用知識。通這篇文章主要給大家介紹了關(guān)于java多線程創(chuàng)建及線程安全的相關(guān)資料,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • SpringBoot 整合Jest實(shí)例代碼講解

    SpringBoot 整合Jest實(shí)例代碼講解

    本文通過實(shí)例代碼給大家介紹了SpringBoot 整合Jest的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • 深入淺析Spring 的aop實(shí)現(xiàn)原理

    深入淺析Spring 的aop實(shí)現(xiàn)原理

    AOP(Aspect-OrientedProgramming,面向方面編程),可以說是OOP(Object-Oriented Programing,面向?qū)ο缶幊蹋┑难a(bǔ)充和完善。本文給大家介紹Spring 的aop實(shí)現(xiàn)原理,感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • java最新版本連接mysql失敗的解決過程

    java最新版本連接mysql失敗的解決過程

    這篇文章主要給大家介紹了關(guān)于java最新版本連接mysql失敗的解決過程,文中通過圖文以及示例代碼將解決的過程介紹的非常詳細(xì),對遇到這個問題的同學(xué)具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-09-09
  • 使用springBoot中的info等級通過druid打印sql

    使用springBoot中的info等級通過druid打印sql

    這篇文章主要介紹了使用springBoot中的info等級通過druid打印sql,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論