Spring MVC 整合 SSM(下)
1. 前言
本節課程將在 SSM 的基礎上實現學生信息查詢模塊,要求不僅查詢出學生信息,還要求查詢出學生所在班級信息。本模塊功能在實現上除了整體遵循 MVC 以外,會引用業務層的概念。
本節課的重點是學會靈活運用 SSM ,當然,在使用過程中,對于初學者來講,業務層的引用會增加代碼量,大家需要理解引用的目的。
2. 前期準備
學生信息查詢模塊的功能描述:用戶進入 index.jsp 頁面,頁面中顯示所有學生信息;點擊某一個學生時,會彈出一個對話框,顯示此學生的信息。
實現學生信息查詢模塊之前,先要做幾個準備工作。
- 進入 MySql ,創建 學生表和班級表;
- 構建班級 ( 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;
}
- 設計頁面。因為頁面需要使用 JSTL 標簽庫,需要打開項目的 pom.xml 文件,在其中添加 JSTL 依賴包。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
在頁面中使用 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 ??梢栽跒g覽器中看到。
5. 小結
本章節課程講解了一個較完整的功能模塊,運用到了業務邏輯層的概念。此查詢模塊的業務并不是很復雜,中間借助于數據層映射器完成了對學生的查詢。
學生查詢信息中因包括班級信息,SQL 語句的實體類中的屬性與表字段之間的映射略顯得有點復雜。對于學習過 MyBatis 的學習者而言理解并不會有太多難度。如果對 MyBatis 并不是很熟悉,請查閱相關的 WIKI 課程。