Mybatis-plus 常用的方法(包含分页查询,模糊查询,排序......)

最简单的那些根据主键增删改查就不写了,下面介绍的是根据其他字段进行操作
JavaBean:

//课程
@TableName(value = "t_course")
@Repository
public class Course {
	@TableId(value = "course_id", type = IdType.AUTO)
	private Integer courseId;//主键id
	private String courseName;
	private String courseIntroduce;// 课程介绍
	private Integer courseCollect;// 课程收藏
	private Integer userId;
	private String courseUptime;
	private Integer coursePrice;
	private String courseType;
	private String courseCover;
	private Integer courseTop;
	private String dynamicFlag;
	
	......省略getter,setter方法
}

方法介绍:

@Service
public class CourseServiceImpl implements ICourseService {
	@Autowired
	CourseMapper courseMapper;

	//增加
	@Override
	public int AddCourse(Course course) {
		int insert = courseMapper.insert(course);
		return insert;
	}

	//根courseName删除
	@Override
	public int DeleteCourseByName(String courseName) {
		QueryWrapper<Course> wrapper = new QueryWrapper<Course>();// 相当于这里是条件
		wrapper.eq("course_name", courseName);
		int delete = courseMapper.delete(wrapper);
		return delete;
	}

	//根据userId查询(这里的userId并不是主键id)
	@Override
	public List<Course> SelectCourse(int userId) {
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userId);
		List<Course> courseList = courseMapper.selectList(wrapper);
		return courseList;
	}

	//根据courseName和userId修改其他字段
	@Override
	public int UpdateCourseTopByCourseName(String courseName, int newCourseTop, int userId) {
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		wrapper.eq("course_name", courseName);// where 条件
		wrapper.eq("user_id", userId);
		Course course = new Course(newCourseTop);// 修改的字段
		int update = courseMapper.update(course, wrapper);
		return update;
	}

	//根据courseTop和userid查询
	@Override
	public List<Course> SelectCourseTop(int courseTop,int userId) {
		System.out.println("查询置顶课程。。。。。");
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		wrapper.eq("course_top", courseTop);
		wrapper.eq("user_id", userId);
		List<Course> selectList = courseMapper.selectList(wrapper);
		return selectList;
	}

	//根据courseName进行分页查询
	@Override
	public IPage<Course> SelectLikeCourseByName(String courseName, int index,int pageSize) {
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		long count=courseMapper.selectCount(wrapper);
        IPage<Course> page = new Page<>(index, pageSize, count);
        return  courseMapper.selectPage(page, wrapper);
	}
	
	//模糊查询
	@Override
	public List<Course> SelectLikeCourseByName(int userId, String courseName) {
		System.out.println("courseName==" + courseName + "--------" + userId);
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		wrapper.eq("userId", userId);
		List<Course> courseList = courseMapper.selectList(new QueryWrapper<Course>().like("course_name", courseName));
		return courseList;
	}

	//排序orderByDesc,orderByAsc
	@Override
	public IPage<Course> SelectCourseCollect(int index,int pageSize) {
		System.out.println("按收藏数排序。。。。。。。。。。。。。。");
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("course_collect");
		long count=courseMapper.selectCount(wrapper);
        IPage<Course> page = new Page<>(index, pageSize, count);
        return  courseMapper.selectPage(page, wrapper);
	}

	//不给参数,但要查询出所有类型
	@Override
	public List<String> SelectCourseAllType() {
		List<String> cList = new ArrayList<>();
		List<Course> selectList = courseMapper.selectList(null);
		for (int i = 0; i < selectList.size(); i++) {
			cList.add(selectList.get(i).getCourseType());
		}
		return cList;
	}
}

你可能感兴趣的:(mybatis)