编写查询图书
service层
@Service
public class BookService {
@Autowired
BookMapper bookMapper;
public List queryBooks(){
return bookMapper.selectByExample(new BookExample());
}
}
编写测试
@Test
public void test03(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService mapper = ioc.getBean(BookService.class);
List books = mapper.queryBooks();
for (Book book : books) {
System.out.println(book.toString());
}
}
向数据库添加数据
controller层
@Controller
@SessionAttributes(value = {"pageInfo"},types = {PageInfo.class})
public class BookController {
@Autowired
BookService bookService;
@RequestMapping("/QueryBook")
public String queryBooks(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
Model model){
PageHelper.startPage(pn,8);
List books = bookService.queryBooks();
PageInfo page = new PageInfo(books, 8);
model.addAttribute("pageInfo",page);
return "bookList";
}
@RequestMapping("/index")
public String toIndex(){
return "index";
}
}
页面展示
增加分页条
使用pagehelper,引入依赖
出错
Cannot find class: com.github.pagehelper.PageInterceptor
原因:没有将新包加入到lib中
解决:项目结构将包加载到lib
分页条效果
优化分页条
增强分页条显示的一些逻辑
当前第${pageInfo.pageNum}页 总共${pageInfo.pages}页
阶段测试问题:发现登录过后跳转页面时导航栏用户显示不登录
问题所在:有关用户登录判断的属性hasUser是null,且发现pageInfo也不能在bookList.jsp页面以外使用
解决:由于controller中写的Model,向Model存入数据是request级别的,故两次请求当然无效。给controller加上@SessionAttributes(value = {"pageInfo"},types = {PageInfo.class})使得该属性可以在session中使用
@SessionAttributes和@SessionAttribute 的区别,前者是存,用在类上,后者是取,用在方法参数里



