尽管我们创建不同的对象我们也不能连续使用
多次使用的时候在循环中创建,使其变成循环中的局部变量,这样就可以了
- 错误示范
public void sendEmail() throws Exception{
Example example = new Example(Record.class);
Example.Criteria criteria = example.createCriteria();
criteria.andIsNull("outtime");
//这两行代码在这里不行,不能让他重复利用,应该使用一次,创建一个
Example example2 = new Example(Member.class);
Example.Criteria criteria2 = example2.createCriteria();
List<Record> records = recordMapper.selectByExample(example);
for (Record record:records) {
String openid = record.getOpenid();
//成员的条件
criteria2.andEqualTo("openid",openid);
Member member = memberMapper.selectOneByExample(example2);
//对member进行的操作
}
}
- 正确示范
public void sendEmail() throws Exception{
Example example = new Example(Record.class);
Example.Criteria criteria = example.createCriteria();
criteria.andIsNull("outtime");
List<Record> records = recordMapper.selectByExample(example);
for (Record record:records) {
//把这两行代码放这里,使用一次,创建一个
Example example2 = new Example(Member.class);
Example.Criteria criteria2 = example2.createCriteria();
String openid = record.getOpenid();
//成员的条件
criteria2.andEqualTo("openid",openid);
Member member = memberMapper.selectOneByExample(example2);
//对member进行的操作
}
}