您的位置:首页 > 其它

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

2016-11-28 11:46 549 查看


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

一.说明

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

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

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

若返回多条数据,dao中定义的返回值是一个对象,则无法将多条数据映射为一个对象,此时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 非常的智能,返回值统一使用 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 -->
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
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


dao文件UserMap.java:

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14

测试代码和结果文件:

@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));
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20





顶0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: