淺談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));
}
}
}

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中正則表達(dá)式的語法以及matches方法的使用方法
正則表達(dá)式(Regular Expression)是一門簡單語言的語法規(guī)范,是強大、便捷、高效的文本處理工具,這篇文章主要給大家介紹了關(guān)于Java中正則表達(dá)式的語法以及matches方法的使用方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
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)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Spring中事務(wù)用法示例及實現(xiàn)原理詳解
這篇文章主要給大家介紹了關(guān)于Spring中事務(wù)用法示例及實現(xiàn)原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
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ù)的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
Java中字節(jié)流和字符流的區(qū)別與聯(lián)系
Java中的字節(jié)流和字符流是用于處理輸入和輸出的兩種不同的流,本文主要介紹了Java中字節(jié)流和字符流的區(qū)別與聯(lián)系,字節(jié)流以字節(jié)為單位進行讀寫,適用于處理二進制數(shù)據(jù),本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-12-12

