mybatis多表关联查询 一:在一个对象(User )中建立另一个对象属性(userExtend): public class User implements Serializable{ private static final long serialVersionUID = 1L; /** ID */ private String id; /** 用户名 */ private String username; /** 登录密码 */ private String password; /** 用户扩展信息*/ private UserExtend userExtend; GET/SET 方法省略 } 二:在第一个对象的mapper中的结果集书写: <resultMap id="ResultMap" type="cn.ss.umm.model.po.User"> <id column="id" property="id" jdbcType="CHAR" /> <result column="username" property="username"jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <association property="userExtend" column="id" javaType="cn.ss.umm.model.po.UserExtend"> <id column="ue_id" property="id" jdbcType="CHAR" /> <result column="sex" property="sex" jdbcType="CHAR"/> <result column="realname" property="realname" jdbcType="VARCHAR"/> <result column="qq" property="qq" jdbcType="VARCHAR" /> </association> </resultMap> 三:查询的SQL的书写: <select id="selectUserByPhoneOrEmail" parameterType="java.util.Map" resultMap="ThreeResultMap"> select a.*, b.* from t_user a left join t_user_extend b on a.id = b.id <where> <if test="qq!= null"> b.qq= #{qq} </if> </where> </select>
时间: 2024-12-12 21:51:14