MyBatis学习5之多表查询 OneToMany

接着上一篇博客OnToOne继续探讨mybatis多表查询

OneToMany,在t_group表中一个组对应多个人员信息,根据一个组去查询该组下的所有人员信息

1、修改sql映射文件GroupPersonUserMapper.xml

         <select id="findGroupAndPersonByGID" resultMap="GroupAndPerson">
	 	select g.id,g.group_name,g.description, 	
		p.gid,p.id pid,p.name,p.telphone,p.address,p.email
		from t_group g left join t_person  p on g.id = p.gid
		where g.id = #{id}
	 </select>
	 <resultMap type="com.zlt.mybatis.model.Group" id="group">
		 <id property="id" column="id"/>
	 	 <result property="groupName" column="group_name"/>
	 	 <result property="description" column="description" />
	 </resultMap>
	 <resultMap type="com.zlt.mybatis.model.Group" id="GroupAndPerson" extends="group">
	 	<collection property="persons" column="gid" ofType="com.zlt.mybatis.model.Person">
	 		<id property="id" column="pid"/>
	 		<result property="name" column="name"/>
	 		<result property="telphone" column="telphone"/>
	 		<result property="address" column="address"/>
	 		<result property="email" column="email"/>
	 		<result property="gid" column="gid"/>
		</collection>
	 </resultMap>

 2、单元测试用例

public void testOneToMany(){
		SqlSession session = null;
		try{
				session = sqlSessionFactory.openSession();
				String statement = "com.zlt.mybatis.model.Group.findGroupAndPersonByGID";
				//List<Group> groups = session.selectList(statement, 1);
				Group group = session.selectOne(statement, 1);
				if(null != group) {
						StringBuffer sb = new StringBuffer();
						sb.append("id:" + group.getId() + "\t")
						  .append("groupName:" + group.getGroupName() +"\t")
						  .append("description:" + group.getDescription() +"\t");
						String len = sb.toString(); 
						List<Person> persons = group.getPersons();
						for (Person person : persons) {
							sb.append("id:" + person.getId() + "\t")
							.append("name:" + person.getName() + "\t")
							.append("telphone:" + person.getTelphone() + "\t")
							.append("address:" +person.getAddress() + "\t")
							.append("email:" + person.getEmail() + "\t")
							.append("gid:" + person.getGid() + "\t");
							System.out.println(sb.toString());
							sb.delete(len.length(), sb.length());
						}
				}
			}finally{
			if(null != session) session.close();
		}
	}

 3、测试结果

id:1	groupName:Java开发部	description:Java项目组开发	id:1	name:张三	telphone:10086	address:浦东新区广兰路	email:[email protected]	gid:1	
id:1	groupName:Java开发部	description:Java项目组开发	id:2	name:李四	telphone:10000	address:浦东新区广兰路	email:[email protected]	gid:1	

 ManyToOne

1、修改SQL映射文件

<select id="findGroupAndPersonByGID2" resultMap="GroupAndPerson2">
	 	select p.id id,p.name,p.telphone,p.address,p.email,p.gid,
	 	g.id gid,g.group_name,g.description	
		from t_person p left join t_group  g on p.gid = g.id
		where p.id = #{id}
	 </select>
	 <resultMap type="com.zlt.mybatis.model.Person" id="GroupAndPerson2">
 		<id property="id" column="id"/>
 		<result property="name" column="name"/>
 		<result property="telphone" column="telphone"/>
 		<result property="address" column="address"/>
 		<result property="email" column="email"/>
 		<result property="gid" column="gid"/>
	 	<association property="group" javaType="com.zlt.mybatis.model.Group">
	 		<id property="id" column="gid"/>
	 		<result property="groupName" column="group_name"/>
	 		<result property="description" column="description" />
		</association>
	 </resultMap> 

 2、单元测试

public void testManyToOne(){
		SqlSession session = null;
		try{
				session = sqlSessionFactory.openSession();
				String statement = "com.zlt.mybatis.model.Group.findGroupAndPersonByGID2";
				Person person = session.selectOne(statement, 2);
				if(null != person) {
						StringBuffer sb = new StringBuffer();
							sb.append("id:" + person.getId() + "\t")
							.append("name:" + person.getName() + "\t")
							.append("telphone:" + person.getTelphone() + "\t")
							.append("address:" +person.getAddress() + "\t")
							.append("email:" + person.getEmail() + "\t")
							.append("gid:" + person.getGid() + "\t");
							Group group = person.getGroup();
							sb.append("group-id:" + group.getId() + "\t")
							  .append("groupName:" + group.getGroupName() +"\t")
							  .append("description:" + group.getDescription() +"\t");
						
							System.out.println(sb.toString());
				}
			}finally{
			if(null != session) session.close();
		}
	}

 3、测试结果

id:2	name:李四	telphone:10000	address:浦东新区广兰路	email:[email protected]	gid:1	group-id:1	groupName:Java开发部	description:Java项目组开发	

猜你喜欢

转载自lintaozhou.iteye.com/blog/2397988