zoukankan      html  css  js  c++  java
  • Mybatis使用注解查询

    MyBatis使用注解查询主要有两种方式:

    一、查询两遍,再进行关联整合,不使用联表查询语句。

    1、Mapper文件:

     1     @Select("SELECT stu.id, stu.stu_key, stu.stu_no, stu.stu_name, stu.tel, stu.email_add, stu.birthdate, stu.gender, stu.school_id, stu.college_id, stu.major_id, stu.class_id, stu.description, stu.remark, stu.create_date, stu.update_date FROM stu_info stu WHERE stu.stu_key = #{stuKey}")
     2     @Results({
     3          
     4             @Result(
     5                     //重要:column:关联的外键 property:实体类属性,不是字段,StuInfo里面的
     6                     column = "school_id",property = "schoolInfo",
     7                     one = @One(select = "com.ahu.mapper.StuInfoMapper.getSchoolInfo")
     8             )
     9     })
    10     StuInfo showCompInfoByStuKey(String stuKey);
    11 
    12     @Select("SELECT school_info.id, school_info.school_id, school_info.school_name, school_info.create_time, school_info.update_time, school_info.remark FROM school_info WHERE school_info.school_id= #{school_info.school_id}")
    13     SchoolInfo getSchoolInfo();

    2、StuInfo实体类

     1 @Data
     2 public class StuInfo implements Serializable {
     3 
     4     //@TableId(type = IdType.AUTO) //主键自增 数据库中需要设置主键自增
     5     private Integer id;
     6     //stuKey作为主键使用,不进行更新操作,所以使用该策略
     7     @TableField(updateStrategy = FieldStrategy.NEVER)
     8     private String stuKey;
     9     private String stuNo;
    10     private String stuName;
    11     private String password;
    12     private String tel;
    13     private String emailAdd;
    14     /**
    15      * @Description: 注解@JsonFormat主要是后台到前台的时间格式的转换
    16      * 注解@DataFormAT主要是前后到后台的时间格式的转换
    17      */
    18     @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    19     @DateTimeFormat(pattern = "yyyy-MM-dd")
    20     private Date birthdate;
    21     private String gender;
    22     private String schoolId;
    23     private String collegeId;
    24     private String majorId;
    25     private String classId;
    26     private String description;
    27     private String remark;
    28     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    29     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    30     private Date createDate;
    31     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    32     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    33     private Date updateDate;
    34 
    35     //数据库中不存在该字段,使用@TableField注解处理,联表查询使用
    36     @TableField(exist = false)
    37     private ClassInfo classInfo;
    38     @TableField(exist = false)
    39     private CollegeInfo collegeInfo;
    40     @TableField(exist = false)
    41     private MajorInfo majorInfo;
    42     @TableField(exist = false)
    43     private SchoolInfo schoolInfo;
    44 }

    二、查询一遍,使用联表查询语句。

     1     @Select("SELECT stu.id, stu.stu_key, stu.stu_no, stu.stu_name,  stu.tel, stu.email_add, stu.birthdate, stu.gender, stu.school_id, sch.school_name, stu.college_id, col.college_name, stu.major_id, maj.major_name, stu.class_id, cla.class_name, stu.description, stu.remark, stu.create_date, stu.update_date FROM stu_info stu LEFT JOIN school_info sch ON stu.school_id = sch.school_id LEFT JOIN college_info col ON stu.college_id = col.college_id LEFT JOIN major_info maj ON stu.major_id = maj.major_id LEFT JOIN class_info cla ON stu.class_id = cla.class_id WHERE stu.stu_key = #{stuKey}")
     2     @Results({
     3             //@Result(column="school_id",property="schoolId"),
     4             @Result(
     5                     //重要:column:关联的外键 property:实体类属性,不是字段,StuInfo stuInfo里面的
     6                     column = "school_id",property = "schoolInfo.schoolId"),
     7             @Result(
     8                     //主表也要做映射,不然为空null
     9                     column = "school_id",property = "schoolId"),
    10             @Result(column = "school_name",property = "schoolInfo.schoolName"),
    11             @Result(column = "college_id",property = "collegeInfo.collegeId"),
    12             @Result(column = "college_id",property = "collegeId"),
    13             @Result(column = "college_name",property = "collegeInfo.collegeName"),
    14             @Result(column = "major_id",property = "majorInfo.majorId"),
    15             @Result(column = "major_id",property = "majorId"),
    16             @Result(column = "major_name",property = "majorInfo.majorName"),
    17             @Result(column = "class_id",property = "classInfo.classId"),
    18             @Result(column = "class_id",property = "classId"),
    19             @Result(column = "class_name",property = "classInfo.className"),
    20     })
    21     StuInfo showCompInfoByStuKey(String stuKey);

    参考:

    1、https://blog.csdn.net/qq_36228916/article/details/93881786

    2、https://blog.csdn.net/z357904947/article/details/97975814

  • 相关阅读:
    字典或者数组与JSON串之间的转换
    银联支付 支付代码
    iOS 一个新方法:- (void)makeObjectsPerformSelector:(SEL)aSelector;
    iOS 直接使用16进制颜色
    iOS 添加view的分类(更加方便的设置view的位置)
    iOS 中UITableView的深理解
    Swift 中调试状态下打印日志
    手把手教React Native实战开发视频教程【更新到40集啦。。。】
    React Native 开发
    React-Native学习指南
  • 原文地址:https://www.cnblogs.com/116970u/p/12553276.html
Copyright © 2011-2022 走看看