一、方法一:使用注解
1、自定义sql方法
@Repository
public interface UserInfoMapper extends BaseMapper<UserInfo> {
@Select("select * from user ${ew.customSqlSegment}")
List<UserInfo> selectAll(@Param(Constants.WRAPPER) Wrapper<UserInfo> wrapper);
}
2、调用自定义sql方法
/**
* 自定义查询
*/
@Test
public void selectByMy() {
LambdaQueryWrapper<UserInfo> lambda = new LambdaQueryWrapper<UserInfo>();
lambda.like(UserInfo::getUsername,"娟")
.and(lqw->lqw.lt(UserInfo::getAge,40).or().isNotNull(UserInfo::getEmail));
List<UserInfo> userInfoList = userInfoMapper.selectAll(lambda);
userInfoList.forEach(System.out::println);
}
二、方法二:使用xml
1、配置路径
mybatis-plus.mapper-locations= com.xiao.permission_system.mapper.UserInfoMapper
2、在xml文件里写sql,去掉UserInfoMapper方法里的注解
<mapper namespace="com.xiao.permission_system.mapper.UserInfoMapper">
<select id="selectAll" resultMap="com.xiao.permission_system.entity.UserInfo">
select * from user ${ew.customSqlSegment}
</select>
</mapper>