MyBatis系列第三篇:MyBatis中Mapper详解与级联查询

MyBatis框架的主要配置文件:mybatis-config.xml文件和Mapper.xml文件。

一、Mapper.xml文件





    
        
        
        
        
        
        
        
        
    

    
        select Id, Name, Age, Salary, Sex, Status, AddTime, UpdateTime from RUN_User
    

1、statement标签:

select、update、delete、insert标签,分别对应查询、更新、删除、新增操作

2、parameterType标签:方法入参类型

(1)基本数据类型:

    

(2)String数据类型:

    

(3)包装类,通过id查询:

    

(4)JavaBean参数:

    
        update RUN_User set name = #{userEntity.name}, age = #{userEntity.age}, salary = #{userEntity.salary}, sex = #{userEntity.sex}, status = 1
        where id = #{userEntity.id}
    

3、resultType标签:方法出参类型

(1)基本数据类型:

    

(2)包装类型:

    

(3)String类型:

    

(4)JavaBean类型:

    

4、级联查询

4.1 建表:班级表与学生表

CREATE TABLE `RUN_Class` (
  `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID(班级)',
  `ClassName` varchar(256) NOT NULL COMMENT '班级名称',
  `Status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '有效=1,无效=-1',
  `AddTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
  `UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB COMMENT='班级信息表';


CREATE TABLE `RUN_Student` (
  `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID(学号)',
  `Name` varchar(256) NOT NULL DEFAULT '' COMMENT '姓名',
  `ClassId` int(11) NOT NULL COMMENT '班级',
  `Status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '有效=1,无效=-1',
  `AddTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
  `UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB COMMENT='学生信息表';

4.2 创建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ClassEntity implements Serializable {

    private static final long serialVersionUID = -3337804323389088625L;

    private int id;                                 //班级ID
    private String className;                       //班级名称
    private int status;                             //是否有效(1:有效,-1:无效)
    private String addTime;                         //添加时间
    private String updateTime;                      //更新时间
    private List studentEntities;    //该班级中有哪些学生

}

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class StudentEntity implements Serializable {

    private static final long serialVersionUID = -7497520016303190017L;

    private int id;                     //学号
    private String name;                //姓名
    private int classId;                //班级
    private int status;                 //是否有效(1:有效,-1:无效)
    private String addTime;             //添加时间
    private String updateTime;          //更新时间
    private ClassEntity classEntity;    //该学生属于哪个班级
}

4.3 写DAO层接口

public interface StudentRepository {
    StudentEntity queryStudent(@Param("id") int id);
    StudentEntity queryStudentWithClass(@Param("id") int id);
}

4.4 写Mapper.xml文件





    
        
        
        
        
        
        
    

    
        
        
        
        
        
        
        
            
            
            
        
    

    
        select Id, Name, ClassId, Status, AddTime, UpdateTime from RUN_Student
    

    

    


4.5 把Mapper.xml文件注册到MyBatis配置文件中





    
        
    

    
    
        
            
            
            
            
                
                
                
                
            
        
    

    
    
        
        
        
    

4.6 一对多查询测试(从一到多):不带java对象

public class UserTest3 {

    public static void main(String[] args) {
        //加载MyBatis配置文件
        InputStream inputStream = UserTest3.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //获取实现接口的代理对象
        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
        StudentEntity studentEntity = studentRepository.queryStudent(2);
        System.out.println(studentEntity);
        sqlSession.close();
    }
}

4.7 一对多查询测试(从一到多):带java对象

4.8 一对多查询测试(从多到一)

public interface ClassRepository {
    ClassEntity queryClassByClassId(@Param("id") int id);
}




    
        
        
        
        
        
        
            
            
            
            
            
            
        
    

    
        select c.Id, c.ClassName, c.Status, c.AddTime, c.UpdateTime,
        s.Id as sId, s.Name as sName, s.ClassId as sClassId, s.Status as sStatus, s.AddTime as sAddTime, s.UpdateTime as sUpdateTime
        from RUN_Class c join RUN_Student s on c.Id =  s.classId
    

    

public class UserTest3 {

    public static void main(String[] args) {
        //加载MyBatis配置文件
        InputStream inputStream = UserTest3.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //获取实现接口的代理对象
        ClassRepository classRepository = sqlSession.getMapper(ClassRepository.class);
        ClassEntity classEntity = classRepository.queryClassByClassId(3);
        System.out.println(classEntity);
        sqlSession.close();
    }
}

4.9 多对多查询:

(1)实体类 顾客:Customer。实体类 商品:Goods。

 @Data
 public class Customer {
     private long id;
     private String name;
     private List goods;
}

@Data
 public class Goods {
     private long id;
     private String name;
     private List customers;
}

(2)中间表:用来存储顾客和商品之间的关系

@Data
 public class Custom_Goods {
     private long id;
     private long cid;
     private long gid;
}

(3)顾客接口:CustomerRepository

public interface CustomerRepository {
     public Customer findById(long id);
}

(4)顾客SQL语句:CustomerRepository.xml


 
 
     
         
         
         
             
             
         
     

     

(5)商品接口:GoodsRepository

public interface GoodsRepository {
     public Goods findById(long id);
}

(6)商品SQL语句:GoodsRepository.xml


 
 
     
         
         
         
             
             
         
     

     

 

 

 

你可能感兴趣的:(MyBatis专题,mybatis,java,xml,mysql,mysql级联查询)