fix:在sql中使用or

上级 f17e2bb1
...@@ -6,15 +6,12 @@ import com.kwan.springbootkwan.service.IUserService; ...@@ -6,15 +6,12 @@ import com.kwan.springbootkwan.service.IUserService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError; import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -67,4 +64,13 @@ public class UserController { ...@@ -67,4 +64,13 @@ public class UserController {
public User getUserByName(@RequestParam String sex) { public User getUserByName(@RequestParam String sex) {
return userService.getUserByName(sex); return userService.getUserByName(sex);
} }
@ApiOperation(value = "or拼接", notes = "or拼接")
@RequestMapping(value = "/getUserByOr", method = RequestMethod.GET)
public List<User> getUserByOr() {
return userService.getUserByOr();
}
} }
\ No newline at end of file
package com.kwan.springbootkwan.entity.query;
import lombok.Data;
@Data
public class QueryInfo {
private String invQtyA;
private String invQtyB;
private String invQtyC;
private String invQtyD;
}
...@@ -3,6 +3,9 @@ package com.kwan.springbootkwan.mapper; ...@@ -3,6 +3,9 @@ package com.kwan.springbootkwan.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.entity.User;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
...@@ -15,6 +18,20 @@ import org.apache.ibatis.annotations.Mapper; ...@@ -15,6 +18,20 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface UserMapper extends BaseMapper<User> { public interface UserMapper extends BaseMapper<User> {
/**
* 通过性别获取user
*
* @param sex
* @return
*/
User getUserByName(String sex); User getUserByName(String sex);
/**
* 通过id获取user
*
* @param invFlag
* @return
*/
List<User> getUserByOr(@Param("invFlag") List<Integer> invFlag);
} }
...@@ -37,4 +37,11 @@ public interface IUserService extends IService<User> { ...@@ -37,4 +37,11 @@ public interface IUserService extends IService<User> {
*/ */
User saveUser(User user); User saveUser(User user);
/**
* 使用or拼接数据
*
* @return
*/
List<User> getUserByOr();
} }
...@@ -11,6 +11,7 @@ import io.seata.spring.annotation.GlobalTransactional; ...@@ -11,6 +11,7 @@ import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
...@@ -53,4 +54,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU ...@@ -53,4 +54,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
personService.savePerson(person); personService.savePerson(person);
return user; return user;
} }
@Override
public List<User> getUserByOr() {
List<Integer> invFlag = new ArrayList<>();
invFlag.add(1);
invFlag.add(2);
invFlag.add(3);
invFlag.add(4);
return userMapper.getUserByOr(invFlag);
}
} }
\ No newline at end of file
...@@ -14,4 +14,25 @@ ...@@ -14,4 +14,25 @@
FROM user FROM user
WHERE sex = #{sex} WHERE sex = #{sex}
</select> </select>
<select id="getUserByOr" resultType="com.kwan.springbootkwan.entity.User">
SELECT *
FROM user
WHERE 1 = 1
<if test="invFlag != null and invFlag.size>0">
<foreach collection="invFlag" item="flag" open="AND (" close=")" separator="or">
<if test="flag==1">
id &lt;= 1
</if>
<if test="flag==2">
id &gt;= 2 and id &lt; 4
</if>
<if test="flag==3">
id &gt;= 5 and id &lt; 6
</if>
<if test="flag==4">
id &gt;= 8 and id &lt; 9
</if>
</foreach>
</if>
</select>
</mapper> </mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册