MyBatis实现级联查询及逆向生成
一,级联查询
1、级联查询 N-1
以多的一方为主表
接口
//级联查询 N-1
List<Emp> selectEmp(Map map);
映射文件
<!--级联查询 N-1 -->
<resultMap id="emp_Dept" type="emp">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<result column="salary" property="salary"/>
<result column="bonus" property="bonus"/>
<result column="birth" property="birth"/>
<result column="hiredate" property="hiredate"/>
<result column="leader" property="leader"/>
<result column="deptid" property="deptid"/>
<association property="dept" javaType="dept">
<id column="id" property="id"/>
<result column="dname" property="dname"/>
</association>
</resultMap>
<select id="selectEmp" resultMap="emp_Dept" parameterType="map">
select * from emp e left join dept d on e.deptid=d.id
<where>
<if test="name!=null">
name like "%"#{name}"%"
</if>
</where>
limit #{start},#{size}
</select>
一的一方的数据封装使用association标签
使用JavaType固定搭配
对应javabean中的属性
测试类
SqlSession session=null;
@Before
public void init() throws IOException {
InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
session=factory.openSession();
}
//级联查询N-1
@Test
public void selectEmp() {
HashMap map = new HashMap();
map.put("name","关");
ma