Spring整理

TestController

@Controller
@RequestMapping("/test")
public class TestController {

	@Autowired
	private TestService testService;

	@RequestMapping("/init")
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = null;
		
		try{
			resultMap = new HashMap<String, Object>();

			//查询实体类
			TestTable o = testService.doSomeBusinessStuff("1");
			System.out.println(o.getId());
			
			PageInfo page = new PageInfo();
			page.setCurrentPage(1);
			
			//分页
			List<TestTable> oList = testService.getMemberListPage(page);
			System.out.println(oList.size());
			resultMap.put("list", oList);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		return new ModelAndView("/sample", resultMap);
	}
	

	@RequestMapping("/list")
	@ResponseBody
	public Object list(@RequestParam String data,@RequestParam String pager, HttpServletRequest request,HttpServletResponse response) {
		JSONObject jData=JSONObject.fromObject(data);//查询条件
		JSONObject jPager=JSONObject.fromObject(pager);//分页条件
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		Pager p = new Pager();
		int total=99;//从service层获取
		p.setTotal(total);
		p.setPageNo(jPager.optInt("pageNo",1));
		p.setPageSize(jPager.optInt("pageSize",10));	
		int len=p.getPageSize();
		if(len>p.getTotal()-(p.getPageNo()-1)*p.getPageSize()){
			len=p.getTotal()-(p.getPageNo()-1)*p.getPageSize();
		}
		for(int i=(p.getPageNo()-1)*p.getPageSize();i<(p.getPageNo()-1)*p.getPageSize()+len;i++){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", i);
			map.put("key1", i*2);
			map.put("key2", i*3);
			//自定义标签内容
			map.put("key3", "<a name=\"sp1\" href=\"#\">编辑</a>&nbsp;<a name=\"sp2\" href=\"#\">数据监控</a>&nbsp;<a name=\"sp3\" href=\"#\">注销</a>&nbsp;");
			list.add(map);
		}
		p.setResultList(list);
		return p;
	}
}

TestService

@Transactional
public interface TestService {

	TestTable doSomeBusinessStuff(String userId);
	
	//分页
	List<TestTable> getMemberListPage(@Param("page") PageInfo page);
}

TestServiceImpl

@Service
public class TestServiceImpl implements TestService {

	// DAO
	private UserMapper userMapper;

	@Autowired
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

        @Autowired
	public TestTable doSomeBusinessStuff(String userId) {
		return this.userMapper.getUser(userId);
	}

	@Override
	public List<TestTable> getMemberListPage(PageInfo page) {
		return this.userMapper.getMemberListPage(page);
	}

}

相关推荐