11.复杂环境搭建
1.一对多处理
场景:
有两张表格宠物表pet和主人表person
宠物和人多对1的关系!
现需要查询出指定用户id的人和他下的所有宠物
1.person类的代码
//重点1:给宠物类起一个别名:wmd->用于更改类的别名
需要注意的是
该标签必须和下述配合使用(在mybatis的主配置文件中配置)
(<typeAliases>
<package name="包名"/>
</typeAliases>)
@Alias("wmd")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Person {
private int id;
private String person_name;
private int age;
private String message;
重点2:因为一个人可以对应多个宠物,所以此处是一个集合
private List<Pets> pets;
}
2. pets类代码:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Alias("pets")
public class Pets {
private int id;
private String name;
private String type;
private int masterId;
}
3.接口类的写法
public interface PersonMapper {
...
Person selectPersonAndPets(@Param("id") int person_id);
}
3.mapper.xml文件中写法
//重点1:因为返回类型是个复杂类型Person,所以此处使用resultMap作为返回值类型,值是自定义的PersonAndPet
<select id="selectPersonAndPets" resultMap="PersonAndPet">
select p.id, p.name, p.age age, p.message message,
t.id pid, t.name pet_name, t.type,t.master_id
from person p
left join pets t on p.id = t.master_id
where p.id=#{id}
</select>
//重点2:自定义的resultMap,id值和上面重点1返回id值对应上了,类型是实体类Person自定义的别名wmd
<resultMap id="PersonAndPet" type="wmd">
<result column="id" property="id"/>
<result column="name" property="person_name"/>
<result column="age" property="age"/>
<result column="message" property="message"/>
//重点3:如果实体类中包含的是集合List,使用的是collection标签,ofType里是泛型的类型
<collection property="pets" ofType="pets">
<result property="id" column="pid"/>
<result property="masterId" column="master_id"/>
<result property="name" column="pet_name"/>
<result property="type" column="type"/>
</collection>
</resultMap>
//第二种方式:如果嫌嵌套的不好看可以使用这种方式
<resultMap id="PersonAndPet" type="wmd">
<result column="id" property="id"/>
<result column="name" property="person_name"/>
<result column="age" property="age"/>
<result column="message" property="message"/>
//重点1:使用这种方式可以取其他标签,使用的标签是resultMap
<collection property="pets" resultMap="wmd_pets"/>
</resultMap>
<resultMap id="wmd_pets" type="pets">
<result property="id" column="pid"/>
<result property="masterId" column="master_id"/>
<result property="name" column="pet_name"/>
<result property="type" column="type"/>
</resultMap>
4.测试输出:可以拿到所有信息
Person(id=1, person_name=吴孟达, age=18, message=吴孟达留言,
pets=[Pets(id=1, name=皮皮, type=dog, masterId=1),
Pets(id=2, name=肉肉, type=dog, masterId=1)])
])
这种方式需要注意的是:
含有collection 标签的resultMap标签,必须要把实体类中的所有字段全部声明出来,要不就会为null
2.多对一
即多个宠物对应一个主人,
如果要查询一个宠物,然后关联出它的主人呢
1.person类的代码
//重点1:给宠物类起一个别名:wmd->用于更改类的别名
需要注意的是
该标签必须和下述配合使用(在mybatis的主配置文件中配置)
(<typeAliases>
<package name="包名"/>
</typeAliases>)
@Alias("wmd")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Person {
private int id;
private String person_name;
private int age;
private String message;
}
2. pets类代码:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Alias("pets")
public class Pets {
private int id;
private String name;
private String type;
private int masterId;
//一个宠物对应一个主人
private Person person;
}
1.接口类的写法:
public interface PetsMapper {
Pets selectPetsAndPerson(@Param("id") int pet_id);
}
2.对应的mapper.xml中的写法
重点1:因返回值是复杂类型,所以返回值类型使用resultMap("自定义的resultMapid")
<select id="selectPetsAndPerson" resultMap="wmd_pets">
select p.id, p.name, p.age age, p.message message,
t.id pid, t.name pet_name, t.type,t.master_id
from person p
right join pets t on p.id = t.master_id
where t.id=#{id}
</select>
重点2:自定义的resultmap
id是自定义resultMap的id值,type是代表的实体类型(可以是别名)
<resultMap id="wmd_pets" type="pets">
<result property="id" column="pid"/>
<result property="type" column="type"/>
<result property="name" column="pet_name"/>
<result property="masterId" column="master_id"/>
重点3:虽然pets实体类中有一个Person引用,但是也可以使用集合的标签collection来获取
<collection property="person" ofType="wmd">
<result property="id" column="id"/>
<result property="person_name" column="name"/>
<result property="age" column="age"/>
<result property="message" column="message"/>
</collection>
</resultMap>
2.另外一种用法,使用<association>标签
<resultMap id="wmd_pets" type="pets">
<result property="id" column="pid"/>
<result property="type" column="type"/>
<result property="name" column="pet_name"/>
<result property="masterId" column="master_id"/>
//重点1:使用association 标签来标注对象中的引用,javaType标明引用的类型(可以是别名)
<association property="person" javaType="wmd">
<result property="id" column="id"/>
<result property="person_name" column="name"/>
<result property="age" column="age"/>
<result property="message" column="message"/>
</association>
</resultMap>
3.可以将其全部拆分
<resultMap id="wmd_pets" type="pets">
<result property="id" column="pid"/>
<result property="type" column="type"/>
<result property="name" column="pet_name"/>
<result property="masterId" column="master_id"/>
重点1:拆分标签,使用resultMap进行引用,和下述resultmap的id对应
<association property="person" resultMap="person_wmd">
</association>
</resultMap>
<resultMap id="person_wmd" type="wmd">
<result property="id" column="id"/>
<result property="person_name" column="name"/>
<result property="age" column="age"/>
<result property="message" column="message"/>
</resultMap>