Mybatis 之级联查询 一对多配置
Mybatis级联 查询相对于hibenate是有点麻烦,但是相应好处也是有的,Mybatis轻量、根据自己要的字段配置方便
一对多配置用 <collection property="bean里面的对象集合" column="一对多的外键" select="一对多 中的‘多’ sql语句id"></collection>
多对一配置 <association property="user" javaType="User"> 配置需要的字段</association >
一对多配置
bean实体类
/** * 用户信息表 bean * @author flm * @date 2017年7月27日 */ public class UserBean implements Serializable{ private static final long serialVersionUID = 4581502828662200769L; private Integer id; //用户ID private String loginName; //登录名称 private Integer sex; //性别 private String phone; //手机号码 private String address; //住址 private Integer u_identity;//身份 1 厂家 2 经销商 3用户 private Integer u_last_id; //上一级用户id private String loginPwd; // 登录密码 public List<UserBean> userBeans =new ArrayList<UserBean>(); // userBeans List集合 应用于 一对多查询 // (本类比较特殊 一对多也是自己userBean) }
mapxml文件
<mapper namespace="com.ifengSearch.user.dao.UserDao"> <!-- 结构Map配置 --> <resultMap id="UserMap" type="com.ifengSearch.user.entity.UserBean" > <id column="id" property="id" javaType="Integer" jdbcType="INTEGER" /> <result column="loginName" property="loginName" javaType="String" jdbcType="VARCHAR" /> <result column="sex" property="sex" javaType="Integer" jdbcType="INTEGER" /> <result column="phone" property="phone" javaType="String" jdbcType="VARCHAR" /> <result column="address" property="address" javaType="String" jdbcType="VARCHAR" /> <result column="u_identity" property="u_identity" javaType="Integer" jdbcType="INTEGER" /> <result column="u_last_id" property="u_last_id" javaType="Integer" jdbcType="INTEGER" /> <result column="loginPwd" property="loginPwd" javaType="String" jdbcType="VARCHAR" /> </resultMap> <!-- === userBean 一 对多查询关联 (本类比较特殊 一对多也是自己userBean) === --> <resultMap id="getlist" type="com.ifengSearch.user.entity.UserBean" > <!-- 实体类属性对应数据库的主键字段,不然主键会查不到 主键 id --> <id property="id" column="id" javaType="Integer" jdbcType="INTEGER" /> <!-- 用collection标签 ,也是实体类属性要对应数据库字段 --> <!-- userBeans 对应的是 userBean实体类配置的集合 column="id" 是对应主键一对多的关联的外键 --> <!-- select="com.ifengSearch.user.dao.UserDao.getUser" getUser一对多查询的sql语句 id ‘多’查询userBeans查询集合 --> <collection property="userBeans" column="id" select="com.ifengSearch.user.dao.UserDao.getUser"> </collection> </resultMap> <!-- 根据 一对多 中的 ‘多’查询 userBeans查询集合 --> <select id="getUser" resultMap="UserMap" parameterType="Integer"> select * from user_info where u_last_id = #{id}; </select> <!-- getlist 一对多 中的‘一’ 查询 --> <select id="selectAll" resultMap="getlist"> select * from user_info where id=#{id} </select>
dao层调用
package com.ifengSearch.user.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import com.ifengSearch.user.entity.UserBean; /** * 用户 dao层 * @author flm * @date 2017年7月27日 */ @Repository public interface UserDao { /** * 一对多查询 user * @return */ public List<UserBean> selectAll(); }
然后就可以获取到 一对多的集合了
多对一 相对应来说比较简单
直接配置就可以了
<resultMap id="resultUserOhter" type="Uother"> <id column="id" property="id" /> <result column="other" property="other" /> <association property="user" javaType="User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="info" property="info" /> </association> </resultMap> <select id="getUserOhters" parameterType="int" resultMap="resultUserOhter"> select user.id,user.name,user.info,uother.id,uother.ohter from uother,user where user.id=uother.user_id </select>