Mybatis中example的使用
生活随笔
收集整理的這篇文章主要介紹了
Mybatis中example的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mybatis有很簡單的方式來完成sql語句的編寫
如下:
int id = 2;
ClusterExample ClusterExample = new ClusterExample();
ClusterExample.Criteria criteria = ClusterExample.createCriteria();
//增加條件,對應為where,下面這行代碼可以理解為where id = 2
criteria.andIdEqualTo(id);
//下面語句可以理解為select * from XXX where id=2
Cluster cluster = ClusterMapper.selectByPrimaryKey(id);
?
如果涉及到and的操作,比如搜索id=2而且name=admin的用戶
int id = 2;String name = admin ClusterExample ClusterExample = new ClusterExample();ClusterExample.Criteria criteria = ClusterExample.createCriteria();criteria.andIdEqualTo(id);criteria.andNameEqualTo(name);Cluster cluster = ClusterMapper.selectByPrimaryKey(id);?
如果涉及到or的操作,比如搜索id=2或者name=admin的用戶
int id = 2;String name = admin; ClusterExample clusterExample?= new ClusterExample();ClusterExample.Criteria criteria1?= ClusterExample.createCriteria();criteria1.andIdEqualTo(id);ClusterExample.Criteria criteria2?= ClusterExample.createCriteria();criteria.andNameEqualTo(name);clusterExample.or(criteria2?);?
總結
以上是生活随笔為你收集整理的Mybatis中example的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【剑指offer】面试题60:n个骰子的
- 下一篇: 【剑指offer】面试题31:栈的压入,