一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java 中MyBatis注解映射的實例詳解

java 中MyBatis注解映射的實例詳解

2020-12-28 09:22yunlian0621 Java教程

這篇文章主要介紹了java 中MyBatis注解映射的實例詳解的相關(guān)資料,這里提供實例幫助大家理解這部分內(nèi)容,需要的朋友可以參考下

java  中MyBatis注解映射的實例詳解

1.普通映射

?
1
2
3
4
5
6
7
8
@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.結(jié)果集映射

?
1
2
3
4
5
6
7
@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.關(guān)系映射

     1),一對一

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

    2)一對多

?
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 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);
}

  Java代碼 

?
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
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);
}

 4.動態(tài)sql注解映射

provider類

?
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
62
63
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();
  }
}

 Mapper接口

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

 如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://yunlian0621.iteye.com/blog/2392181

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产婷婷高清在线观看免费 | 青草视频免费 | 香港论理午夜电影网 | 国产1区2区三区不卡 | 国产1广场舞丰满老女偷 | www.麻豆| 69日本xxxx| 亚洲高清国产品国语在线观看 | 免费在线看片网站 | 青青草国产精品免费 | 日本黄大片影院一区二区 | 亚洲国产精品久久精品成人网站 | 四虎精品免费视频 | 亚洲情欲网 | 国产一级在线免费观看 | 国产精品va在线观看手机版 | 国产草逼视频 | 美女扒开两腿露出尿口的视频 | 99免费精品| 2019韩国最新三级 | 草莓视频丝瓜 | 高清免费毛片 | 欧美精品综合一区二区三区 | 亚洲冬月枫中文字幕在线看 | 肉色欧美久久久久久久蜜桃 | a男人天堂 | 精品久久成人免费第三区 | 亚洲国产麻豆 | 午夜精品免费 | 国产99精品成人免费视频 | 天天操天天舔 | 欧美成人中文字幕在线看 | 欧美久在线观看在线观看 | xxxx性欧美极品另类 | 美女流白浆 | 国产福利视频一区二区微拍 | 精品久久日日躁夜夜躁AV | 98成人网| 欧美亚洲桃花综合 | 四虎影院地址 | 国产欧美另类久久精品91 |