关于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
踩
相关文章推荐
- 关于mybatis返回单一对象或对象列表的问题
- 关于mybatis返回单一对象或对象列表的问题
- 关于mybatis 使用case when 计算BigDecimal 数据返回对象的问题
- 关于C++中输入操作符(>>)返回输入流对象的的问题
- mybatis关于一对一映射返回结果与数据库查出的不符问题
- 关于Mybatis一次性插入多条数据返回的记录数的问题
- mybatis执行insert语句,如何返回主键给对象的问题(限主键为整数类型)
- Mybatis 返回对象列表
- 关于mybatis查询出错,异常情况下返回值的问题
- [小技巧]关于spring boot 整合mybatis-plus: value为null时不返回的问题。
- 关于json格式对象返回页面时间字段显示NANA的问题
- mybatis返回对象为null或者0的问题
- 关于返回对象或变量的引用(reference)的问题
- 使用PageHelper插件分页结合mybatis返回的列表个数不对问题解决
- mybatis关于插入返回主键ID问题(mysql)
- 关于PHP中查询返回结果集和对象问题
- 【C++总结】关于函数返回对象析构的问题
- 关于spring MVC 返回集合或对象自动转成json串的问题
- MyBatis关于返回时间的问题
- 关于MyBatis中insert返回值与SQLserver触发器/存储过程返回执行结果的问题