Spring学习笔记(九)简单的SSM框架整合
实验目的
掌握SSM框架整合。
实验环境
硬件:PC机
操作系统:Windows
开发工具:idea
实验内容
整合SSM框架。
实验步骤
- 搭建SSM环境:构建web项目,导入需要的jar包,通过单元测试测试各层框架搭建的正确性。
2
Mapper编写User详情查询;service编写详情查询业务逻辑控制 controller层控制页面的跳转,在详情页面渲染用户详情。
项目结构:
Pom.xml配置文件
注意完成这部分后需要重新加载项目,这里创建了一个maven项目
4.0.0
com.example
SpringMVC1
1.0-SNAPSHOT
war
8
8
org.springframework
spring-core
5.3.10
org.springframework
spring-context
5.3.10
org.springframework
spring-web
5.3.10
org.springframework
spring-webmvc
5.3.10
org.mybatis
mybatis
3.5.7
org.mybatis
mybatis-spring
2.0.7
mysql
mysql-connector-java
8.0.28
org.springframework
spring-jdbc
5.3.10
javax.servlet
javax.servlet-api
4.0.1
provided
junit
junit
4.13.2
test
org.springframework
spring-test
5.3.10
test
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
utf-8
8080
/SSM测试
UserController
package org.example.controller;
import org.example.model.User;
import org.example.service.UserServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServie userServie;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView getUserById(@PathVariable("id") Long id) {
ModelAndView mav = new ModelAndView();
User user = userServie.getById(id);
// 添加user对象到ModelAndView中
mav.addObject("user", user);
// 设置视图名称为userDetails,对应userDetails.jsp页面
mav.setViewName("userDetails");
return mav;
}
}
UserMapper
package org.example.mapper;
import org.example.model.User;
public interface UserMapper {
User getById(Long id);
User getByUsername(String username); // 添加根据username查询的方法
}
User
package org.example.model;
public class User {
private Long id;
private String username;
private String password;
private Long departmentId;
public User() {
}
public User(Long id, String username, String password, Long departmentId) {
this.id = id;
this.username = username;
this.password = password;
this.departmentId = departmentId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
}
UserServiceImpl
package org.example.service.impl;
import org.example.mapper.UserMapper;
import org.example.model.User;
import org.example.service.UserServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserServie {
@Autowired
private UserMapper userMapper;
@Override
public User getById(Long id) {
return userMapper.getById(id);
}
@Override
public User getByUsername(String username) {
return userMapper.getByUsername(username);
}
}
UserServie
package org.example.service;
import org.example.model.User;
public interface UserServie {
User getById(Long id);
User getByUsername(String username); // 添加根据username查询的方法声明
}
编写UserMapper.xml
注意:s_user是数据库mydb中的一个数据表
SELECT id, username, password, dept_id AS departmentId
FROM s_user
WHERE id = #{id}
编写applicationContext.xml
编写user Details.jsp
User Details
User Details
ID
Name
Password
Department ID
${user.id}
${user.username}
${user.password}
${user.departmentId}
编写web.xml
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
dispatcher
/
编写一个测试类用于测试整合情况
UserServicelmplTest.java
package org.example.service.impl;
import org.example.model.User;
import org.example.service.UserServie;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserServicelmplTest {
@Autowired
private UserServie userService;
@Test
public void testGetById() {
Long userId = 11L;
User user = userService.getById(userId);
assertNotNull("The user should not be null", user);
assertEquals("The user ID should match the expected ID", userId, user.getId());
System.out.println("Retrieved user: " + user.toString());
}
}
测试结果
运行效果
部署本地后在本地浏览器的运行地址为:localhost:8080/SSM测试/user/12,其中12为响应的id