天天看點

mybatis(一對一、一對多、多對多)分表操作模闆

一對一:

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());
        }
    }

}
           

學生 跟 課程 之間是多對多

一個學生 對應多個中間表

一個中間表 又對應多個課程表

繼續閱讀