MyBatis中的分步查询

 一.新建两个表

User  Dept表

  二.新建JavaBean对象用于方便对数据操作

public class User {
	private int id;
	private String name;
	private Dept dept;
	

 dept一样

三.对每个对象创建Mapper,并写 操作方法,这里是查询

public interface UserMapper {
 
	  public User getUserBYid(int i) ;
	
    }
public interface DeptMapper {
 
	  public Dept getDeptBYid(Integer id) ;
	
    }

 四.分辨编写DeptMapper.xml,UserMapper.xml

    
    <select id="getDeptBYid" resultType="com.cws.mybatis.bean.Dept">
        select * from dept where id=#{id}
    </select>

  • 将所要查询的用户查处
  • d_id取出放入getDeptBYid方法中
  • 取出Dept
<select id="getUserBYid" resultMap="step">
		select * from user where id = #{id}
	</select>



	<resultMap type="user" id="step">
	<id column="id" property="id"/>
	<result column="name" property="name"/>
	<association property="dept" select="com.cws.mybatis.dao.DeptMapper.getDeptBYid"
	column="d_id"></association>
	</resultMap>


    

猜你喜欢

转载自blog.csdn.net/qq_38474916/article/details/81288126
今日推荐