µ±Ç°Î»Öà Ö÷Ò³ > ÍøÕ¾¼¼Êõ > ´úÂëÀà >

    dz̸ΪʲôҪʹÓÃmybatisµÄ@param

    À¸Ä¿£º´úÂëÀà ʱ¼ä£º2019-10-13 18:04

    ÆðÒò

    ÎÒÃÇÏÈÀ´¿´Ò»¸ö±¨´í

    ±¨´íºÜ¼òµ¥£¬²ÎÊý start ûÕÒµ½¡£

    ÎÒÊÇÔÚʵÏÖÒ»¸ö API ½Ó¿Úʱ·¢ÏÖÁËÒ»¸öÎÊÌ⣬µ±ÎÒ²»Ê¹Óà @Param ±êǩʱ£¬mybatis ÊDz»ÈÏʶÄĸö²ÎÊý½ÐʲôÃû×ֵģ¬¾¡¹ÜÎÒ¶¨ÒåÁË (long start,long end) ËüÈÔÈ»²»ÈÏʶ¡£ÔÚÕâ¸ö½Ó¿ÚÉÏ£¬ÎÒÏ£Íû¸ù¾ÝÇ°¶Ë´«À´µÄ²ÎÊý£¬²éÕÒÖ¸¶¨·¶Î§µÄÊý¾Ý£¬ÀýÈ磺ÎÒÏëËÑË÷µÚ¶þÒ³µÄÊý¾Ý£¬¼ÙÉèÒ»Ò³20¸ö£¬ÄÇôËÑË÷µÄ·¶Î§¾ÍÊÇ21-40£¬ÓÚÊǾͻáµ÷ÓýӿÚÖÐµÄ getTypeListByRange À´²éÕÒ¶ÔÓ¦ mapper µÄ SQL Óï¾ä¡£

    public interface TypeDao {
     Type getTypeByid(long id);
     List<Type> getTypeListAll();
     List<Type> getTypeListByRange(long start,long end);
    }
    
    

    ½âÊÍ @Param

    org.apache.ibatis.annotations.Param µ±Ó³ÉäÆ÷·½·¨ÐèÒª¶à¸ö²ÎÊýʱ£¬Õâ¸ö×¢½â¿ÉÒÔ±»ÓÃÓÚ£º¸øÓ³ÉäÆ÷·½·¨ÖеÄÿ¸ö²ÎÊýÀ´È¡Ò»¸öÃû×Ö¡£·ñÔò£¬¶à²ÎÊý½«»áÒÔËüÃǵÄ˳ÐòλÖúÍSQLÓï¾äÖеıí´ïʽ½øÐÐÓ³É䣬ÕâÊÇĬÈϵġ£
    Óï·¨ÒªÇó£ºÈôʹÓÃ@Param(¡°id¡±)£¬ÔòSQLÖвÎÊýÓ¦¸Ã±»ÃüÃûΪ£º#{id}¡£

    ʹÓÃ

    Dao ²ã

    import org.apache.ibatis.annotations.Param;
    
    import com.caeser.upmovie.entity.Type;
    
    public interface TypeDao {
     Type getTypeByid(long id);
     List<Type> getTypeListAll();
     List<Type> getTypeListByRange(@Param("start")long start,@Param("end")long end);
    }
    
    

    Mapper

    <select   resultType="com.caeser.upmovie.entity.Type">
     SELECT
     ID,
     NAME,
     CREATE_TIME,
     UPDATE_TIME
     FROM
     upm_type
     LIMIT 
      #{start},#{end};
     </select>
    
    

    µ¥Ôª²âÊÔ

    public class TypeTest extends BaseTest{
     @Autowired
     private TypeDao typeDao;
     
     @Test
     public void testDao(){
     List<Type> typeList1=typeDao.getTypeListByRange(1, 4);
     for(int i=0;i<typeList1.size();i++){
      System.out.println(typeList1.get(i).getName());
     }
     }
    }
    
    

    ½á¹û

    ×ܽá

    µ± Dao ²ã´«µÝ²ÎÊýΪ¶à¸ö²ÎÊýʱ£¬ÎªÁ˹淶£¬±ØÐëʹÓà @Param ¸ø²ÎÊýÃüÃû¡£ÕâÀïÐèҪעÒ⣬ʹÓõÄÊÇ mybatis µÄ param À´ÃüÃû¡£

    ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒµÄѧϰÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³ÖIIS7Õ¾³¤Ö®¼Ò¡£