Detailed configuration of resultMap of mybatis

1. Entity class inheritance of mybatis

Reference materials:
1. The entity class in mybatis, the po class inherits another po class.
2. The resultMap configuration rules in mybatis

The role of entity class inheritance is to reduce code reuse in entity classes through inheritance, such as attributes in entity classes corresponding to fields that often appear in database tables.

2. The attribute extends in the resultMap tag of mybatis can inherit the resultMap tag of another namespace.

<mapper namespace="xuecheng.dao.ACVSFundTest">

    <resultMap id="res" extends="xuecheng.dao.BaseEntityTest.baseCol" type="xuecheng.domain.ACVSFund">
        <!--<id column="id" property="id"/>-->
        <result column="fund_code" property="fundCode"/>
        <result column="fund_name" property="fundName"/>
        <result column="gmt_Create" property="gmtCreate"/>

    </resultMap>

Inherit another

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="xuecheng.dao.BaseEntityTest">
    <resultMap id="baseCol" type="xuecheng.domain.BaseEntity">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="age" property="age"/>
    </resultMap>

One, the attribute label of mybatis

<resultMap>
        <constructor>
            <idArg/>
            <arg/>
        </constructor>
        <id/>
        <result/>
        <association property=""/>
        <collection property=""/>
        <discriminator javaType="">
            <case value=""></case>
        </discriminator>
</resultMap>

1.1, label constructor

By default, mybatis will use the no-parameter construction in the entity class. When a parameterized structure is provided, no parameterless structure is not provided in the entity class. At this time, special configuration is required in resultMap

Suppose the configuration in the entity class is as follows:

public User(Long id, String username, String password, String address) {
    
    
        this.id = id;
        this.username = username;
        this.password = password;
        this.address = address;
    }

The configuration of the mapping file is as follows:

<resultMap id="userResultMap" type="org.sang.bean.User">
        <constructor>
            <idArg column="id" javaType="long"/>
            <arg column="username" javaType="string"/>
            <arg column="password" javaType="string"/>
            <arg column="address" javaType="string"/>
        </constructor>
    </resultMap>

1.2, label association

Applicable scenarios are one-to-one

Two tables: 1, alias
Insert picture description here

2、province
Insert picture description here

The provinceMapper.xml file is as follows

<mapper namespace="xuecheng.dao.ProvinceMapper">
    <resultMap id="provinceResultMapper" type="xuecheng.domain.Province">
        <id column="id" property="id"/>
        <!--association 中的属性property 是一对一中的属性名称,
                        column是指传入参数的id
                        select 是指执行的方法-->
        <association property="alias" column="id" select="xuecheng.dao.AliasMapper.findAliasByPid"/>
    </resultMap>
    <select id="getProvince" resultMap="provinceResultMapper">
      SELECT * FROM province
    </select>

</mapper>

The xml file of alias is as follows:

<mapper namespace="xuecheng.dao.AliasMapper">
   <select id="findAliasByPid" parameterType="long" resultType="xuecheng.domain.Alias">
       SELECT * FROM alias WHERE pid=#{id}
   </select>
</mapper>

1.3, label collection

 <resultMap id="provinceResultMapper" type="xuecheng.domain.Province">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <!--association 中的属性property 是一对一中的属性名称,
                        column是指传入参数的id
                        select 是指执行的方法-->
        <!--<association property="alias" column="id" select="xuecheng.dao.AliasMapper.findAliasByPid"/>-->

        <!--<collection property="cities" column="id" select="xuecheng.dao.CityMapper.findById"/>-->
</resultMap>

1.4, label discriminator (somewhat similar to switch statement)

    <resultMap id="provinceResultMapper" type="xuecheng.domain.Province">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <!--association 中的属性property 是一对一中的属性名称,
                        column是指传入参数的id
                        select 是指执行的方法-->
        <!--<association property="alias" column="id" select="xuecheng.dao.AliasMapper.findAliasByPid"/>-->

        <!--collection的测试-->
        <!--<collection property="cities" column="id" select="xuecheng.dao.CityMapper.findById"/>-->


        <!--以下有点类似于switch语句-->
        <discriminator javaType="int" column="area">
            <case value="1" resultMap="noodleResultMap"></case>
            <case value="2" resultMap="riceResultMap"></case>
        </discriminator>

        <!--以下collection,不能查出准确数据。原因不知因为是啥?-->
        <!--<collection property="cities" ofType="xuecheng.domain.City">-->
            <!--<id column="id" property="id"/>-->
            <!--<result column="name" property="name"/>-->
            <!--<result column="pid" property="pid"/>-->

        <!--</collection>-->

    </resultMap>

1.5. Points to note: lazy loading of mybatis

1.5.1. Two ways to open, way one (open globally, that is, for all queries):

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

Note: lazyLoadingEnabled is false by default, that is, it is not turned on. aggressiveLazyLoading is true by default, that is, the query is performed according to the level. Noodle and rice are at the same level, that is, when querying noodle, rice is also queried. If it is false, the query is performed on demand. When you need to query noodle, you will not query rice.

1.5.2, Method Two (Operate Partially)

In the collection tag and association tag, use the fetchType attribute, eager represents immediate loading, and lazy represents delayed loading.

<association property="alias" column="id" select="org.sang.db.AliasMapper.findAliasByPid" fetchType="eager"/>
<collection property="cities" column="id" select="org.sang.db.CityMapper.findCityByPid" fetchType="lazy"/>

1.6, sql label

You can encapsulate only part of the query statement as follows:

<sql id="selectAll3">
        id,username,address,password
</sql>

The reference method is as follows:

<select id="getUser3" resultType="user">
        SELECT
        <include refid="selectAll3"/> FROM user
 </select>

You can also use some variables when encapsulating, as follows:

<sql id="selectAll4">
        ${prefix}.id,${prefix}.username,${prefix}.address
</sql>

Note that the variable reference method is the $ sign, not #. The reference method is as follows:

<select id="getUser4" resultType="user" parameterType="string">
        SELECT
        <include refid="selectAll4">
            <property name="prefix" value="u"/>
        </include> FROM user u
</select>

Guess you like

Origin blog.csdn.net/weixin_43983411/article/details/109846839
Recommended