一、一对多

举例:一个人,对应多辆车

1.1 实体类

1.1.1 PerSon类

  1. package com.woniuxy.entity;
  2. import java.util.List;
  3. import lombok.Data;
  4. @Data
  5. public class Person {
  6. private int pid;
  7. private String pname;
  8. private List<Car> cars;
  9. }

1.1.2 Car类

  1. package com.woniuxy.entity;
  2. import lombok.Data;
  3. @Data
  4. public class Car {
  5. private int cid;
  6. private String cname;
  7. }

1.2 接口

PersonMapper.java 接口

  1. package com.woniuxy.mapper;
  2. import com.woniuxy.entity.Person;
  3. public interface PersonMapper {
  4. //连表查询
  5. public Person findPersonByPid(int pid);
  6. // n+1查询
  7. public Person findPersonByPid2(int pid);
  8. }

1.3 XML文件

PersonMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.woniuxy.mapper.PersonMapper">
  6. <!-- 连表查询 -->
  7. <select id="findPersonByPid" resultMap="personMap">
  8. select * from person
  9. p,car c where p.pid=c.pid and p.pid=#{pid}
  10. </select>
  11. <resultMap type="Person" id="personMap">
  12. <id column="pid" property="pid" />
  13. <result column="pname" property="pname" />
  14. <!-- 多个 -->
  15. <collection property="cars" ofType="Car">
  16. <id column="cid" property="cid" />
  17. <result column="cname" property="cname" />
  18. </collection>
  19. </resultMap>
  20. <!-- n+1 -->
  21. <select id="findPersonByPid2" resultMap="personMap2">
  22. select * from person where pid=#{pid}
  23. </select>
  24. <resultMap type="Person" id="personMap2">
  25. <id column="pid" property="pid" />
  26. <collection property="cars" column="pid" select="findCarByPid"></collection>
  27. </resultMap>
  28. <select id="findCarByPid" resultType="Car">
  29. select * from car where pid=#{pid}
  30. </select>
  31. </mapper>

注意:xml文件中主要不同的地方在于,一对一,映射另一张表关系的时候使用的是,而1对多使用的是,多对多同理

1.4 测试类

OneToMoreTest 测试类

package com.woniuxy.test;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.woniuxy.mapper.PersonMapper;

public class OneToMoreTest {

    public static void main(String[] args) throws IOException {

        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = factory.openSession();

        PersonMapper personMapper = session.getMapper(PersonMapper.class);
//        System.out.println(personMapper.findPersonByPid(1001));
        System.out.println(personMapper.findPersonByPid2(1001));
    }

}

二、多对多

多对多与一对多主要不同的地方在于有一张中间表,其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">

<mapper namespace="com.woniuxy.mapper.ClazzMapper">
    <select id="findClazzByCid" resultMap="clazzMap">
        select * from clazz where cid = #{cid}
    </select>

    <resultMap type="Clazz" id="clazzMap">
        <id column="cid" property="cid"/>
        <collection property="teachers" column="cid" select="findTeacherByCid"></collection>
    </resultMap>

    <select id="findTeacherByCid" resultType="Teacher">
        select * from teacher t,clazz_teacher ct where t.tid=ct.tid and ct.cid=#{cid}
    </select>
</mapper>

逻辑:先通过id查其中一张表(clazz)表,再联查中间表(clazz_teacher)和另外一张表(teacher),用第一张表的id和中间表的id,建立关系,并且用第二张表和中间表的id建立关系,例如:

select * from teacher t,clazz_teacher ct where t.tid=ct.tid and ct.cid=#{cid}

三、动态SQL

3.1 if拼接

<!-- 假设有这样一个需求:用户可以输入,选择多个查询条件 这些条件有:
          图书名字(模糊)、分类、作者(模糊)三个作为查询条件
          如果一个查询条件都没有,则查询出所有的商品,有指定查询条件就作为查询条件查询出对应的数据
          if    用来判断条件是否成立,如果成立则将其中的SQL语句拼接到SQL的后边
   -->
    <select id="findIf" resultType="Goods">
        select * from mall_goods
       <!-- where 可以根据其中的条件是否成立自动决定是否添加where关键字,
           而且还能够自动去除多余的and 和 or 
       -->
           <where>
               <if test="name!=null and name.length()!=0">
                   and name like #{name}
               </if>
               <if test="categoryid!=0">
                   and categoryid = #{categoryid}
               </if>
               <if test="author!=null and author.length()!=0">
                   and author like #{author}
               </if>
           </where>
    </select>

3.2 foreach循环批量操作

<!-- 批量删除数据 -->
    <deldete id="delGoodsByIds">
        delete from mall_goods where id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </deldete>