入参javaBean
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class UserReq implements Serializable {
private Long id;
private String username;
private String password;
private List<OrderFiled> orderFields;
}
import lombok.Data;
import java.io.Serializable;
@Data
public class OrderFiled implements Serializable {
private String orderField;
private String orderType;
}
Dao
public interface UserMapper {
List<User> getAllByOrderFiled(UserReq userReq);
}
xml
<select id="getAllByOrderFiled" parameterType="com.springboot.req.UserReq" resultMap="baseResultMap">
select id, username, password from user
<if test="orderFields !=null and orderFields.size() > 0 ">
order by
<foreach item="item" index="index" collection="orderFields" separator=",">
${item.orderField} ${item.orderType}
</foreach>
</if>
</select>
接口调用示例
@Test
public void getAllByOrderFiledTest(){
UserReq userReq = new UserReq();
List<OrderFiled> orderFileds = new ArrayList<>();
OrderFiled filed = new OrderFiled();
filed.setOrderField("username");
filed.setOrderType("desc");
orderFileds.add(filed);
filed = new OrderFiled();
filed.setOrderField("password");
filed.setOrderType("ASC");
orderFileds.add(filed);
userReq.setOrderFields(orderFileds);
List<User> users = userMapper.getAllByOrderFiled(userReq);
}