mybatis中的mapper介面檔案以及example類的執行個體函數以及詳解

來源:互聯網
上載者:User

標籤:刪除   去除   art   select   distinct   排列   app   exception   primary   

    1. ##Example example = new ##Example();    
    2. example.setOrderByClause("欄位名 ASC"); //升序排列,desc為降序排列。    
    3. example.setDistinct(false)//去除重複,boolean型,true為選擇不重複的記錄。    
    4. Criteria criteria = new Example().createCriteria();    
    5. is null;is not null;    
    6. equal to(value);not equal to(value);    
    7. GreaterThan(value);GreaterThanOrEqualTo(value);    
    8. LessThan(value); LessThanOrEqualTo(value);    
    9. in(item,item,item,...);not in(item,item,item,...);    
    10. like("%"+value+"%");not like("%"+value+"%");    
    11. Between(value1,value2);not between(value1,value2)    
    12.     
    13.      
    14.     
    15. mybatis中mapper的執行個體函數:    
    16. int countByExample(UserExample example) thorws SQLException:按條件計數。    
    17. int deleteByPrimaryKey(Integer id) thorws SQLException:按主鍵刪除。    
    18. int deleteByExample(UserExample example) thorws SQLException:按條件刪除。    
    19. String/Integer insert(User record) thorws SQLException:插入(傳回值為id值)    
    20. User selectByPrimaryKey(Integer id) thorws SQLException:按主鍵查詢。    
    21. List<?>selectByExample(UserExample example) thorws SQLException:按條件查詢    
    22. List<?>selectByExampleWithBLOGs(UserExample example) thorws SQLException:按    
    23.     
    24. 條件查詢(包括BLOB欄位)。只有當資料表中的欄位類型有為二進位的才會產生。    
    25. int updateByPrimaryKey(User record) thorws SQLException:按主鍵更新    
    26. int updateByPrimaryKeySelective(User record) thorws SQLException:按主鍵更新    
    27.     
    28.  值不為null的欄位    
    29. int updateByExample(User record, UserExample example) thorws SQLException:     
    30.     
    31. 按條件更新    
    32. int updateByExampleSelective(User record, UserExample example) thorws      
    33.     
    34. SQLException:按條件更新值不為null的欄位    
    35.     
    36. mybatis中mapper的執行個體函數詳解:    
    37. ① selectByPrimaryKey()    
    38.     
    39. User user = ##Mapper.selectByPrimaryKey(100); 相當於select * from user where    
    40.     
    41. id = 100    
    42.     
    43. ② selectByExample() 和 selectByExampleWithBLOGs()    
    44.     
    45. UserExample example = new UserExample();    
    46. Criteria criteria = example.createCriteria();    
    47. criteria.andUsernameEqualTo("joe");    
    48. criteria.andUsernameIsNull();    
    49. example.setOrderByClause("username asc,email desc");    
    50. List<?>list = ##Mapper.selectByExample(example);    
    51. 相當於:select * from user where username = ‘joe‘ and username is null order    
    52.     
    53. by username asc,email desc    
    54.     
    55. 註:在iBator 產生的檔案UserExample.java中包含一個static 的內部類 Criteria ,    
    56.     
    57. 在Criteria中有很多方法,主要是定義SQL 語句where後的查詢條件。    
    58.     
    59. ③ insert()    
    60.     
    61. User user = new User();    
    62. user.setId(101);    
    63. user.setUsername("test");    
    64. user.setPassword("123")    
    65. user.setEmail("[email protected]");    
    66. ##Mapper.insert(user);    
    67. 相當於:insert into user(ID,username,password,email) values    
    68.     
    69. (101,‘test‘,‘123‘,‘[email protected]‘);    
    70.     
    71.  ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()    
    72.     
    73. User user =new User();    
    74. user.setId(101);    
    75. user.setUsername("joe");    
    76. user.setPassword("joe");    
    77. user.setEmail("[email protected]");    
    78. ##Mapper.updateByPrimaryKey(user);    
    79. 相當於:update user set username=‘joe‘,password=‘joe‘,email=‘[email protected]‘    
    80.     
    81. where id=101    
    82.     
    83. User user = new User();    
    84. user.setId(101);    
    85. user.setPassword("joe");    
    86. ##Mapper.updateByPrimaryKeySelective(user);    
    87. 相當於:update user set password=‘joe‘ where id=101    
    88.     
    89. ⑤ updateByExample() 和 updateByExampleSelective()    
    90.     
    91. UserExample example = new UserExample();    
    92. Criteria criteria = example.createCriteria();    
    93. criteria.andUsernameEqualTo("joe");    
    94. User user = new User();    
    95. user.setPassword("123");    
    96. ##Mapper.updateByPrimaryKeySelective(user,example);    
    97. 相當於:update user set password=‘123‘ where username=‘joe‘    
    98.     
    99. ⑥ deleteByPrimaryKey()    
    100.     
    101. ##Mapper.deleteByPrimaryKey(101);  相當於:delete from user where id=101    
    102.     
    103. ⑦ deleteByExample()    
    104.     
    105. UserExample example = new UserExample();    
    106. Criteria criteria = example.createCriteria();    
    107. criteria.andUsernameEqualTo("joe");    
    108. ##Mapper.deleteByExample(example);    
    109. 相當於:delete from user where username=‘joe‘    
    110.     
    111. ⑧ countByExample()    
    112.     
    113. UserExample example = new UserExample();    
    114. Criteria criteria = example.createCriteria();    
    115. criteria.andUsernameEqualTo("joe");    
    116. int count = ##Mapper.countByExample(example);    
    117. 相當於:select count(*) from user where username=‘joe‘   

mybatis中的mapper介面檔案以及example類的執行個體函數以及詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.