Mybatis-Plus Query Wrapper使用

1、配置

mybatis-plus:

                  mapper-location:classpath:/mapper/**.xml

2、具体实现

(1) 使用注解实现

Mybatis-plus:

@SELECT("select * from Student ${ew.customSqlSegment}
List<Student> selectByMyWrapepr(@Param(constants.WRAPPER) Wrapepr<Student> studentWrapper);

Mybatis:

@Select("select * from student where name = #{name}")
List<Student> selectByName (@Param("name") String name);

(2) 使用xml文件实现

mapper:

//Mybatis-plus
List<Student> selectByMyWrpper(@Param(Constants.WRAPPER) Wrapper<Student> userWrapper);

//Mybatis
List<Student> selectByName(@Param("name") String name)

**.xml:

//Mybatis-plus
<select id = "selectByWrapper" resultType = "com.example.demo.model.Student">
Select * From Student ${ew.customSqlSegment}
</select>

//Mybatis
<select id = "selectByName" resultType = "com.example.demo.model.Student">
Select * From Student where name = #{name}
</select>

分页查询:

@Override
public Page<StudentResp> queryStudent(StudentReq req){
    //获取分页信息
    PageVo pageVo = new PageVo();
    pageVo.setPageSize(req.getPageSize());
    pageVo.setPageNum(req.getPageNum());
    Page<StudentResp> page = this.getPage(pageVo);

    QueryWrapper wrapper = new QueryWrapper();

    if(StringUtils.isNotEmpty(req.getAId())){
       wrapper.eq(ID,req.getAId());
    }

    if(StringUtils.isNotEmpty(req.getUserId())){
       wrapper.eq(NAME,req.getUserName());
    }

    return accountMapper.selectStudentList(page,wrapper);
}

//StudentMapper
Page<StudentResp> selectStudentList(Page<StudentResp> page,@Param(Constants.WRAPPER) Wrapper wrapper);

//StudentMapper.xml
<select id = "selectStudentList" resultType = "com.example.demo.model.StudentResp" parameterType = "com.example.demo.model.StudentReq">

select * from user${ew.customSqlSegment}
</select>

 

Update:

//mapper:
int updateByMyWrapper(@Param(Constants.WRAPPER) Wrapper<Student> studentWrapper,@Param("student") Student student);

//**.xml:
<update id = "updateByMyWrapper">
Update student SET email = #{student.email}${ew.customSqlSegment}
</update>

猜你喜欢

转载自www.cnblogs.com/xudongsheng0721/p/12067550.html