Mybatis输出参数之简单类型与resultMap的使用

一. Mybatis输出参数为int类型
需求:查询用户表数据条数
sql:SELECT count(*) FROM user
在UserMapper.xml中配置sql,如下图:

image.png

在UserMapper添加方法,如下图:
image.png

在UserMapeprTest增加测试方法,如下:
@Test
public void testQueryUserCount() {
// mybatis和spring整合,整合之后,交给spring管理
SqlSession sqlSession = this.sqlSessionFactory.openSession();
// 创建Mapper接口的动态代理对象,整合之后,交给spring管理
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

// 使用userMapper执行查询用户数据条数
int count = userMapper.queryUserCount();
System.out.println(count);

// mybatis和spring整合,整合之后,交给spring管理
sqlSession.close();

}
二 . Mybatis输出参数为POJO类型
resultType可以指定将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。如果sql查询字段名和pojo的属 性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
需求:查询订单表order的所有数据
sql:SELECT id, user_id, number, createtime, note FROM order
数据库表如下图:

image.png

Order对象:
public class Order {
// 订单id
private int id;
// 用户id
private Integer userId;
// 订单号
private String number;
// 订单创建时间
private Date createtime;
// 备注
private String note;
get/set。。。
}
创建OrderMapper.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="cn.itcast.mybatis.mapper.OrderMapper">

<select id="queryOrderAll" resultType="Order">
SELECT id, user_id,
number,
createtime, note FROM order
</select>
</mapper>
Mapper接口 OrderMapper 编写接口如下:
public interface OrderMapper {
/**
* 查询所有订单
*
* @return
*/
List<Order> queryOrderAll();
}
编写测试方法OrderMapperTest如下:
public class OrderMapperTest {
private SqlSessionFactory sqlSessionFactory;

@Before
public void init() throws Exception {
    InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}

@Test
public void testQueryAll() {
    // 获取sqlSession
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // 获取OrderMapper
    OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

    // 执行查询
    List<Order> list = orderMapper.queryOrderAll();
    for (Order order : list) {
        System.out.println(order);
    }
}

}
测试效果如下图:


image.png

发现userId为null
解决方案:使用resultMap


使用resultMap来解决上面的问题
由于上边的mapper.xml中sql查询列(user_id)和Order类属性(userId)不一致,所以查询结果不能映射到pojo中。
需要定义resultMap,把orderResultMap将sql查询列(user_id)和Order类属性(userId)对应起来
改造OrderMapper.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="cn.zyh.mybatis.mapper.OrderMapper">

<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
<!-- id:设置ResultMap的id -->
<resultMap type="order" id="orderResultMap">
    <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id-->
    <!-- property:主键在pojo中的属性名 -->
    <!-- column:主键在数据库中的列名 -->
    <id property="id" column="id" />

    <!-- 定义普通属性 -->
    <result property="userId" column="user_id" />
    <result property="number" column="number" />
    <result property="createtime" column="createtime" />
    <result property="note" column="note" />
</resultMap>

<!-- 查询所有的订单数据 -->
<select id="queryOrderAll" resultMap="orderResultMap">
    SELECT id, user_id,
    number,
    createtime, note FROM `order`
</select>

</mapper>
只需要修改Mapper.xml就可以了,再次测试结果如下:


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。