Mybatis 基础进阶 一对多 多对多关系

Mapper XML 文件

使用xml文件实现 一些常用的查询操作

  1. 准备工作 一张person 表
create table person
(
  id      int auto_increment
    primary key,
  name    varchar(100) null,
  address varchar(100) null,
  sex     varchar(10)  null,
  age     int          null
)

2.实体类
3.接口 PersonDao.java
4. PersonDao.xml
5.在configuration.xml配置文件中添加一行别名 方便我们下面的配置

<typeAliases>
        
        <package name="com/james/bean">package>
    typeAliases>

 // 根据多个id 查询person
    public List<Person> selectPersonByIds(List<Integer> list);

// resultType 虽然返回值是List类型 但是这里使用类名 因为mybatis已经帮我们处理好了集合类型
// 因此直接添加集合中的类型即可
// foreach 标签被遍历的集合
         collection 指定的类型
         item 每一次遍历得到的对象
         separator 分隔符
         open 开始
         close 结束
<select id="selectPersonByIds" parameterType="java.util.List" resultType="Person">
        select * from person where id in
        <foreach collection="list" item="i" separator="," open="(" close=")">
            #{i}
        foreach>

    select>

Test.java

 @Test
    public void testMain3() throws Exception {
        // 构造器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 读取配置文件
        InputStream input = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 获取会话工厂
        SqlSessionFactory factory = builder.build(input);
        // 获取会话
        SqlSession session = factory.openSession();
        PersonDao dao = session.getMapper(PersonDao.class);
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(6);
        List lists = dao.selectPersonByIds(list);
        System.out.println(lists);
    }
// 根据姓名和性别同时查询
    public List<Person> getPerson(Person person);
    
    <select id="getPerson" parameterType="Person" resultType="Person">
        select * from person
        <where>
        <if test="name != null and name != '' ">
            and name = #{name}
        if>
        <if test="sex != null and sex != '' ">
            and sex = #{sex}
        if>
    where>
    select>
    // test 方法不在演示

mybatis 实现 一对多 与 多对一关系

准备一张orders 表

create table orders
(
  id       int auto_increment
    primary key,
  name     varchar(100) null,
  price    double       null,
  date     date         null,
  personid int          null,
  constraint orders_person_id_fk
  foreign key (personid) references person (id)
    on update cascade
    on delete cascade
)

创建orders的实体类

 // 获取所有订单
    public List<Orders> getAllOrder();

 // 获取所有人
 public List<Person> getAllPerson();



<mapper namespace="com.lanou.dao.OrdersDao">
    
    <resultMap id="OrdersNap" type="Orders" autoMapping="true">
        
        <result property="name" column="o_name">result>
        <result property="id" column="o_id">result>
        
        <association property="person" autoMapping="true" javaType="Person">association>
    resultMap>
    
    <select id="getAllOrder" resultType="Orders" resultMap="OrdersNap">
        select *,orders.name o_name,orders.id o_id from person right join orders on person.id = orders.personid
    select>
<! -- 一对多关系 注意:autoMapping 一定不要忘记写-->
    <resultMap id="PersonMap" type="Person" autoMapping="true">
        <result property="name" column="p_name">result>
        <result property="id" column="p_id">result>
        // 一对多关系使用 collection 字段
        <collection property="orders" autoMapping="true" ofType="Orders">
            <result property="name" column="o_name">result>
            <result property="id" column="o_id">result>
        collection>
    resultMap>

    <select id="getAllPerson" resultMap="PersonMap" resultType="Person">
        select *,person.name p_name,person.id p_id,orders.name o_name,orders.id o_id
        from person left join orders on person.id =  orders.personid
    select>
mapper>

@Test
    public void testMain4() throws Exception {
        // 构造器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 读取配置文件
        InputStream input = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 获取会话工厂
        SqlSessionFactory factory = builder.build(input);
        // 获取会话
        SqlSession session = factory.openSession();

        OrdersDao dao = session.getMapper(OrdersDao.class);
        List<Orders> list = dao.getAllOrder();
        for (Orders od : list) {
            System.out.println(od);
        }

    }

多对多关系

1.创立一个群组表

create table groups
(
  g_id     int auto_increment
    primary key,
  g_name   varchar(100) null,
  g_msg    varchar(100) null,
  g_manage varchar(100) null
)

创立一个中间表

create table group_person
(
  g_id int null,
  p_id int null,
  constraint group_person_groups_g_id_fk
  foreign key (g_id) references groups (g_id)
    on update cascade
    on delete cascade,
  constraint group_person_person_id_fk
  foreign key (p_id) references person (id)
    on update cascade
    on delete cascade
)




<mapper namespace="com.lanou.dao.GroupsDao">
    
    <resultMap id="GroupsMap" type="Groups" autoMapping="true">
        <collection property="person" autoMapping="true" ofType="Person">collection>
    resultMap>

    <select id="selectAllGroups" resultType="Groups" resultMap="GroupsMap">
        select * from groups left join group_person on group_person.g_id = groups.g_id
          left join person on group_person.p_id = person.id;
    select>

    <resultMap id="PersonMap" type="Person" autoMapping="true">

        <collection property="groups" autoMapping="true" ofType="Groups">collection>
    resultMap>

    <select id="selectAllPerson" resultType="person" resultMap="PersonMap">
          select * from person left join group_person g on person.id = g.p_id
  left join groups g2 on g.g_id = g2.g_id;

    select>
mapper>

 @Test
    public void test() throws IOException {
        // 构造器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 读取配置文件
        InputStream input = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 获取会话工厂
        SqlSessionFactory factory = builder.build(input);
        // 获取会话
        SqlSession session = factory.openSession();

        GroupsDao dao = session.getMapper(GroupsDao.class);
        List<Person> list = dao.selectAllPerson();
        for (Person person :list){
            System.out.println(person);
        }
    }

你可能感兴趣的:(java)