mybatis-复杂映射-20200428

XML文件配置模式开发

  1. resultType标签指定的类型,是让mybatis底层自动封装查询返回列名称和实体属性对应关系的,这时候要求列名称要和实体属性名称一致。比如下面的配置,user实体和user表结构完全一致时即可
	<select id = "findAll" resultType="user">
        select * from user
	</select>
  1. resultMap标签是手动配置实体属性与表字段对应关系的,因为有时候用于储存查询结果的实体中的属性,并非能与表查询的返回列名称是一一对应上,此时就可以手工配置。当存储查询结果的实体中,有实体属性时,可以用association标签来配置;有集合时,可以用collection标签来配置。
    association的使用例子(Orders实体中包含了User实体属性):
    <resultMap id="orderMap" type="com.realwangyu.pojo.Orders">
        <result property="id" column="id"></result>
        <result property="orderTime" column="orderTime"></result>
        <result property="total" column="total"></result>

        <association property="user" javaType="com.realwangyu.pojo.User">
            <result property="id" column="uid"></result>
            <result property="username" column="username"></result>
        </association>
    </resultMap>
    
    <select id="findOrderAndUser" resultMap="orderMap">
        select * from orders o, user u where o.uid = u.id
    </select>

collection的使用例子(User实体中包含了Orders类型的List)

    <resultMap id="userMap" type="com.realwangyu.pojo.User">
        <result property="id" column="id"></result>
        <result property="username" column="username"></result>
        <collection property="ordersList" ofType="com.realwangyu.pojo.Orders">
            <result property="id" column="oid"></result>
            <result property="orderTime" column="orderTime"></result>
            <result property="total" column="total"></result>
        </collection>
    </resultMap>


    <select id="findAll" resultMap="userMap">
        select u.id, u.username, o.id oid, o.orderTime, o.total from user u left join orders o on u.id=o.uid;
    </select>
  1. 可以采用导入包的形式导入mapper配置文件,这个时候要求配置文件和所对应的接口同包同名,才能使用
    如下:在这里插入图片描述

注解模式开发

  1. 单标查询CRUD操作比较简单
	@Insert("insert into user values(#{id}, #{username})")
    public void addUser(User user);

    @Update("update user set username = #{username} where id = #{id}")
    public void updateUser(User user);

    @Select("select * from user")
    public List<User> selectUser();

    @Delete("delete from user where id = #{dad}")
    public void deleteUser(Integer id);
  1. 一对一的关联查询,使用到注解@Results,类似于XML配置模式中的resultMap标签;注解@Result,类似result标签;注解@One类似association标签的功能,@Many类似于collection的功能。
  2. 使用到@Results注解时,首先要有一个结果集来给它处理,比如以下代码:
	@Select("select * from orders")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderTime", column = "orderTime"),
            @Result(property = "total", column = "total"),
            @Result(property = "user", column = "uid", javaType = User.class, one=@One(select = "com.realwangyu.dao.IUserMapper.findUserByIdAndName"))
    })
    public List<Orders> findOrderAndUser();

@Results注解要使用到@Select注解的结果,将其中的列与返回类型中的属性一一对应上,如果返回类型中还包含实体属性,那就要通过JavaType标识出属性的类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值