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