好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

浅谈mybatis返回单一对象或对象列表的问题

mybatis返回单一对象或对象列表

一、说明

返回数据类型由dao中的接口和map.xml文件共同决定。另外,不论是返回单一对象还是对象列表,***map.xml中的配置都是一样的,都是resultMap=]***Map]或resultType=[* .* .*]类型. 每一次mybatis从数据库中select数据之后,都会检查数据条数和dao中定义的返回值是否匹配。 若返回一条数据,dao中定义的返回值是一个对象或对象的List列表,则可以正常匹配,将查询的数据按照dao中定义的返回值存放。 若返回多条数据,dao中定义的返回值是一个对象,则无法将多条数据映射为一个对象,此时mybatis报错。

二、代码测试

UserMap.xml映射文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

< 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 -->

dao文件UserMap.java

?

1

2

3

4

5

6

7

8

9

10

11

public interface UserMapper {

     User selectByPrimaryKey(Long id);

     User selectByUserName(String username );

     /**关于mybatis返回单一对象或对象列表的问题:

      * 1.返回数据类型由dao中的接口和*map.xml文件共同决定。另外,不论是返回单一对象还是对象列表,*map.xml中的配置都是一样的,都是resultMap="*Map"*或resultType=[* .* .*]类型.

      * 2.每一次mybatis从数据库中select数据之后,都会检查数据条数和dao中定义的返回值是否匹配。

      * 3.若返回一条数据,dao中定义的返回值是一个对象或对象的List列表,则可以正常匹配,将查询的数据按照dao中定义的返回值存放。

      * 4.若返回多条数据,dao中定义的返回值是一个对象,则无法将多条数据映射为一个对象,此时mybatis报错。

      * */

     List<User> selectByEmail(String email );

}

测试代码和结果文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@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

?

1

2

3

4

5

6

7

8

9

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

?

1

2

3

4

5

6

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<? 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 >

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/kangsenkangsen/article/details/51236279

查看更多关于浅谈mybatis返回单一对象或对象列表的问题的详细内容...

  阅读:38次