zoukankan      html  css  js  c++  java
  • MYBatis 动态SQL

    1.普通映射

    [html] view plain copy
     
    1. @Select("select * from mybatis_Student where id=#{id}")  
    2. public Student getStudent(int id);  
    3. @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
    4. public int insert(Student student);  
    5. @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
    6. public int update(Student student);  
    7. @Delete("delete from mybatis_Student where id=#{id}")  
    8. public int delete(int id);  


    2.结果集映射

    [html] view plain copy
     
    1. @Select("select * from mybatis_Student")  
    2. @Results({  
    3.     @Result(id=true,property="id",column="id"),  
    4.     @Result(property="name",column="name"),  
    5.     @Result(property="age",column="age")  
    6. })  
    7. public List<Student> getAllStudents();  


    3.关系映射

    1),一对一

    [html] view plain copy
     
    1. @Select("select * from mybatis_Student")  
    2. @Results({  
    3.     @Result(id=true,property="id",column="id"),  
    4.     @Result(property="name",column="name"),  
    5.     @Result(property="age",column="age"),  
    6.     @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
    7. })  
    8. public List<Student> getAllStudents();  

    2)一对多

    [html] view plain copy
     
    1. package com.skymr.mybatis.mappers;  
    2.   
    3. import org.apache.ibatis.annotations.Many;  
    4. import org.apache.ibatis.annotations.Result;  
    5. import org.apache.ibatis.annotations.Results;  
    6. import org.apache.ibatis.annotations.Select;  
    7.   
    8. import com.skymr.mybatis.model.Grade;  
    9.   
    10. public interface Grade2Mapper {  
    11.   
    12.     @Select("select * from mybatis_grade where id=#{id}")  
    13.     @Results({  
    14.         @Result(id=true,column="id",property="id"),  
    15.         @Result(column="grade_name",property="gradeName"),  
    16.         @Result(property="students",column="id",many=@Many(select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))  
    17.     })  
    18.     public Grade getGrade(int id);  
    19. }  
    [html] view plain copy
     
    1. package com.skymr.mybatis.mappers;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.apache.ibatis.annotations.Delete;  
    6. import org.apache.ibatis.annotations.Insert;  
    7. import org.apache.ibatis.annotations.One;  
    8. import org.apache.ibatis.annotations.Result;  
    9. import org.apache.ibatis.annotations.Results;  
    10. import org.apache.ibatis.annotations.Select;  
    11. import org.apache.ibatis.annotations.Update;  
    12.   
    13. import com.skymr.mybatis.model.Student;  
    14.   
    15. public interface Student2Mapper {  
    16.   
    17.     @Select("select * from mybatis_Student where id=#{id}")  
    18.     public Student getStudent(int id);  
    19.     @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
    20.     public int insert(Student student);  
    21.     @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
    22.     public int update(Student student);  
    23.     @Delete("delete from mybatis_Student where id=#{id}")  
    24.     public int delete(int id);  
    25.       
    26.     @Select("select * from mybatis_Student")  
    27.     @Results({  
    28.         @Result(id=true,property="id",column="id"),  
    29.         @Result(property="name",column="name"),  
    30.         @Result(property="age",column="age"),  
    31.         @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
    32.     })  
    33.     public List<Student> getAllStudents();  
    34.     @Select("select * from mybatis_Student where grade_id=#{gradeId}")  
    35.         @Results({  
    36.         @Result(id=true,property="id",column="id"),  
    37.         @Result(property="name",column="name"),  
    38.         @Result(property="age",column="age"),  
    39.         @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
    40.     })  
    41.     public List<Student> getStudentsByGradeId(int gradeId);  
    42. }  



    4.动态sql注解映射

    provider类

    [html] view plain copy
     
    1. package com.skymr.mybatis.mappers.provider;  
    2.   
    3. import java.util.Map;  
    4.   
    5. import org.apache.ibatis.jdbc.SQL;  
    6.   
    7. import com.skymr.mybatis.model.Student;  
    8.   
    9. public class StudentDynaSqlProvider {  
    10.   
    11.     public String insertStudent(final Student student){  
    12.         return new SQL(){  
    13.             {  
    14.                 INSERT_INTO("mybatis_Student");  
    15.                 if(student.getName() != null){  
    16.                     VALUES("name","#{name}");  
    17.                 }  
    18.                 if(student.getAge() > 0){  
    19.                     VALUES("age","#{age}");  
    20.                 }  
    21.             }  
    22.         }.toString();  
    23.     }  
    24.       
    25.     public String updateStudent(final Student student){  
    26.         return new SQL(){  
    27.             {  
    28.                 UPDATE("mybatis_Student");  
    29.                 if(student.getName() != null){  
    30.                     SET("name=#{name}");  
    31.                 }  
    32.                 if(student.getAge() > 0){  
    33.                     SET("age=#{age}");  
    34.                 }  
    35.                 WHERE("id=#{id}");  
    36.             }  
    37.         }.toString();  
    38.     }  
    39.       
    40.     public String getStudent(final Map<String,Object> map){  
    41.         return new SQL(){  
    42.             {  
    43.                 SELECT("*");  
    44.                 FROM("mybatis_Student");  
    45.                 if(map.containsKey("name")){  
    46.                     WHERE("name like #{name}");  
    47.                 }  
    48.                 if(map.containsKey("age")){  
    49.                     WHERE("age=#{age}");  
    50.                 }  
    51.             }  
    52.         }.toString();  
    53.     }  
    54.       
    55.     public String deleteStudent(){  
    56.         return new SQL(){  
    57.             {  
    58.                 DELETE_FROM("mybatis_Student");  
    59.                 WHERE("id=#{id}");  
    60.             }  
    61.         }.toString();  
    62.     }  
    63. }  

    Mapper接口

    [html] view plain copy
     
      1. @SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent")  
      2. public List<Student> getStudents(Map<String,Object> map);  

    link: http://blog.csdn.net/naruto_mr/article/details/48207437

  • 相关阅读:
    js 判断图片是否加载完成(使用 onload 事件)
    使用 css 的 keyframe 实现 loading 动画
    meta标签常用属性
    Chrome开发者工具 debug 调试
    ajaxForm上传文件到本地服务器(封装)
    优化jQuery选择器
    “要有足够的耐心,一点一滴地改变世界”
    Event事件的三个阶段
    css控制页面文字不能被选中user-select:none;
    webstrom打开多个项目,webstrom常用快捷键
  • 原文地址:https://www.cnblogs.com/jpit/p/7448320.html
Copyright © 2011-2022 走看看