目录mybatis返回单一对象或对象列表一、说明二、代码测试UserMap.xml映射文件dao文件UserMap.java测试代码和结果文件mybatis返回的对象包含集合mybatis返回单一对象或对象列表 一、说明 返回数据
<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 非常的智能,返回值统一使用 resultMap="BaseResultMap",mybatis会根据查询到的条目数量自动进行判断,如果是一条就返回对象,如果是多条就返回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 -->
public interface UserMapper {
User selectByPrimaryKey(Long id);
User selectByUserName(String username );
List<User> selectByEmail(String email );
}
@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));
}
}
}
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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程界。
--结束END--
本文标题: 浅谈mybatis返回单一对象或对象列表的问题
本文链接: https://www.lsjlt.com/news/100.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2023-09-28
2023-09-28
2023-09-28
2023-09-28
2023-09-28
2023-09-28
2023-09-28
2023-09-28
2023-09-28
2023-09-27
回答
回答
回答
回答
回答
回答
回答
回答
0