Spring MVC 整合 SSM(下)
1. 前言
本節(jié)課程將在 SSM 的基礎(chǔ)上實(shí)現(xiàn)學(xué)生信息查詢模塊,要求不僅查詢出學(xué)生信息,還要求查詢出學(xué)生所在班級信息。本模塊功能在實(shí)現(xiàn)上除了整體遵循 MVC 以外,會(huì)引用業(yè)務(wù)層的概念。
本節(jié)課的重點(diǎn)是學(xué)會(huì)靈活運(yùn)用 SSM ,當(dāng)然,在使用過程中,對于初學(xué)者來講,業(yè)務(wù)層的引用會(huì)增加代碼量,大家需要理解引用的目的。
2. 前期準(zhǔn)備
學(xué)生信息查詢模塊的功能描述:用戶進(jìn)入 index.jsp 頁面,頁面中顯示所有學(xué)生信息;點(diǎn)擊某一個(gè)學(xué)生時(shí),會(huì)彈出一個(gè)對話框,顯示此學(xué)生的信息。
實(shí)現(xiàn)學(xué)生信息查詢模塊之前,先要做幾個(gè)準(zhǔn)備工作。
- 進(jìn)入 MySql ,創(chuàng)建 學(xué)生表和班級表;
- 構(gòu)建班級 ( classRoom )、學(xué)生 ( student ) 實(shí)體類:
public class classRoom {
private Integer stuId;
private String className;
//……
}
班級實(shí)體類很簡單,但是構(gòu)建學(xué)生實(shí)體類時(shí)需要考慮頁面顯示需求。除了顯示學(xué)生外,還要顯示學(xué)生所在的班級信息。所以,設(shè)計(jì)實(shí)體類時(shí),需要考慮如何和班級對象產(chǎn)生關(guān)系:
public class Student implements Serializable {
private Integer stuId;
private String stuName;
private String stuPassword;
//引用班級對象
private ClassRoom classRoom;
}
- 設(shè)計(jì)頁面。因?yàn)轫撁嫘枰褂?JSTL 標(biāo)簽庫,需要打開項(xiàng)目的 pom.xml 文件,在其中添加 JSTL 依賴包。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
在頁面中使用 JSTL 顯示從控制器中傳過來的學(xué)生數(shù)據(jù)。
<body>
當(dāng)前登錄者:${loginUser.userLoginName}
<h2>學(xué)生列表</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. 核心邏輯
先看一下項(xiàng)目結(jié)構(gòu)。
對項(xiàng)目中的主要核心組件逐一介紹一下:
3.1 MyBatis 映射器
組件功能描述:對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行操作,這里是查詢出所有學(xué)生。
public interface StudentMapper {
public List<Student> getStudents();
}
SQL 語句映射: SQL 語句放置在 StudentMapper.xml 文件中。因 SQL 語句是多表查詢,需要進(jìn)行關(guān)聯(lián)映射。
<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 業(yè)務(wù)層接口
功能描述: 定義查詢所有學(xué)生業(yè)務(wù)。
public interface IStudentService {
public List<Student> getAllStudents;
}
3.3 業(yè)務(wù)層實(shí)現(xiàn)類
功能描述: 提供查詢出所有學(xué)生的業(yè)務(wù)邏輯。
public class StudentService implements IStudentService {
@Autowired
private StudentMapper StudentMapper;
@Override
public List<Student> getAllStudents() {
return this.StudentMapper.getStudents();;
}
}
Tips: 業(yè)務(wù)對象依賴于映射器組件。使用 @Autowired 注解讓 Spring 自動(dòng)注入進(jìn)來。
3.4 控制器組件
功能描述: 用來響應(yīng)頁面的請求。
@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: 控制器組件依賴業(yè)務(wù)層組件。
3.4 核心組件之間的依賴關(guān)系
4. 測試
發(fā)布項(xiàng)目,啟動(dòng)服務(wù)器,打開瀏覽器,在地址欄中輸入:http://localhost:8888/sm-demo/student/list ??梢栽跒g覽器中看到。
5. 小結(jié)
本章節(jié)課程講解了一個(gè)較完整的功能模塊,運(yùn)用到了業(yè)務(wù)邏輯層的概念。此查詢模塊的業(yè)務(wù)并不是很復(fù)雜,中間借助于數(shù)據(jù)層映射器完成了對學(xué)生的查詢。
學(xué)生查詢信息中因包括班級信息,SQL 語句的實(shí)體類中的屬性與表字段之間的映射略顯得有點(diǎn)復(fù)雜。對于學(xué)習(xí)過 MyBatis 的學(xué)習(xí)者而言理解并不會(huì)有太多難度。如果對 MyBatis 并不是很熟悉,請查閱相關(guān)的 WIKI 課程。