实现按id递增顺序依次读取数据

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:config/spring-common.xml")
public class UserTest {
	@Resource
	private UserService userService;
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private static final String query_sql = "select * from user where access = 0 order by ID limit 20";
	
	
	@Test
	public void testFindAll(){
	
		/*List> list = jdbcTemplate.queryForList(query_sql);
		System.out.println(Thread.currentThread().getName()+":"+list.size());
		
		for(Map map : list) {
			int user = (Integer) map.get("id");
			System.out.println(user);
		}*/
		while(true)
		{
			List> list = jdbcTemplate.queryForList(query_sql);
			System.out.println(Thread.currentThread().getName()+":"+list.size());
			if(null == list || list.isEmpty()) {
				break;
			}
			for(Map map : list) {
				int id = (Integer) map.get("ID");
				System.out.println(id);
				try {

					// 锁定
					if(!lock(id)) {
						System.out.println("cant lock the execute plan, " + id);
						continue;
					}
				} catch(Exception e) {
					System.out.println("error at process " + id);
				}
			}
			/*try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
		}
		
	}
	
	private static final String modify_lock_sql = "update user set access = 1 where access = 0 and ID = ?";
    //可实现按id递增每次取出20条数据
	private boolean lock(int id) {
		int result = jdbcTemplate.update(modify_lock_sql, id);
		if(1 == result) {
			return true;
		}
		return false;
	}
	
	
	
}
	
		
		
		
		
	
	
	  
	        
	  
	
	
		
		
		
		
	
	
	
	
	
		
		 
	 


你可能感兴趣的:(实现按id递增顺序依次读取数据)