今天因业务需要一对一进行数据库查询,在mybatis中进行了配置。
public class RiskRuleBiztype { private Integer id; private String name; private Integer ruleCode; private Integer type;
}
public class RiskRuleBiztypeProduct { private Integer pId; private Integer productType; private String productName; private Integer processNode; private RiskRuleBiztype riskRuleBiztype;
private Integer RiskRuleBiztypeId; (上表的外键)
}
<resultMap id="BaseResultMap2" type="com.zbjk.finance.xff.lrm.domain.RiskRuleBiztypeProduct" > <id column="p_id" property="pId" jdbcType="INTEGER" /> <result column="product_type" property="productType" jdbcType="TINYINT" /> <association property="riskRuleBiztype" column="biztype_id" javaType="com.zbjk.finance.xff.lrm.domain.RiskRuleBiztype"> <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="rule_code" property="ruleCode" jdbcType="INTEGER" /> <result column="type" property="type" jdbcType="TINYINT" /> </association> </resultMap>
<select id="query" resultMap="BaseResultMap2"> select * from risk_rule_biztype_product rp, risk_rule_biztype r where rp.biztype_id = r.id </select>
通过 association 进行一对一的关联查询。
association中的 property 代表的是 外键表的实体类。
column是数据库中的外键字段
javaType是外键对应的实体类
需要注意的一点是
<id column="p_id" property="pId" jdbcType="INTEGER" />
<id column="id" property="id" jdbcType="INTEGER" />这两个 column(数据库中的字段名 id)并不能一样,否则会以第一个查询结果为主。