当前位置:   article > 正文

Mybatis常用的注解_mybatis注解

mybatis注解

Mybatis常用的注解

MyBatis的常用注解以及简单使用
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);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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();  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.关系映射
1)一对一

@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();  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2)一对多

package com.skymr.mybatis.mappers;  

import org.apache.ibatis.annotations.Many;  
import org.apache.ibatis.annotations.Result;  
import org.apache.ibatis.annotations.Results;  
import org.apache.ibatis.annotations.Select;  

import com.skymr.mybatis.model.Grade;  

public interface Grade2Mapper {  

    @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);  
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
package com.skymr.mybatis.mappers;  

import java.util.List;  
import org.apache.ibatis.annotations.Delete;  
import org.apache.ibatis.annotations.Insert;  
import org.apache.ibatis.annotations.One;  
import org.apache.ibatis.annotations.Result;  
import org.apache.ibatis.annotations.Results;  
import org.apache.ibatis.annotations.Select;  
import org.apache.ibatis.annotations.Update;  

import com.skymr.mybatis.model.Student;  

public interface Student2Mapper {  

    @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);  

    @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_Student where grade_id=#{gradeId}")  
        @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> getStudentsByGradeId(int gradeId);  
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

4.动态sql注解映射
provider类

package com.skymr.mybatis.mappers.provider;  

import java.util.Map;  
import org.apache.ibatis.jdbc.SQL;  
import com.skymr.mybatis.model.Student;  

public class StudentDynaSqlProvider {  

    public String insertStudent(final Student student){  
        return new SQL(){  
            {  
                INSERT_INTO("mybatis_Student");  
                if(student.getName() != null){  
                    VALUES("name","#{name}");  
                }  
                if(student.getAge() > 0){  
                    VALUES("age","#{age}");  
                }  
            }  
        }.toString();  
    }  

    public String updateStudent(final Student student){  
        return new SQL(){  
            {  
                UPDATE("mybatis_Student");  
                if(student.getName() != null){  
                    SET("name=#{name}");  
                }  
                if(student.getAge() > 0){  
                    SET("age=#{age}");  
                }  
                WHERE("id=#{id}");  
            }  
        }.toString();  
    }  

    public String getStudent(final Map<String,Object> map){  
        return new SQL(){  
            {  
                SELECT("*");  
                FROM("mybatis_Student");  
                if(map.containsKey("name")){  
                    WHERE("name like #{name}");  
                }  
                if(map.containsKey("age")){  
                    WHERE("age=#{age}");  
                }  
            }  
        }.toString();  
    }  

    public String deleteStudent(){  
        return new SQL(){  
            {  
                DELETE_FROM("mybatis_Student");  
                WHERE("id=#{id}");  
            }  
        }.toString();  
    }  
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

Mapper接口

@SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent")  
public List<Student> getStudents(Map<String,Object> map);  
  • 1
  • 2

结语

这是我总结的mybatis常用的注解,这些注解相对简单。以后我每天都会写一篇博客。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/72239
推荐阅读
相关标签
  

闽ICP备14008679号