龙空技术网

mybatis 多表映射结果如何进行定义

爱Code 282

前言:

现在大家对“mybatis多表查询映射”大约比较关切,朋友们都想要知道一些“mybatis多表查询映射”的相关内容。那么小编也在网络上汇集了一些对于“mybatis多表查询映射””的相关文章,希望各位老铁们能喜欢,看官们快快来了解一下吧!

近期小编在公司做 spring boot +mybatis的项目。那问大家一件事情,那就是对于数据库结构,在开发过程之中会设计的一点失误都没有吗?

小编则认识不会有这么个情况,那怕是再小的项目都有一定风险数据库字段类型的变动。

那咱们就设计另外一个问题,如果我字段变动了,对于多表查询出来的结果,都要重新把结果处理一次吗?

这个肯定是不可以的。虽然我身边等多个朋友说不会要求全部返回的,但如果我们做的是标准接口,两个接口返回过来的值不一样,那就是相当尴尬的一件事情。

那我们如何进行处理呢,小编这里用的就比较容易

 <select id="sysuserQuery" parameterType="com.lxmes.automapper.entity.SysUserExample"  resultMap="sysUserAllEntity">        select        <include refid="Columns" >            <property name="table_name" value="SYS_USER"/>            <property name="asName" value=""/>        </include>        ,        <include refid="Columns" >            <property name="table_name" value="Create_Sys_User"/>            <property name="asName" value="Create_Sys_User_"/>        </include>        ,        <include refid="Columns" >            <property name="table_name" value="Modify_Sys_User"/>            <property name="asName" value="Modify_Sys_User_"/>        </include>        FROM SYS_USER        LEFT JOIN SYS_USER Create_Sys_User ON Create_Sys_User.ID=SYS_USER.CREATE_SYS_USER_ID        LEFT JOIN SYS_USER Modify_Sys_User ON Modify_Sys_User.ID=SYS_USER.MODIFY_SYS_USER_ID        <if test="_parameter != null">            <include refid="Example_Where_Clause" >                <property name="table_name" value="SYS_USER."/>            </include>        </if>        <if test="orderByClause != null">            order by ${orderByClause}        </if>    </select>
<sql id="Columns">    ${table_name}.ID as  ${asName}ID,    ${table_name}.Create_Sys_User_ID as  ${asName}Create_Sys_User_ID,    ${table_name}.Create_DateTime as  ${asName}Create_DateTime,    ${table_name}.Modify_Sys_User_ID as  ${asName}Modify_Sys_User_ID,    ${table_name}.Modify_DateTime as  ${asName}Modify_DateTime,    ${table_name}.Organization as  ${asName}Organization,    ${table_name}.User_Code as  ${asName}User_Code,    ${table_name}.User_Name as  ${asName}User_Name,    ${table_name}.User_Password as  ${asName}User_Password,    ${table_name}.User_State as  ${asName}User_State,    ${table_name}.User_Category as  ${asName}User_Category,    ${table_name}.Position as  ${asName}Position,    ${table_name}.Contact_Number as  ${asName}Contact_Number,    ${table_name}.ID_Number as  ${asName}ID_Number</sql>
<resultMap id="sysUserAllEntity" type="com.lxmes.sys.entity.SysUserAllEntity"  extends="com.lxmes.automapper.mapper.SysUserMapper.BaseResultMap">    <association property="createSysuser" javaType="com.lxmes.automapper.entity.SysUser"  resultMap="com.lxmes.automapper.mapper.SysUserMapper.BaseResultMap" columnPrefix="Create_Sys_User_"/>    <association property="modifySysUser" javaType="com.lxmes.automapper.entity.SysUser"  resultMap="com.lxmes.automapper.mapper.SysUserMapper.BaseResultMap" columnPrefix="Modify_Sys_User_"/></resultMap>

代码写了很多,但有用的就几个点。

思路是在查询SQL时,起别名,然后用 association 中的 columnPrefix 对别名进行识别。

基本上就达到目标了。

部分知识换机也使用了 java mybatis 一对一 如何更简洁的内容

标签: #mybatis多表查询映射