1 基本
1.1 配置maven包
<!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Spring test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency>
基本引入
2 Dao
2.1 目录结构
2.2 常规
public class PageHelperTest { private ApplicationContext applicationContext; @Before public void before(){ applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); } @Test public void testPageHelper() throws Exception { //获得Mapper的代理对象 TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class); //设置分页信息 PageHelper.startPage(1, 30); //执行查询 TbItemExample example = new TbItemExample(); List<TbItem> list = itemMapper.selectByExample(example); //取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<>(list); System.out.println(pageInfo.getTotal()); System.out.println(pageInfo.getPages()); System.out.println(pageInfo.getPageNum()); System.out.println(pageInfo.getPageSize()); } }
2.3 注解
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/applicationContext-*.xml" }) public class ItemServiceImplTest { @Autowired protected TbItemMapper itemMapper; @Test public void getItemById() throws Exception { TbItem item = itemMapper.selectByPrimaryKey(830972L); System.out.println(item); } }