美文网首页
mybatis中多表联合查询与DTO的字段对应问题

mybatis中多表联合查询与DTO的字段对应问题

作者: 永不熄灭的火焰_e306 | 来源:发表于2021-11-29 16:09 被阅读0次

你把返回的字段映射到一个javabean里面,或者直接返回一个map,都可以。
引用文章地址:https://www.cnblogs.com/onlymate/p/4747227.html
https://blog.csdn.net/u012726702/article/details/72593547
单字段对应(没有对象外键和list)https://blog.csdn.net/vili_sky/article/details/73289962
附加:mybaits封装数据为map的方法:https://blog.csdn.net/m0_49194578/article/details/119378104?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link
···
<span style="font-family:Comic Sans MS;font-size:12px;"><?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="com.sica.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.sica.domain.User">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
</resultMap>

<resultMap id="queryForListMap" type="com.sica.domain.User">
    <id column="id" property="id" jdbcType="VARCHAR"/>
    <result column="username" property="username" jdbcType="VARCHAR"/>
    <result column="password" property="password" jdbcType="VARCHAR"/>
    <collection property="roles" javaType="java.util.List" ofType="com.sica.domain.Role">
        <id column="r_id" property="id" jdbcType="VARCHAR" />
        <result column="r_name" property="name" jdbcType="VARCHAR" />
        <result column="r_jsms" property="jsms" jdbcType="VARCHAR" />
        <result column="r_bz" property="bz" jdbcType="VARCHAR" />
        <result column="r_jlzt" property="jlzt" jdbcType="INTEGER" />
        <result column="r_glbm" property="glbm" jdbcType="VARCHAR" />
    </collection>
</resultMap>
<select id="queryForList" resultMap="queryForListMap">
    SELECT
      u.id,
      u.username,
      u.password,
      r.id r_id,
      r.name r_name,
      r.jsms r_jsms,
      r.bz r_bz,
      r.jlzt r_jlzt,
      r.glbm r_glbm
    FROM
      user u
    LEFT JOIN
      role r
    ON
      u.id = r.userid
</select>
<sql id="Base_Column_List">
  id, username, password
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
    select
    <include refid="Base_Column_List"/>
    from user
    where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.sica.domain.User">
insert into user (id, username, password
  )
values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
  )
</insert>
<insert id="insertSelective" parameterType="com.sica.domain.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
            id,
        </if>
        <if test="username != null">
            username,
        </if>
        <if test="password != null">
            password,
        </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">
            #{id,jdbcType=VARCHAR},
        </if>
        <if test="username != null">
            #{username,jdbcType=VARCHAR},
        </if>
        <if test="password != null">
            #{password,jdbcType=VARCHAR},
        </if>
    </trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.sica.domain.User">
    update user
    <set>
        <if test="username != null">
            username = #{username,jdbcType=VARCHAR},
        </if>
        <if test="password != null">
            password = #{password,jdbcType=VARCHAR},
        </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.sica.domain.User">
update user
set username = #{username,jdbcType=VARCHAR},
  password = #{password,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}

</update>
</mapper></span>
···

相关文章

网友评论

      本文标题:mybatis中多表联合查询与DTO的字段对应问题

      本文链接:https://www.haomeiwen.com/subject/rydlxrtx.html