mybatis中的mapper接口文件以及example类的实例函数以及详解 转载 2016年03月18日 10:28:46 19170213 [java] view plain copy ##Exa

来源:互联网 发布:出版印刷设计软件 编辑:程序博客网 时间:2024/06/16 09:44

mybatis中的mapper接口文件以及example类的实例函数以及详解

[java] view plain copy
  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("joe@163.com");    
  66. ##Mapper.insert(user);    
  67. 相当于:insert into user(ID,username,password,email) values    
  68.     
  69. (101,'test','123','joe@163.com');    
  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("joe@163.com");    
  78. ##Mapper.updateByPrimaryKey(user);    
  79. 相当于:update user set username='joe',password='joe',email='joe@163.com'    
  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'    
阅读全文
0 0
原创粉丝点击