MyBatis多对多映射查询 Day12 2018-11-30

7 一对多查询

元素的解释:

  • column 表示当前查询数据库表的列名DEPARTMENT_ID
  • property 表示返回类型PhoneUserIdAndDepartment属性名department
  • javaType 表示该属性对于的类型名称,本例是一个ArrayList集合
  • select 表示执行的查询语句,将查询到的数据封装到property所代表的类型对象当中
  • ofType 表示集合当中的类型

7.1 基本应用

7.1.1 java bean
  • 同 6.1.1
7.1.2 映射文件

    
        
        
        
        
        
    
    
    
  


7.1.3 测试代码
SqlSession session = SqlSessionFactoryUtil.getSession();
        List classes = session.selectList("com.zhougl.web.dao.WebClassDao.selectClass");
        classes.forEach(classe ->{
            System.out.println(classe);
            List students = classe.getStudents();
            students.forEach(student -> System.out.println(student));
        });
        session.commit();
        session.close();
  • 结果
==>  Preparing: select * from WEB_CLASS 
==> Parameters: 
<==      Total: 2
==>  Preparing: select * from STUDENT where class_id = ? 
==> Parameters: 2(Integer)
<==      Total: 2
WebClass [id=2, code=C002, name=无限流战斗班]
Student [id=2, name=王怡, sex=女, age=24]
Student [id=4, name=王多燕, sex=女, age=26]
==>  Preparing: select * from STUDENT where class_id = ? 
==> Parameters: 1(Integer)
<==      Total: 2
WebClass [id=1, code=C001, name=大乱斗指导班]
Student [id=1, name=王一倩, sex=女, age=22]
Student [id=3, name=王二赞, sex=男, age=28]

7.2 一对多映射

7.2.1 mybatis配置
  • mybatis-config.xml添加如下配置

    
    
    
    

7.2.2 java bean
  • 同 6.1.1
7.2.3 mapper映射文件
  • WebClassMapper.xml

    
        
        
        
        
        
            
            
            
            
        
    

    
        ID, CODE, NAME
    
    


7.2.4 mapper接口
  • WebClassMapper.xml
public interface WebClassMapper {
  
    WebClass selectWebClassById(int i);
}
7.2.5 测试类
public class OneToManyTest {

    public static void main(String[] args) {
        SqlSession session = SqlSessionFactoryUtil.getSession();
        OneToManyTest test = new OneToManyTest();
        //测试一对多
        test.testOneToMany(session);
        
        //测试多对一
        //test.testManyToOne(session);
        session.commit();
        session.close();

    }
    //测试一对多,查询班级(一)级联查询学生(多)
    public void testOneToMany(SqlSession session) {
        WebClassMapper mapper = session.getMapper(WebClassMapper.class);
        WebClass webClass = mapper.selectWebClassById(1);
        System.out.println(webClass.getId()+" "+webClass.getCode()+" "+webClass.getName());
        System.out.println(webClass.toString());
        List students = webClass.getStudents();
        for (Student student : students) {
            System.out.println(student.toString());
        }
    }

}
==>  Preparing: select ID, CODE, NAME from WEB_CLASS where ID = ? 
==> Parameters: 1(Integer)
<==      Total: 1
1 C001 大乱斗指导班
==>  Preparing: select ID as studentId, NAME as studentName, SEX, AGE, CLASS_ID from STUDENT where CLASS_ID = ? 
==> Parameters: 1(Integer)
<==      Total: 4
WebClass [id=1, code=C001, name=大乱斗指导班]
Student [id=1, name=王一倩, sex=女, age=22]
Student [id=2, name=王怡, sex=女, age=24]
Student [id=3, name=王二赞, sex=男, age=28]
Student [id=4, name=王多燕, sex=女, age=26]

7.3 多对一映射

7.3.1 java bean
  • 同 6.1.1

7.3.2 mapper配置

  • StudentMapper.xml

  
    
    
    
    
    
    
        
        
        
    
  
 
  
    ID as studentId, NAME as studentName, SEX, AGE, CLASS_ID
  
  
    ${student}.ID as studentId, ${student}.NAME as studentName, ${student}.SEX, ${student}.AGE, ${student}.CLASS_ID
  
  
        ${webClass}.ID , ${webClass}.CODE, ${webClass}.NAME 
  
 
 
  
  
 

7.3.3 mapper接口
  • StudentMapper.java
public interface StudentMapper {
   
    Student selectStudentById(int id);
    List selectStudentByClassId(int classId);

}
7.3.4 测试类
public class OneToManyTest {
    //测试多对一,查询学生(多)级联查询班级(一)
    public void testManyToOne(SqlSession session) {
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        Student student = studentMapper.selectStudentById(1);
        System.out.println(student);
        System.out.println(student.getWebClass().toString());
    }

  • 结果
==>  Preparing: select s.ID as studentId, s.NAME as studentName, s.SEX, s.AGE, s.CLASS_ID , c.ID , c.CODE, c.NAME from STUDENT s,WEB_CLASS c where s.class_id=c.id and s.ID = ? 
==> Parameters: 1(Integer)
<==      Total: 1
Student [id=1, name=王一倩, sex=女, age=22]
WebClass [id=1, code=C001, name=大乱斗指导班]

7.4 多对多映射

7.4.1 java bean
public class WebOrder {
    private BigDecimal id;
    private String code;
    private BigDecimal total;
    private BigDecimal userId;
    //订单和用户是多对一关系
    private WebUser user;
    //订单和商品是多对多关系
    private List articles;
}
public class WebUser {
    private BigDecimal id;
    private String username;
    private String loginname;
    private String password;
    private String phone;
    private String address;
    
    //用户和订单是一对多关系
    private List orders;
}
public class WebArticle {
    private BigDecimal id;
    private String name;
    private BigDecimal price;
    private String remark;
}
7.4.2 mapper配置
  • WebOrderMapper.xml

  
    
    
    
    
    
         
        
        
        
        
        
    
    
    
        
        
        
        
    
  
    
  
  


  
    
    
    
    
    
    
    
    
        
        
        
    
  
 
  
    ID, USERNAME, LOGINNAME, PASSWORD, PHONE, ADDRESS
  
 
  
  


  
    
    
    
    
  
  
  
    ID, NAME, PRICE, REMARK
  
  
  
  

7.4.3 mapper接口
public interface WebOrderMapper {
    List selectOrderByUserId(int userId);
    WebOrder selectOrderById(int id); 
}
public interface WebUserMapper {
    WebUser selectUserById(int id);
}
7.4.4 测试类

public class ManyToManyTest {

    public static void main(String[] args) {
        SqlSession session = SqlSessionFactoryUtil.getSession();
        ManyToManyTest test = new ManyToManyTest();
        //test.testOneToMany(session);
        test.testManyToMany(session);
        session.commit();
        session.close();
    }
    public void testOneToMany(SqlSession session) {
        WebUserMapper userMapper = session.getMapper(WebUserMapper.class);
        WebUser user = userMapper.selectUserById(1);
        System.out.println(user.getUsername()+" "+user.getLoginname()+" "+user.getPhone()+" "+user.getAddress());
        List orders = user.getOrders();
        for (WebOrder webOrder : orders) {
            System.out.println(webOrder.toString());
        }
    }
    public void testManyToMany(SqlSession session) {
        WebOrderMapper orderMapper = session.getMapper(WebOrderMapper.class);
        WebOrder order = orderMapper.selectOrderById(1);
        System.out.println(order.getCode()+" "+order.getTotal());
        WebUser user = order.getUser();
        System.out.println(user.toString());
        List articles = order.getArticles();
        for (WebArticle webArticle : articles) {
            System.out.println(webArticle.toString());
        }
    }
}

你可能感兴趣的:(MyBatis多对多映射查询 Day12 2018-11-30)