大家好,又见面了,我是你们的朋友全栈君。
@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
package com.urthink.upfs.springbootmybatis;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.urthink.upfs.springbootmybatis.entity.User;
import com.urthink.upfs.springbootmybatis.mapper.UserMapper;
import com.urthink.upfs.springbootmybatis.servcie.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* mybatis测试类
* https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
* @author zhao
* @date 2019.1.21
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
public class MybatisTest {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Autowired
private UserService userService;
@Autowired
private UserMapper userMapper;
@Test
public void sqlSessionTemplateTest() {
User user = sqlSessionTemplate.selectOne("com.urthink.upfs.springbootmybatis.mapper.UserMapper.selectByPrimaryKey",1000);
System.out.println(user.getUserName());
}
@Test
public void selectTest() {
User user = userService.selectByPrimaryKey(1000);
System.out.println(user.getUserName());
}
@Test
public void insertTest() {
User user = new User();
user.setUserName("test");
user.setPassword("test"); //需加密
user.setEnable("1");
user.setCreateBy(1);
user.setCreateTime(new Date());
int r = userService.insert(user);
System.out.println(r);
}
@Test
public void pageTest1() {
//第一种,RowBounds方式的调用,需要把rowBoundsWithCount设置为true
List<User> userList = sqlSessionTemplate.selectList("com.urthink.upfs.springbootmybatis.mapper.UserMapper.getPageList", null, new RowBounds(0, 10));
//用PageInfo对结果进行包装
PageInfo pageInfo = new PageInfo(userList);
//PageInfo包含了非常全面的分页属性
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("每页的数量:" + pageInfo.getPageSize());
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("结果集:" + pageInfo.getList());
}
@Test
public void pageTest2() {
//第二种,Mapper接口方式的调用,推荐这种使用方式。
PageHelper.startPage(1, 10);
//排序
PageHelper.orderBy("create_time asc, id asc");
List<User> list = userMapper.getPageList(null);
//用PageInfo对结果进行包装
PageInfo pageInfo = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("每页的数量:" + pageInfo.getPageSize());
System.out.println("当前页的数量:" + pageInfo.getSize());
System.out.println("当前页面第一个元素在数据库中的行号:" + pageInfo.getStartRow());
System.out.println("当前页面最后一个元素在数据库中的行号:" + pageInfo.getEndRow());
System.out.println("总页数:" + pageInfo.getPages());
System.out.println("前一页:" + pageInfo.getPrePage());
System.out.println("下一页:" + pageInfo.getNextPage());
System.out.println("是否为第一页:" + pageInfo.isIsFirstPage());
System.out.println("是否为最后一页:" + pageInfo.isIsLastPage());
System.out.println("是否有前一页:" + pageInfo.isHasPreviousPage());
System.out.println("是否有下一页:" + pageInfo.isHasNextPage());
System.out.println("导航页码数:" + pageInfo.getNavigatePages());
System.out.println("所有导航页号:" + pageInfo.getNavigatepageNums());
System.out.println("导航条上的第一页:" + pageInfo.getNavigateFirstPage());
System.out.println("导航条上的最后一页:" + pageInfo.getNavigateLastPage());
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("结果集:" + pageInfo.getList());
System.out.println(list.size());
}
@Test
public void pageTest3() {
Map map = new HashMap<String, Object>();
map.put("pageNum", 1);
map.put("pageSize", 10);
map.put("orderBy", "create_time asc, id asc");
map.put("userName", "test"); //XML里要的查询条件
PageInfo pageInfo = userService.getPageInfo(map);
//PageInfo包含了非常全面的分页属性
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("每页的数量:" + pageInfo.getPageSize());
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("结果集:" + pageInfo.getList());
}
@Test
public void pageTest4() {
Map map = new HashMap<String, Object>();
map.put("pageNum", 1);
map.put("pageSize", 10);
map.put("orderBy", "create_time asc, id asc");
map.put("userName", "test"); //XML里要的查询条件
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
if(StringUtils.isNotEmpty((String)map.get("id"))) {
criteria.andEqualTo("id", map.get("id"));
}
if(StringUtils.isNotEmpty((String)map.get("userName"))) {
criteria.andLike("userName", "%"+(String)map.get("userName")+"%");
}
map.put("example", example);
PageInfo pageInfo = userService.selectPageInfo(map);
//PageInfo包含了非常全面的分页属性
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("每页的数量:" + pageInfo.getPageSize());
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("结果集:" + pageInfo.getList());
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/145080.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...