根据班级查找学生信息(支持模糊查找)代码
时间: 2023-11-15 17:05:49 浏览: 49
假设有一个名为Student的实体类,包含属性:id、name、classId、gender等字段,以及对应的getter和setter方法。下面是根据班级查找学生信息的Mapper代码:
```xml
<!-- StudentMapper.xml -->
<mapper namespace="com.example.mapper.StudentMapper">
<!-- 根据班级查找学生信息,支持模糊查找 -->
<select id="getStudentsByClass" resultType="com.example.entity.Student">
SELECT * FROM student WHERE class_id = #{classId} AND name LIKE CONCAT('%',#{name},'%')
</select>
</mapper>
```
在上面的代码中,我们使用了SELECT语句来查询符合条件的学生信息。其中,class_id是数据库表中的班级id字段,#{classId}是我们从Java代码中传入的班级id参数;name是数据库表中的学生姓名字段,#{name}是我们从Java代码中传入的学生姓名参数,使用了模糊查询的方式来查找符合条件的学生信息。
在Java代码中,我们可以调用StudentMapper接口中的getStudentsByClass方法来执行上面的SQL语句,代码如下:
```java
// StudentMapper.java
public interface StudentMapper {
// 根据班级查找学生信息,支持模糊查找
List<Student> getStudentsByClass(@Param("classId") Integer classId, @Param("name") String name);
}
// StudentService.java
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
// 根据班级查找学生信息,支持模糊查找
public List<Student> getStudentsByClass(Integer classId, String name) {
return studentMapper.getStudentsByClass(classId, name);
}
}
```
在上面的代码中,我们通过@Autowired注解将StudentMapper注入到StudentService中,并且在StudentService中定义了一个getStudentsByClass方法,用于根据班级查找学生信息,支持模糊查找。在该方法中,我们调用了StudentMapper中的getStudentsByClass方法,并将查询结果返回给调用方。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)