maybatis一对多/多对一查询

1.mybatis 一对多的三种实现方式(查询1的一方把多的一方查出来):

方式1:

1的一方:

<?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.finance.cmp.ruleEngine.dao.mapper.TRuleMapper">
    <resultMap id="BaseResultMap" type="com.finance.cmp.ruleEngine.dao.model.TRule">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="rule_name" jdbcType="VARCHAR" property="ruleName"/>
        <result column="rule_expressions" jdbcType="VARCHAR" property="ruleExpressions"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        //一对多 conllection标签
        //property:一的类中持有多的集合的引用 
        // ofType:多的一方集合的类型 
        //select :在多的一方的xml中写查询语句 
        // 把要按什么条件查的参数传入多的一方的sql
        <collection property="tRuleFactors" ofType="com.finance.cmp.ruleEngine.dao.model.TRuleFactor" select="com.finance.cmp.ruleEngine.dao.mapper.TRuleFactorMapper.selectFactorByRuleId" column="id">

        </collection>
    </resultMap>


    <select id="selectById" parameterType="map" resultMap="BaseResultMap">
    SELECT
	*
    FROM
      t_rule a
    WHERE
      a.id = #{id}
  </select>

 

</mapper>

多的一方的xml:

<?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.finance.cmp.ruleEngine.dao.mapper.TRuleFactorMapper">
    <resultMap id="BaseResultMap" type="com.finance.cmp.ruleEngine.dao.model.TRuleFactor">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="rule_id" jdbcType="INTEGER" property="ruleId"/>
        <result column="factor_in_expression" jdbcType="VARCHAR" property="factorInExpression"/>
        <result column="factor_var_type" jdbcType="VARCHAR" property="factorVarType"/>
        <result column="factor_var_id" jdbcType="INTEGER" property="factorVarId"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <select id="selectFactorByRuleId" parameterType="int" resultMap="BaseResultMap">
        SELECT * FROM t_rule_factor WHERE rule_id=#{0}
  </select>
</mapper>

方式2:后续再补充

2.多对一查询:后续再补充

猜你喜欢

转载自blog.csdn.net/qq_41799291/article/details/88932481