补全代码 根据班级查找学生信息(支持模糊查找),并输出学生信息。 最终运行结果如下: 学生姓名:汪罗睿 所在班级:软工1503 生日: 1995-11-02 性别:男 址:北京市利星行广场微软大厦206体验中心 联系电话: 13939688375 ****** *****★- *★★★★★ 学生姓名:王玉强 所在班级:软工1503 生日: 1995-09-07 性别:男 址:吉林省长春市东南湖大路1281号 联系电话: 16070958343 轩 ****** ***** 学生姓名:王浩 所在班级:软工1502 生日: 1996-02-09 性别:男 址:福建省福州市湖东路中大厦,兴业银行 联系电话: 15851868080 ****** ****** ****** 学生姓名:包敦喜 所在班级:软工1503 生日: 1995-05-15 性别:男 住址:北京市西城区阜成i ]外大街29号, 联系电话: 17215808700 ****** ****** ****** public class StudentMapper implements RowMapper<Student> { //自定义映射规则 public Student mapRow(ResultSet arg0, int arg1) throws SQLException { // TODO Auto-generated method stub ...........(代码1) } }
时间: 2023-07-01 19:19:29 浏览: 103
补全代码如下:
package com.example.demo.dao.impl;
import com.example.demo.dao.StudentDao;
import com.example.demo.entity.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
public StudentDaoImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Student> findByClass(String className) {
String sql = "SELECT * FROM student WHERE class_name LIKE ?";
Object[] args = {"%" + className + "%"};
return jdbcTemplate.query(sql, args, new StudentMapper());
}
private class StudentMapper implements RowMapper<Student> {
@Override
public Student mapRow(ResultSet resultSet, int i) throws SQLException {
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setClassName(resultSet.getString("class_name"));
student.setBirthday(resultSet.getDate("birthday"));
student.setGender(resultSet.getString("gender"));
student.setAddress(resultSet.getString("address"));
student.setTel(resultSet.getString("tel"));
return student;
}
}
}
其中,代码1应该是这样的:
Student student = new Student();
student.setId(arg0.getInt("id"));
student.setName(arg0.getString("name"));
student.setClassName(arg0.getString("class_name"));
student.setBirthday(arg0.getDate("birthday"));
student.setGender(arg0.getString("gender"));
student.setAddress(arg0.getString("address"));
student.setTel(arg0.getString("tel"));
return student;
阅读全文