1. bean层
User.java
package com.wx.bean;
public class User {
private Integer id;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
}
2. dao层
UserDao.java
package com.wx.dao;
import com.wx.bean.User;
import java.util.List;
public interface UserDao {
List<User> findAll();
void addUser(User user);
//修改前的查询
User UDPfindAllUserById(int id);
//更改用户信息
void UDPUserById(User user);
//模糊查询
List<User> findByName(String name);
//删除用户
void deleteById(int id);
}
3. service层
UserService.java
package com.wx.service;
import com.wx.bean.User;
import java.util.List;
public interface UserService {
List<User> findAll();
void addUser(User user);
User UDPfindAllUserById(int id);
void UDPUserById(User user);
//模糊查询
List<User> findByName(String name);
//删除用户
void deleteById(int id);
}
UserServiceImpl.java
package com.wx.service.impl;
import com.wx.dao.UserDao;
import com.wx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wx.bean.User;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public User UDPfindAllUserById(int id) {
return userDao.UDPfindAllUserById(id);
}
@Override
public void UDPUserById(User user) {
userDao.UDPUserById(user);
}
@Override
public List<User> findByName(String name) {
return userDao.findByName(name);
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
}
4. controller层
UserController.java
package com.wx.controller;
import com.wx.bean.User;
import com.wx.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.sql.DriverManager;
import java.util.List;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserServiceImpl userService;
@RequestMapping("findAll.do")
public ModelAndView findAll() {
List<User> users = userService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("userlist", users);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("addUser.do")
public String addUser(User user) {
userService.addUser(user);
return "redirect:findAll.do";
}
//修改前的查询
@RequestMapping("UDPfindAllUserById.do")
public ModelAndView UDPfindAllUserById(int id) {
User user = userService.UDPfindAllUserById(id);
ModelAndView mv = new ModelAndView();
mv.setViewName("user-update");
mv.addObject("user", user);
return mv;
}
//修改用户信息
@RequestMapping("updateUser.do")
public String UDPUserById(User user) {
userService.UDPUserById(user);
return "redirect:findAll.do";
}
//模糊查询
@RequestMapping("findByName.do")
public ModelAndView findByName(String name) {
List<User> users = userService.findByName(name);
ModelAndView mv = new ModelAndView();
mv.addObject("userlist", users);
mv.setViewName("user-list");
return mv;
}
//删除用户
@RequestMapping("deleteById.do")
public String deleteById(int id) {
userService.deleteById(id);
//提示删除成功
return "redirect:findAll.do";
}
}
分析ssm框架执行流程
- 刷新模块:
<button type="submit" class="btn btn-default" title="刷新" onclick="window.location='${pageContext.request.contextPath}/user/findAll.do'">
<i class="fa fa-refresh"></i> 刷新</button>
点击按钮后则转到action findAll,即调用了Controller层的findAll方法,Controller层依次调用service层以及aao层从数据库中查找除所有信息并封装到ModelAndView中,交由Spring-mvc框架进行显示。整个项目中的联系主要方式为ssm框架中的注解。
- 更新用户模块
首先根据用户id进行查询,转到UDPfindAllUserById.do,并在url中加入用户参数的信息 ,使用?和${}表达式:
<a href="${pageContext.request.contextPath}/user/UDPfindAllUserById.do?id=${user.id}"
class="btn bg-olive btn-xs">更新</a>
用户点击按钮后,程序转到UDPfindAllUserById.do,即调用了Controller层的UDPfindAllUserById方法,用于执行更新用户前对用户信息的查询,然后传递用户信息到user-update页面中显示,在user-update页面中保存按钮则对用户信息进行封装成User类,然后交由Controller层的UDPUserById(User user)方法完成对用户信息的更新。
ssm框架写到这儿就⑧写了。