Mybatis输入和输出映射

输入映射和输出映射

Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心。

parameterType(输入类型)

1.传递基本数据类型:int,string,long,Date;

{参数} 获取参数中的值
<!-- 根据id获取用户信息 -->
<select id="findUserById" parameterType="int" resultType="cn.thw.mybatis.po.User">
    select * from user where id = #{id}
</select>

2.复杂数据类型:类和Map
Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称,map中则是#{key}。

  • map
<select id="queryCarList" resultMap="BaseResultMap" parameterType="java.util.Map">
        select id,car_dept_name from car where 1=1
        <if test="id != null">
            and  id = #{id,jdbcType=DECIMAL}
        </if>
        <if test="carDeptName != null">
            and  car_dept_name = #{carDeptName,jdbcType=VARCHAR}
        </if>
 </select>
<select id="queryCarList" resultMap="BaseResultMap" parameterType="com.model.Car">
        select id,car_dept_name from car where 1=1
        <if test="id != null">
            and  id = #{id,jdbcType=DECIMAL}
        </if>
        <if test="carDeptName != null">
            and  cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR}
        </if>
 </select>
  • 传递pojo包装对象
public class QueryVo {

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
<!-- 使用包装类型查询用户,使用ognl从对象中取属性值,如果是包装对象可以使用.(点)操作符来取内部的属性-->
    <select id="findUserByQueryVo" parameterType="queryvo" resultType="user">
        SELECT * FROM user where username like '%${user.username}%'
    </select>


resultType(输出类型)

在mapper的statement中,我们通过resultType指定输出参数的类型,类型可以是简单类型和pojo的包装类型。使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象,不一致的属性的值为null。

1.输出简单类型

  • Mapper.xml文件
<!-- 获取用户列表总数 -->
    <select id="findUserCount" parameterType="user" resultType="int">
       select count(1) from user
    </select>
  • Mapper接口
public int findUserCount(User user) throws Exception;

2.pojo对象和pojo对象列表

不管是输出的pojo单个对象还是一个list列表,在mapper.xml中resultType指定的类型是一样的。

<select id="findUserList" parameterType="cn.thw.mybatis.po.UserQueryVo" 
        resultType="cn.thw.mybatis.po.UserCustom">
SELECT * FROM USER
</select>

<select id="findUserByName" parameterType="java.lang.String" resultType="cn.thw.mybatis.po.User">
    SELECT * FROM USER WHERE username LIKE '%${value}%'
</select>

3.resultMap

resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。

如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。

resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

  • Mapper.xml定义
<!-- 查询用户列表 根据用户名称和用户性别查询用户列表 -->
<select id="findUserListResultMap" parameterType="queryVo" resultMap="userListResultMap">
    select id id_,username username_,birthday birthday_ from user
    <!-- where自动将第一个and去掉 -->
    <where>
        <!-- refid:指定sql片段的id,如果引用其他命名空间的sql片段,需要前面加namespace -->
        <include refid="query_user_where">
    </where> 
</select>
  • 定义resultMap

    由于上边的mapper.xml中sql查询列和Users.java类属性不一致,需要定义resultMap:userListResultMap将sql查询列和Users.java类属性对应起来

<!-- 定义resultMap,将用户查询字段和user这个pojo的属性一个对应关系 -->
<!-- 
    type:最终映射的java对象 
    id:resultMap的唯一标识
-->
<resultMap type="user" id="userListResultMap">
<!-- 
    id标签:查询结果集的唯一标识 列(主键或唯一标识)
    column:sql查询字段别名(列名)
    property:pojo的属性名

    result标签:普通列
-->
    <id column="id_" property="id"/>
    <result column="username_" property="username"/>
    <result column="birthday_" property="birthday"/>
</resultMap>
  • Mapper接口定义
public List<User> findUserListResultMap() throws Exception
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值