首页 > Java框架应用 > Spring MVC 入门教程 > Spring MVC 整合SSM(下)

Spring MVC 整合 SSM(下)

1. 前言

本节课程将在 SSM 的基础上实现学生信息查询模块,要求不仅查询出学生信息,还要求查询出学生所在班级信息。本模块功能在实现上除了整体遵循 MVC 以外,会引用业务层的概念。

本节课的重点是学会灵活运用 SSM ,当然,在使用过程中,对于初学者来讲,业务层的引用会增加代码量,大家需要理解引用的目的。

2. 前期准备

学生信息查询模块的功能描述:用户进入 index.jsp 页面,页面中显示所有学生信息;点击某一个学生时,会弹出一个对话框,显示此学生的信息。

实现学生信息查询模块之前,先要做几个准备工作。

  1. 进入 MySql ,创建 学生表和班级表;

图片描述

  1. 构建班级 ( classRoom )、学生 ( student ) 实体类:

    public class classRoom { private Integer stuId; private String className; //…… }

班级实体类很简单,但是构建学生实体类时需要考虑页面显示需求。除了显示学生外,还要显示学生所在的班级信息。所以,设计实体类时,需要考虑如何和班级对象产生关系:

public class Student implements Serializable {
    private Integer stuId;
    private String stuName;
    private String stuPassword;
    //引用班级对象
    private ClassRoom classRoom;
}
  1. 设计页面。因为页面需要使用 JSTL 标签库,需要打开项目的 pom.xml 文件,在其中添加 JSTL 依赖包。

    javax.servlet jstl 1.2

在页面中使用 JSTL 显示从控制器中传过来的学生数据。

<body>
    当前登录者:${loginUser.userLoginName}
    <h2>学生列表</h2>
    <c:forEach var="stu" items="${students}">
        <div>
<span>${stu.stuId}</span> <span>${stu.stuName}</span> <span>${stu.classRoom.className}</span>
        </div>
    </c:forEach>
</body>

3. 核心逻辑

先看一下项目结构。

图片描述

对项目中的主要核心组件逐一介绍一下:

3.1 MyBatis 映射器

组件功能描述:对数据库中的数据进行操作,这里是查询出所有学生。

public interface StudentMapper {
    public List<Student> getStudents();
}

SQL 语句映射: SQL 语句放置在 StudentMapper.xml 文件中。因 SQL 语句是多表查询,需要进行关联映射。

<mapper namespace="com.mk.web.dao.StudentMapper">
<resultMap type="com.mk.web.entity.Student" id="stuMap">
    <result column="stuId" property="stuId" />
    <result column="stuName" property="stuName" />
    <result column="stuPassword" property="stuPassword" />
    <association property="classRoom" column="classId">
        <result column="classId" property="classId" />
        <result column="className" property="className" />
    </association>
</resultMap>
<select id="getStudents" resultMap="stuMap">
    SELECT
    student.stuId,
    student.classId,
    student.stuName,
    student.stuPassword,
    student.stuPic,
    classroom.classId,
    classroom.className
    FROM
    student
    inner join
    classroom
    on
    student.classId=classroom.classId
</select>
</mapper>

3.2 业务层接口

功能描述: 定义查询所有学生业务。

public interface IStudentService {
    public List<Student> getAllStudents;
}

3.3 业务层实现类

功能描述: 提供查询出所有学生的业务逻辑。

public class StudentService implements IStudentService {
    @Autowired
    private StudentMapper StudentMapper;

    @Override
    public List<Student> getAllStudents() { 
        return this.StudentMapper.getStudents();;
    }
}

Tips: 业务对象依赖于映射器组件。使用 @Autowired 注解让 Spring 自动注入进来。

3.4 控制器组件

功能描述: 用来响应页面的请求。

@Controller
@RequestMapping("/student")
public class StudentAction {

    @Autowired
    private IStudentService StudentService;

    @RequestMapping("/list")
    public String list(ModelMap map) {
        List<Student> students= this.StudentService.getAllStudents();
        map.addAttribute("students", students);
        return "index";
    }
}

Tips: 控制器组件依赖业务层组件。

3.4 核心组件之间的依赖关系

图片描述

4. 测试

发布项目,启动服务器,打开浏览器,在地址栏中输入:http://localhost:8888/sm-demo/student/list 。可以在浏览器中看到。

图片描述

5. 小结

本章节课程讲解了一个较完整的功能模块,运用到了业务逻辑层的概念。此查询模块的业务并不是很复杂,中间借助于数据层映射器完成了对学生的查询。

学生查询信息中因包括班级信息,SQL 语句的实体类中的属性与表字段之间的映射略显得有点复杂。对于学习过 MyBatis 的学习者而言理解并不会有太多难度。如果对 MyBatis 并不是很熟悉,请查阅相关的 WIKI 课程。

本文来自互联网用户投稿,不拥有所有权,该文观点仅代表作者本人,不代表本站立场。
访问者可将本网站提供的内容或服务用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。
本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站,邮箱:80764001@qq.com,予以删除。
© 2023 PV138 · 站点地图 · 免责声明 · 联系我们 · 问题反馈