连表查询解决字段冲突
字段冲突在 id
, age
class 表 (加表名在前面,让sql查询通过 )
<sql id="Base_Column_List">
id,class_name,age
</sql>
<!-- 改为-->
<sql id="join_Base_Column_List">
my_class.id,
my_class.class_name,
my_class.age
</sql>
stu表 ( 加别名)
<sql id="Base_Column_List">
<[email protected]>
id, stu_name, class_id, age, address, province, `value`
</sql>
<!-- 改为-->
<sql id="bb">
`stu`.id as stu_stu_id,
`stu`.stu_name as stu_stu_name,
`stu`.class_id as stu_class_id,
`stu`.age as stu_age
</sql>
<!--结果对象映射 加前缀 columnPrefix="stu_"-->
<resultMap extends="BaseResultMap" id="MyClassResultMap" type="com.example.demomybatisplus.module.MyClass">
<collection columnPrefix="stu_" ofType="com.example.demomybatisplus.module.Stu" property="stus" resultMap="com.example.demomybatisplus.mapper.StuMapper.BaseResultMap">
</collection>
</resultMap>
StuMapper.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.example.demomybatisplus.mapper.StuMapper">
<resultMap id="BaseResultMap" type="com.example.demomybatisplus.module.Stu">
<[email protected]>
<!--@Table yogurt.stu-->
<id column="id" jdbcType="VARCHAR" property="stuId" />
<result column="stu_name" jdbcType="VARCHAR" property="stuName" />
<result column="class_id" jdbcType="VARCHAR" property="classId" />
<result column="age" jdbcType="VARCHAR" property="age" />
</resultMap>
<sql id="Base_Column_List">
<[email protected]>
id, stu_name, class_id, age, address, province, `value`
</sql>
<!---->
<sql id="bb">
`stu`.id as stu_stu_id,
`stu`.stu_name as stu_stu_name,
`stu`.class_id as stu_class_id,
`stu`.age as stu_age
</sql>
</mapper>
MyClassMapper.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.example.demomybatisplus.mapper.MyClassMapper">
<resultMap id="BaseResultMap" type="com.example.demomybatisplus.module.MyClass">
<[email protected]>
<!--@Table yogurt.my_class-->
<id column="id" property="classId" />
<result column="class_name" property="className" />
<result column="age" property="age" />
</resultMap>
<sql id="Base_Column_List">
id,class_name,age
</sql>
<sql id="join_Base_Column_List">
my_class.id,
my_class.class_name,
my_class.age
</sql>
<!--结果对象映射 加前缀 columnPrefix="stu_"-->
<resultMap extends="BaseResultMap" id="MyClassResultMap" type="com.example.demomybatisplus.module.MyClass">
<collection columnPrefix="stu_" ofType="com.example.demomybatisplus.module.Stu" property="stus" resultMap="com.example.demomybatisplus.mapper.StuMapper.BaseResultMap">
</collection>
</resultMap>
<!--<select id="findByAll" resultMap="findAll">-->
<!-- select-->
<!-- <include refid="Base_Column_List" /> ,-->
<!-- <include refid="com.example.demomybatisplus.mapper.StuMapper.bb" />-->
<!-- from my_class left join stu on stu.class_id = my_class.class_id-->
<select id="findById" resultMap="MyClassResultMap">
select
<include refid="join_Base_Column_List" /> ,
<include refid="com.example.demomybatisplus.mapper.StuMapper.bb" />
from my_class left join stu on stu.class_id = my_class.id
</select>
</mapper>
标签:name,age,id,连表,stu,冲突,查询,my,class
From: https://www.cnblogs.com/firsthelloworld/p/17901077.html