淺談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ù)量自動進(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.返回數(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 Scanner對象中hasNext()與next()方法的使用
這篇文章主要介紹了Java Scanner對象中hasNext()與next()方法的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
java多態(tài)實現(xiàn)電子寵物系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java多態(tài)實現(xiàn)電子寵物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
使用springBoot中的info等級通過druid打印sql
這篇文章主要介紹了使用springBoot中的info等級通過druid打印sql,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

