使用map存储结果集
使用规范
- 原则上,所有的结果集都可以存储到
map
- 但使用
map
存储,会大大降低阅读性 mapper文件
中使用map
存储结果的参考<select id="xxx" resultType="map"> ... </select>
测试步骤参考
- 全局配置文件和实体类省略
mapper文件
配置<!--使用 map 存储结果集 --> <select id="get_map" resultType="map"> select * from emp </select>
mapper接口
省略- 主要测试类部分截取
Test_resultType dao = session.getMapper(Test_resultType.class); // 将结果对象依次封装到 map 实例中 // 以 key-value 的形式 // key 表示属性; value 表示属性值 List<Map<?, ?>> list = dao.get_map(); for (Map<?, ?> map : list) { log.info(map); }
- 校验输出部分截取
DEBUG 2019-01-13 00:18:24,865 org.apache.ibatis.logging.jdbc.BaseJdbcLogger : <== Total: 15 INFO 2019-01-13 00:18:24,866 siye.ibatis.test.Test_mapper_conf : {sex=1, name=xiaoli, id=1, age=23} INFO 2019-01-13 00:18:24,866 siye.ibatis.test.Test_mapper_conf : {sex=1, name=xiaohong, id=2, age=28} INFO 2019-01-13 00:18:24,866 siye.ibatis.test.Test_mapper_conf : {sex=1, name=xiaofang, id=3, age=28} INFO 2019-01-13 00:18:24,866 siye.ibatis.test.Test_mapper_conf : {sex=1, name=xiaofang, id=4, age=28} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {sex=1, name=xiaofang, id=5, age=28} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {sex=1, name=xiaofang, id=6, age=28} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {sex=0, name=xiaoHua, id=7, age=90} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {sex=0, name=xiaoHua, id=8, age=87} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {sex=0, name=xiaoLi, id=9, age=66} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {sex=0, name=Rose, id=10, age=23} INFO 2019-01-13 00:18:24,867 siye.ibatis.test.Test_mapper_conf : {name=xiao_zhu, id=11} INFO 2019-01-13 00:18:24,868 siye.ibatis.test.Test_mapper_conf : {name=xiao_zhu, id=12} INFO 2019-01-13 00:18:24,868 siye.ibatis.test.Test_mapper_conf : {name=xiao_zhu, id=13} INFO 2019-01-13 00:18:24,868 siye.ibatis.test.Test_mapper_conf : {sex=0, name=xiaoHua, id=14, age=34} INFO 2019-01-13 00:18:24,868 siye.ibatis.test.Test_mapper_conf : {sex=0, name=xiaoHua, id=15, age=83}
使用POJO存储结果集
使用规范
- 只需指定合法的
POJO
类对象的正确包路径即可 - 可以是
完全限定名
,也可以是别名
- 强大的
可读性
,和对执行结果的便捷操作
,较为推荐的方式 mapper文件
中使用POJO
存储结果集的参考<select id="xxx" resultType="object_qualifier_name"> ... </select>
测试步骤参考
- 全局配置文件和实体类省略
mapper文件
的配置<!--使用 POJO 存储结果集 --> <select id="get_pojo" resultType="siye.ibatis.entity.Emp"> select * from emp </select>
mapper接口
省略- 主要测试类部分截取
Test_resultType dao = session.getMapper(Test_resultType.class); // 依据 POJO 映射封装,依次填入到 list 实例中 List<Emp> list = dao.get_pojo(); for (Emp emp : list) { log.info(emp); }
- 校验输出部分截取
DEBUG 2019-01-13 00:25:10,463 org.apache.ibatis.logging.jdbc.BaseJdbcLogger : <== Total: 15 INFO 2019-01-13 00:25:10,463 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@150c158 INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@124411f INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@1e7803 INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@dba097 INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@186f9d5 INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@4d6e83 INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@1a0cee9 INFO 2019-01-13 00:25:10,464 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@b21112 INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@da0940 INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@178436e INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@6a04ae INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@ceb130 INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@a035a0 INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@1b467e9 INFO 2019-01-13 00:25:10,465 siye.ibatis.test.Test_mapper_conf : siye.ibatis.entity.Emp@1db50c5