Mybatis-plus的Mapper.xml添加${ew.customSqlSegment}

首先在你的Mapper添加你的自定义查询方法

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.wuxianggujun.wuxiangblog.vo.SearchBlogVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BlogMapper extends BaseMapper<SearchBlogVo> {

    //查询文章
    List<SearchBlogVo> list(@Param(Constants.WRAPPER) QueryWrapper<SearchBlogVo> ew);

}

注意一定要添加@Param(Constants.WRAPPER) 不然xml文件里面解析不了会报错

然后使用MyBatisX插件在xml中新建Mybatis-plus的Mapper.xml添加${ew.customSqlSegment}_第1张图片

具体步骤

首先找到红色波浪线的方法
Mybatis-plus的Mapper.xml添加${ew.customSqlSegment}_第2张图片
右键选择显示上下文操作或者直接使用快捷键ALT+Enter
Mybatis-plus的Mapper.xml添加${ew.customSqlSegment}_第3张图片
选择Generate statement

Mybatis-plus的Mapper.xml添加${ew.customSqlSegment}_第4张图片
将会在你的Mapper.xml文件中,新建如下配置

 <select id="list" resultType="com.wuxianggujun.wuxiangblog.vo.SearchBlogVo">select>
属性 理解
id 就是你在Mapper的接口中所写的方法名
resultType 返回的实体类,建议补充完整不然报错,或者你可以在配置文件中先定义好来

将你的 ${ew.customSqlSegment} 添加在你语句的最后面就可以了,如下

 <select id="list" resultType="com.wuxianggujun.wuxiangblog.vo.SearchBlogVo">
        select tb.id ,
        tb.create_time,
        tb.recommend ,
        tb.title,
        tt.name
        from wuxiangblog.t_blog
        tb left join
        wuxiangblog.t_type tt on tb.type_id =tt.id
        ${ew.customSqlSegment}
    select>

这是我的调用语句

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuxianggujun.wuxiangblog.mapper.BlogMapper;
import com.wuxianggujun.wuxiangblog.service.BlogService;
import com.wuxianggujun.wuxiangblog.vo.SearchBlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, SearchBlogVo> implements BlogService {
    private BlogMapper blogDao;

    @Autowired
    public void setBlogDao(BlogMapper blogDao) {
        this.blogDao = blogDao;
    }

    @Override
    public List<SearchBlogVo> searchBlog(String title) {

        QueryWrapper<SearchBlogVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SearchBlogVo::getTitle, "文章");

        List<SearchBlogVo> searchBlogVos = blogDao.list(queryWrapper);

//
//        LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(User.class).eq(User::getUsername, title);
//        //先查询用户数据
//        User user = userMapper.selectOne(wrapper);

        return searchBlogVos;
    }
}

我的Service

import com.baomidou.mybatisplus.extension.service.IService;
import com.wuxianggujun.wuxiangblog.vo.SearchBlogVo;

import java.util.List;

public interface BlogService extends IService<SearchBlogVo> {

    List<SearchBlogVo> searchBlog(String title);


}

我的控制层

  @PostMapping("/search")
    public Result search(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, @RequestParam String title) {
        List<SearchBlogVo> searchBlogs = blogService.searchBlog(title);
        return ResultGenerator.getSuccessResult(searchBlogs);
    }

然后我们再运行程序之后就可以在控制台中看到SQL语句,

select tb.id , tb.create_time, tb.recommend , tb.title, tt.name from wuxiangblog.t_blog tb left join wuxiangblog.t_type tt on tb.type_id =tt.id WHERE (title = ?)

图片
Mybatis-plus的Mapper.xml添加${ew.customSqlSegment}_第5张图片

你可能感兴趣的:(SpringBoot,mybatis-plus,mybatis,xml,java)