zoukankan      html  css  js  c++  java
  • MyBatis的基本注解

     MyBatis的基本注解:

    增删改查

    @Select("select * from teacher")
    
     public List<Teacher> selAll();
    
     
    
     //增删改返回值都是int
    
     @Insert("insert into teacher values(default,#{name})")
    
     public int add(Teacher teacher);
    
     
    
     @Update("update teacher set name=#{name} where id=#{id}")
    
     public int update(Teacher teacher);
    
     
    
     @Delete("delete from teacher where id=#{id}")
    
     public int delete(int id);
    

      

    MyBatis映射

    1.普通映射

     

    @Select("select * from mybatis_Student where id=#{id}")  
    public Student getStudent(int id);  
    @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
    public int insert(Student student);  
    @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
    public int update(Student student);  
    @Delete("delete from mybatis_Student where id=#{id}")  
    public int delete(int id); 
    

     

     2.结果集映射

    @Select("select * from mybatis_Student")  
    @Results({  
        @Result(id=true,property="id",column="id"),  
        @Result(property="name",column="name"),  
        @Result(property="age",column="age")  
    })  
    public List<Student> getAllStudents(); 
    

    3.关系映射

    一对一

    @Select("select * from mybatis_Student")  
    @Results({  
        @Result(id=true,property="id",column="id"),  
        @Result(property="name",column="name"),  
        @Result(property="age",column="age"),  
        @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
    })  
    public List<Student> getAllStudents();
    

     一对多

    @Select("select * from mybatis_grade where id=#{id}")  
        @Results({  
            @Result(id=true,column="id",property="id"),  
            @Result(column="grade_name",property="gradeName"),  
            @Result(property="students",column="id",many=@Many(select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))  
        })  
        public Grade getGrade(int id);
    

      

     

  • 相关阅读:
    Vue GET xxxx/sockjs-node/info?t=1573626343344 net::ERR_CONNECTION
    NavigationDuplicated Navigating to current location (“/XXX”) is not allowed
    node-sass报错(Node Sass could not find a binding for your current environment)
    DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
    VSCODE 中.art文件识别为html文件
    gulp4.0构建任务
    gulp报错The following tasks did not complete
    setTimeout()
    格式化日期
    作业1.3
  • 原文地址:https://www.cnblogs.com/wishsaber/p/11714752.html
Copyright © 2011-2022 走看看