查询学生表所有信息,用MyBatis-Plus的selecList()怎么写
时间: 2024-03-15 09:46:28 浏览: 22
假设你已经定义了一个名为 `Student` 的实体类,对应着数据库中的 `student` 表,那么可以按照如下方式使用 MyBatis-Plus 的 `selectList()` 方法查询学生表所有信息:
1. 首先,需要引入 MyBatis-Plus 的依赖,以及相关的数据库连接等配置。
2. 创建一个继承自 `BaseMapper<Student>` 接口的 DAO 接口,例如 `StudentMapper`。
3. 在 `StudentMapper` 中声明一个方法,用于查询学生表所有信息:
```java
public interface StudentMapper extends BaseMapper<Student> {
/**
* 查询学生表所有信息
*/
List<Student> selectAll();
}
```
4. 在 XML 配置文件中编写 SQL 语句,可以使用 MyBatis-Plus 提供的 `<select>` 标签,也可以使用普通的 SQL 语句。例如:
```xml
<!-- 使用 MyBatis-Plus 提供的 select 标签查询学生表所有信息 -->
<select id="selectAll" resultType="com.example.entity.Student">
SELECT *
FROM student
</select>
<!-- 使用普通的 SQL 语句查询学生表所有信息 -->
<select id="selectAll" resultType="com.example.entity.Student">
<![CDATA[
SELECT *
FROM student
]]>
</select>
```
5. 在 `StudentMapper` 接口中调用 `selectList()` 方法,传入条件构造器对象即可查询学生表所有信息:
```java
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getAllStudents() {
return studentMapper.selectList(null);
}
}
```
注意:在调用 `selectList()` 方法时,传入的条件构造器对象为 `null`,表示查询所有数据。如果需要添加查询条件,可以使用 MyBatis-Plus 提供的条件构造器类,例如 `QueryWrapper`。