在原题基础上完成1编写Student实体类编写Student实体类有属性idLongsnameStringsageint addressString创建相对于的数据库表主键为id2 参考UserMapper接口创建StudentMapper并完善在StudentMapper接口中添加方法findbyId返回值User对象和findAllList集合findBbySname返回集合可能有重名。3创建
- Student实体类
public class Student {
private Long id;
private String sname;
private int sage;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- StudentMapper
public interface StudentMapper {
Student findById(Long id);
List<Student> findAll();
List<Student> findBySname(String sname);
}
- StudentMapper.xml
<mapper namespace="com.example.mapper.StudentMapper">
<select id="findById" resultType="com.example.entity.Student">
select * from student where id = #{id}
</select>
<select id="findAll" resultType="com.example.entity.Student">
select * from student
</select>
<select id="findBySname" resultType="com.example.entity.Student">
select * from student where sname = #{sname}
</select>
</mapper>
- StudentService接口和StudentServiceImpl实现类
public interface StudentService {
Student getStudent(Long id);
List<Student> getStudents();
List<Student> getStudentsBySname(String sname);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student getStudent(Long id) {
return studentMapper.findById(id);
}
@Override
public List<Student> getStudents() {
return studentMapper.findAll();
}
@Override
public List<Student> getStudentsBySname(String sname) {
return studentMapper.findBySname(sname);
}
}
- StudentController
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/index")
public ModelAndView allList(HttpSession session) {
ModelAndView mav = new ModelAndView();
User user = (User) session.getAttribute("user");
if (user != null) {
List<Student> students = studentService.getStudents();
mav.addObject("students", students);
mav.setViewName("students");
} else {
mav.setViewName("index");
}
return mav;
}
}
``
原文地址: http://www.cveoy.top/t/topic/hfCS 著作权归作者所有。请勿转载和采集!