欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 中MyBatis注解映射的实例详解

程序员文章站 2024-02-25 15:47:03
java  中mybatis注解映射的实例详解 1.普通映射  @select("select * from mybatis_stude...

java  中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.关系映射

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

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

  java代码 

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.动态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(); 
  } 
} 

 mapper接口

@selectprovider(type=studentdynasqlprovider.class,method="getstudent") 
public list<student> getstudents(map<string,object> map); 
 

 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!