MyBatis自定义映射规则篇(连接查询,需要修改对应映射时使用)

版权声明:转载请说明去处,文章仅供学习参考 https://blog.csdn.net/qq_38487155/article/details/82857820

自定义映射规则:即自己定义数据库与Bean对象的映射规则,不再使用默认Bean对象与记录同名规则,即每个数据库

                            的属性都可以决定它映射到哪个类的哪个属性, 所以操作标签(如<select>)的resultType属性不再使

                            用,改用resultMap属性,二者不能同时使用。

一、改变Bean对象属性名与数据库属性名不一致需要改变映射规则

<resultMap>:在<mapper>标签下的自定义映射规则
              type:指定要修改的映射Bean对象
              id  : 映射规则的别名
              
              <id>:指定主键的映射规则
                            column:被修改的列名(数据库的列名)
                            property:修改后的列名(Bean对象的列名)
                    
              <result>: 指定普通列的映射规则,也可以映射主键。
                            column:被修改的列名(数据库的列名)
                            property:修改后的列名(Bean对象的列名)
                    
       注意:未指定的列自动根据Bean对象映射规则进行映射

模板:

<mapper namespace="接口全类名">
    <!--映射规则的制订-->
    <resultMap type="Bean对象全类名" id="规则名">
        <id column="数据库下的主键名" property="Bean对象的属性名"/>
        <result column="数据库下的列名" property="Bean对象的属性名"/>
    </resultMap>
    <select id="接口方法" resultMap="规则名">
         .....
    </select>
</mapper>

二、连接查询经常使用到的情况:在数据库中我们使用主键,外键参照约束来关联几张表。(即使用在B表中添加一个aid属性来

关联A表),可在Bean对象中常常是一个B对象包含了一个A对象,这时候我们使用连接查询返回的的映射属性明显就不只有aid

属性,还有aname等等,可我们B对象中只包含了一个A属性类,这时候我们就要修改返回属性的映射规则,将其映射至B包含的

A对象中。

1、当Bean对象包含了另一个Bean对象时, 可以使用在类属性名.属性名来指定映射的属性,如:Employee表有一个dept属性类

 dept.id来指定数据库映射到dept的id属性。例:

    <resultMap type="mybatis.bean.Employee" id="myEmp">
        <!-Employee表中现有一个类属性dept->
        <result column="d_id" property="dept.id"/>
        <result column="dept_name" property="dept.dname"/>
    </resultMap>

2、我们还可以使用<association>标签

                                property:指定要映射类属性名

                                javaType:指定全类名

    <resultMap type="mybatis.bean.Employee" id="myemp">
        <association property="dept" javaType="mybatis.bean.Department">
	        <result column="id" property="id"/>
	        <result column="dept_name" property="dname"/>
        </association>
    </resultMap>

3、分步查询:先执行sql配置文件的一句sql语句,再执行另一sql配置文件的sql语句,类似sql里的嵌套查询。

                       可以达到节省资源的效果,在需要其属性时再进行查询,所以多使用分步查询。

                                property:指定要映射类属性名

                                select:接口全类名.方法名,要调用哪个sql配置文件中的sql语句             

                                column:要传入返回的哪一列值传给调用sql语句作参数

    <resultMap type="mybatis.bean.Employee" id="ruleOne">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <association property="类属性名" select="接口全类名.方法名(即其它sql配置文件的语句)" 
                     column="列名(即传入查到的哪一列作为其它sql语句需要传入的参数值)">
        </association>
    </resultMap>

    还需要在全局配置文件中<configuration>下添加如下配置实现延迟加载:

	<settings>
	    <setting name="lazyLoadingEnabled" value="true"/>
	    <setting name="aggressiveLazyLoading" value="false"/>
	</settings>

三、在查询对象时候包含了一个List列表

使用<collection>标签

                                property:指定要映射List属性名

                                ofType   :指定List里的类的全类名

    <resultMap type="Bean对象全类名" id="规则名">
        <result column="id" property="id"/>
        <result column="dept_name" property="dname"/>
        
        <collection property="List属性名" ofType="List里的类的全类名">
            <id column="数据库主键名" property="List里的类的属性名"/>
            <result column="数据库列名" property="List里的类的属性名"/>
        </collection>
    </resultMap>

使用分步查询:

    <resultMap type="Bean对象全类名" id="规则名">
        <result column="id" property="id"/>
        <result column="dept_name" property="dname"/>
        <!-- List部分分步查询映射 -->
        <collection property="List属性名"         
            select="接口方法名" column="要传入下个查询语句的列名">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="gender" property="gender"/>
            <result column="email" property="email"/>
        </collection>
    </resultMap>

  当传入的参数值有多个时,column应使用如下格式:

        column="{占位符名=列名,占位符名=列名,....}"

sql映射文件的占位符名应与column的占位符名一致:

         select * from tb1_employee where d_id = #{占位符名}

猜你喜欢

转载自blog.csdn.net/qq_38487155/article/details/82857820