// 练习2: 查询id包含 1,2,6,8 并且 pname 不为null, 且是上架状态 的商品信息 // selectList(wrapper)用LambdaQueryWrapper怎么写
时间: 2024-04-10 20:28:08 浏览: 76
您可以使用LambdaQueryWrapper的链式调用来编写查询条件。下面是一个示例代码,展示如何查询id包含1、2、6、8,并且pname不为null且为上架状态的商品信息:
```java
LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
wrapper.in(Goods::getId, Arrays.asList(1, 2, 6, 8))
.isNotNull(Goods::getPname)
.eq(Goods::getStatus, "上架");
List<Goods> goodsList = goodsMapper.selectList(wrapper);
```
在这个示例中,我们通过LambdaQueryWrapper的`in`方法来指定id包含1、2、6、8,使用`isNotNull`方法来确保pname不为null,并通过`eq`方法来匹配状态为"上架"的商品。最后,使用`selectList`方法执行查询并将结果存储在`goodsList`中。
请注意,示例中的`Goods`是您的商品实体类,您需要将其替换为您实际使用的实体类。另外,该示例假设您已经通过注入或其他方式获取了`goodsMapper`对象,用于执行数据库操作。
相关问题
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; class Person { public: Person(char *vn, int vage, float vh, float vw); Person(const Person &vref); void Print(); ~Person(); private: char *pName;//姓名 int age;//年龄 float height;//身高 float weight;//体重 }; //构造函数 Person::Person(char *vn, int vage, float vh, float vw) { age = vage; height = vh; weight = vw; strcpy(pName, vn); cout << age << endl; cout << height << endl; cout << weight << endl; cout << pName << endl; } //拷贝构造函数 Person::Person(const Person &vref) { int len = strlen(vref.pName); pName = new char[len]; age = vref.age; height = vref.height; weight = vref.weight; strcpy(pName, vref.pName); } //输出函数 void Person::Print() { cout << age << endl; cout << height << endl; cout << weight << endl; cout << pName << endl; } Person::~Person() { if (!pName == NULL) { delete[]pName; pName = NULL; } } int main() { string A; double B, C, D; cin >> A >> B >> C >> D; Person A1(A, B, C, D); }
这段代码有一些问题:
1. 在构造函数中,没有为 pName 分配内存,只是复制了指针,这可能导致程序崩溃。
2. 在拷贝构造函数中,同样没有为 pName 分配内存,只是复制了指针,这也可能导致程序崩溃。
3. 在析构函数中,应该检查指针是否为空,如果不为空,才进行释放操作。
4. 在 main 函数中,使用了一个 string 类型的变量 A,但是在 Person 类的构造函数中,要求传入的是 char* 类型的指针,这会导致编译错误。
5. 在 main 函数中,使用了一个 double 类型的变量 B,但是在 Person 类的构造函数中,要求传入的是 int 类型的年龄,这也会导致编译错误。
6. 在 main 函数中,没有调用 Person 类的输出函数,也就是没有输出对象的信息。
下面是修改后的代码:
```c++
#include <iostream>
#include <string.h>
using namespace std;
class Person {
public:
Person(char *vn, int vage, float vh, float vw);
Person(const Person &vref);
void Print();
~Person();
private:
char *pName;//姓名
int age;//年龄
float height;//身高
float weight;//体重
};
//构造函数
Person::Person(char *vn, int vage, float vh, float vw) {
age = vage;
height = vh;
weight = vw;
pName = new char[strlen(vn) + 1];
strcpy(pName, vn);
cout << age << endl;
cout << height << endl;
cout << weight << endl;
cout << pName << endl;
}
//拷贝构造函数
Person::Person(const Person &vref) {
int len = strlen(vref.pName);
pName = new char[len + 1];
age = vref.age;
height = vref.height;
weight = vref.weight;
strcpy(pName, vref.pName);
}
//输出函数
void Person::Print() {
cout << age << endl;
cout << height << endl;
cout << weight << endl;
cout << pName << endl;
}
Person::~Person() {
if (pName != NULL) {
delete[]pName;
pName = NULL;
}
}
int main() {
char A[100];
int B;
float C, D;
cin >> A >> B >> C >> D;
Person A1(A, B, C, D);
A1.Print();
return 0;
}
```
在修改后的代码中,我们为 pName 分配了内存,并在析构函数中进行了释放。同时,我们也将 main 函数中的输入类型和 Person 类的构造函数要求的类型进行了对应。最后,我们调用了 Person 类的输出函数,输出了对象的信息。
#include <iostream> #include <string> #include <vector> using namespace std; class Student { private: int id; //学号 string name; //姓名 float score; //个人成绩 public: //构造函数 Student(int id, string name, float score) { this->id = id; this->name = name; this->score = score; } //获取学号 int getId() { return id; } //获取姓名 string getName() { return name; } //获取成绩 float getScore() { return score; } }; class Project { private: int pId; //项目编号 string pName; //项目名称 Student leader; //组长 Student members[3]; //小组成员 float pScore; //小组成绩 public: //构造函数 Project(int pId, string pName, Student leader, Student members[]) { this->pId = pId; this->pName = pName; this->leader = leader; for(int i=0; i<3; i++) { this->members[i] = members[i]; } //计算小组成绩 pScore = (members[0].getScore() + members[1].getScore() + members[2].getScore()) / 3; } //获取项目编号 int getPid() { return pId; } //获取项目名称 string getPname() { return pName; } //获取组长 Student getLeader() { return leader; } //获取小组成员 Student* getMembers() { return members; } //获取小组成绩 float getScore() { return pScore; } //输出项目组信息 void printProject() { cout << "项目编号:" << pId << endl; cout << "项目名称:" << pName << endl; cout << "组长:" << leader.getName() << endl; cout << "小组成员:" << endl; for(int i=0; i<3; i++) { cout << members[i].getName() << endl; } cout << "小组成绩:" << pScore << endl; } }; int main() { //创建学生 Student s1(1, "张三", 80); Student s2(2, "李四", 85); Student s3(3, "王五", 90); Student s4(4, "赵六", 85); Student s5(5, "钱七", 92); Student s6(6, "孙八", 87); //创建项目组 Student members1[3] = {s1, s2, s3}; Project p1(1, "项目一", s1, members1); Student members2[3] = {s4, s5, s6}; Project p2(2, "项目二", s4, members2); //输出项目组信息 p1.printProject(); p2.printProject(); return 0; }修改程序
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
private:
int id; //学号
string name; //姓名
float score; //个人成绩
public:
//构造函数
Student(int id, string name, float score) {
this->id = id;
this->name = name;
this->score = score;
}
//获取学号
int getId() const {
return id;
}
//获取姓名
string getName() const {
return name;
}
//获取成绩
float getScore() const {
return score;
}
};
class Project {
private:
int pId; //项目编号
string pName; //项目名称
Student leader; //组长
vector<Student> members; //小组成员
float pScore; //小组成绩
public:
//构造函数
Project(int pId, string pName, Student leader, vector<Student> members) {
this->pId = pId;
this->pName = pName;
this->leader = leader;
this->members = members;
//计算小组成绩
float sum = leader.getScore();
for (Student member : members) {
sum += member.getScore();
}
pScore = sum / (members.size() + 1);
}
//获取项目编号
int getPid() const {
return pId;
}
//获取项目名称
string getPname() const {
return pName;
}
//获取组长
Student getLeader() const {
return leader;
}
//获取小组成员
vector<Student> getMembers() const {
return members;
}
//获取小组成绩
float getScore() const {
return pScore;
}
//输出项目组信息
void printProject() const {
cout << "项目编号:" << pId << endl;
cout << "项目名称:" << pName << endl;
cout << "组长:" << leader.getName() << endl;
cout << "小组成员:" << endl;
for (Student member : members) {
cout << member.getName() << endl;
}
cout << "小组成绩:" << pScore << endl;
}
};
int main() {
//创建学生
Student s1(1, "张三", 80);
Student s2(2, "李四", 85);
Student s3(3, "王五", 90);
Student s4(4, "赵六", 85);
Student s5(5, "钱七", 92);
Student s6(6, "孙八", 87);
//创建项目组
vector<Student> members1 {s1, s2, s3};
Project p1(1, "项目一", s1, members1);
vector<Student> members2 {s4, s5, s6};
Project p2(2, "项目二", s4, members2);
//输出项目组信息
p1.printProject();
p2.printProject();
return 0;
}
阅读全文