实验目的:
- 掌握Spring、MyBatis以及SpringMVC框架的使用。
- 掌握SSM框架的整合步骤,环境搭建,整合思路。
- 掌握SSM框架整合时的配置文件内容。
- 掌握SSM框架整合应用程序的编写。
实验内容及步骤:
1)在src目录下,创建com.itheima.po包,并在包中创建持久化类Customer:
2)在src目录下,创建一个com.itheima.dao包,并在包中创建接口文件CustomerDao以及对应的映射文件CustomerDao.xml
3)在src目录下,创建com.itheima.service包,然后在包中创建接口文件CustomerService,并在CustomerService中定义通过id查询客户的方法
4)在src目录下,创建一个com.itheima.service.impl包,并在包中创建CustomerService接口的实现类CustomerServiceImpl
5)在src目录下,创建一个com.itheima.controller包,并在包中创建用于处理页面请求的控制类CustomerController:
6)在WEB-INF目录下,创建一个jsp文件夹,在该文件夹下创建一个用于展示客户详情的页面文件customer.jsp:
7)将项目发布到Tomcat服务器并启动,在浏览器中访问地址http://localhost:8080/chapter17/findCustomerById?id=1,显示效果如下所示:
实验代码:
代码语言:javascript复制//Customer.java
package com.xzk.po;
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
//CustomerDao.java
package com.xzk.dao;
import com.xzk.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id) ;
}
//CustomerDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xzk.dao.CustomerDao">
<!--根据id查询客户信息 -->
<select id="findCustomerById" parameterType="Integer"
resultType="Customer">
select * from t_customer where id = #{id}
</select>
</mapper>
//CustomerConrroller.java
package com.xzk.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xzk.po.Customer;
import com.xzk.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
* 根据id查询客户详情
*/
@RequestMapping("/findCustomerById")
public String findCustomerById(Integer id,Model model) {
Customer customer = customerService.findCustomerById(id);
model.addAttribute("customer", customer);
//返回客户信息展示页面
return "customer";
}
}
//CustomerService.java
package com.xzk.service;
import com.xzk.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
//CustomerServiceImpl.java
package com.xzk.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xzk.dao.CustomerDao;
import com.xzk.po.Customer;
@Service
@Transactional
public class CustomerServiceImpl {
//注解注入CustomerDao
@Autowired
private CustomerDao customerDao;
//查询客户
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
//Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置加载Spring文件的监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class> org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/:拦截所有请求(除了jsp)-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
//customer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户信息界面</title>
</head>
<body>
<table border=1>
<tr>
<td>编号</td>
<td>名称</td>
<td>职业</td>
<td>电话</td>
</tr>
<tr>
<td>${customer.id}</td>
<td>${customer.username}</td>
<td>${customer.jobs}</td>
<td>${customer.phone}</td>
</tr>
</table>
</body>
</html>