一对一:
public class Person {
private Integer id;
private String name;
private Date birthday;
private String address;
private Car car;
}
public class Car {
private Integer cid;
private String cname;
private Integer pid;
}
public interface PersonMapper {
//一对一分表查询
List<Person> list();
}
public interface CarMapper {
//一对一分表查询
Car get(int pid);
}
service同mapper
PerSonMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.PersonMapper">
<resultMap id="baseResultMap" type="com.wwz.pojo.Person">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="birthday" property="birthday" />
<result column="address" property="address" />
<association property="car" javaType="com.wwz.pojo.Car" column="id"
select="com.wwz.mapper.CarMapper.get"></association>
</resultMap>
<select id="list" resultMap="baseResultMap">
select * from person
</select>
CarMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.CarMapper">
<resultMap id="baseResultMap" type="com.wwz.pojo.Car">
<id column="cid" property="cid" />
<result column="cname" property="cname" />
<result column="pid" property="pid" />
</resultMap>
<select id="get" resultMap="baseResultMap">
select * from `car` where pid = #{pid}
</select>
</mapper>
public class PersonTest extends BaseTest {
//一对一分表查询
@Test
public void testList() throws IOException {
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
List<Person> personList = mapper.list();
//遍历集合
for (Person person : personList) {
System.out.println(person);
System.out.println(person.getCar());
}
}
}
一对多:
public class Person {
private Integer id;
private String name;
private Date birthday;
private String address;
private List<Order> orderList;
}
public class Order {
private Integer oid;
private String oname;
private Integer pid;
}
public interface PersonMapper {
//一对多分表查询
Person get(int id);
}
public interface OrderMapper {
//一对多分表查询 注意这里一定要把id带上,你得指定一个id,它才好去查
List<Order> list(int pid);
}
service同mapper
PerSonMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.PersonMapper">
<resultMap id="baseResultMap2" type="com.wwz.pojo.Person">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="birthday" property="birthday" />
<result column="address" property="address" />
<collection property="orderList" ofType="com.wwz.pojo.Order" column="id"
select="com.wwz.mapper.OrderMapper.list"></collection>
</resultMap>
<select id="get" resultMap="baseResultMap2">
select * from person where id = #{id}
</select>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.OrderMapper">
<resultMap id="baseResultMap" type="com.wwz.pojo.Order">
<id column="oid" property="oid" />
<result column="oname" property="oname" />
<result column="pid" property="pid" />
</resultMap>
<select id="list" resultMap="baseResultMap">
select * from `order` where pid = #{pid}
</select>
</mapper>
多对多:
多对多实际上就是拆分的一对一和一对多
public class Student {
private int sid;
private String sname;
//一个学生有多个StuCou
private List<StuCou> stuCouList;
}
这是中间表:
public class StuCou {
private int scid;
private int sid;
private int cid;
//一个StuCou对应一个course
private Course course;
}
public class Course {
private int cid;
private String cname;
}
public interface StudentMapper {
//一对多
Student get(int sid);
}
public interface StuCouMapper {
//一对多
List<StuCou> list(int sid);
}
public interface CourseMapper {
//一对一
Course get(int cid);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.StudentMapper">
<!--分表查询-->
<resultMap id="baseResultMap" type="com.wwz.pojo.Student">
<id column="sid" property="sid" />
<result column="sname" property="sname" />
<collection property="stuCouList" ofType="com.wwz.pojo.StuCou"
select="com.wwz.mapper.StuCouMapper.list" column="sid"></collection>
</resultMap>
<select id="get" resultMap="baseResultMap">
select * from `student` where `sid` = #{sid}
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.StuCouMapper">
<resultMap id="baseResultMap" type="com.wwz.pojo.StuCou">
<id column="scid" property="scid" />
<result column="sid" property="sid" />
<result column="cid" property="cid" />
<association property="course" javaType="com.wwz.pojo.Course"
select="com.wwz.mapper.CourseMapper.get" column="cid"></association>
</resultMap>
<select id="list" resultMap="baseResultMap">
select * from `stu_cou` where `sid` = #{sid}
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 命名空间:映射文件唯一标识符,相当于类名
作用:区别多个mapper StudentMapper DogMapper-->
<mapper namespace="com.wwz.mapper.CourseMapper">
<resultMap id="baseResultMap" type="com.wwz.pojo.Course">
<id column="cid" property="cid" />
<result column="cname" property="cname" />
</resultMap>
<select id="get" resultMap="baseResultMap">
select * from `course` where `cid` = #{cid}
</select>
</mapper>
public class StudentTest extends BaseTest {
@Test
public void test(){
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.get(1);
System.out.println("该学生"+student.getSname()+"有如下课程:");
List<StuCou> stuCouList = student.getStuCouList();
for (StuCou stuCou : stuCouList) {
System.out.println(stuCou.getCourse().getCname());
}
}
}
学生 跟 课程 之间是多对多
一个学生 对应多个中间表
一个中间表 又对应多个课程表