ssm框架整合实现简单的增删改查功能!!!

参考这个环境配置:ssm框架的简单整合!!!(配置环境)-CSDN博客

思路:建立好环境后,我们要根据Spring MVC三层架构的思想建立项目结构。

ssm框架整合实现简单的增删改查功能!!!_第1张图片

 一、controller控制层:(一共有两个类:AccountController和LoginController)

AccountController:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import com.by.pojo.Account;
import com.by.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 

Project: SpringMVC - AccountController

*

Powered by scl On 2024-01-10 16:28:10

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; @RequestMapping("/loginAccount") public String login(HttpServletRequest request) { Integer id = (Integer) request.getAttribute("id"); String userName = request.getAttribute("userName").toString(); String password= request.getAttribute("password").toString(); return "selectAccount"; } /** * 查询 * * @param model * @return */ @RequestMapping("/selectAccount") public String selectAccount(Model model) { List accountList = accountService.selectAccount(); model.addAttribute("accountList", accountList); return "select_Account"; } /** * 添加前的跳转页面 * * @return */ @RequestMapping("/toAccount") public String toAccount() { return "add_Account"; } /** * 添加页面 * * @param account * @param img * @param request * @return * @throws IOException */ @RequestMapping("/addAccount") public String addAccount(Account account, MultipartFile img, HttpServletRequest request) throws IOException { if (!img.isEmpty()){ //获取上传目录 String realPath = request.getSession().getServletContext().getRealPath("uploads"); //判断文件是否存在 File file = new File(realPath); if (!file.exists()) { file.mkdirs(); } //上传 String filename = img.getOriginalFilename(); img.transferTo(new File(realPath, filename)); account.setHead(filename); } accountService.addAccount(account); return "redirect:selectAccount"; } /** * 根据id删除 * * @param id * @return */ @GetMapping("/deleteAccount/{id}") public String deleteAccount(@PathVariable Integer id) { accountService.deleteAccount(id); return "redirect:/account/selectAccount"; } /** * 数据回显 * * @param id * @param model * @return */ @RequestMapping("/upAccount/{id}") public String upAccount(@PathVariable Integer id, Model model) { Account account = accountService.selectAccountById(id); model.addAttribute("account", account); return "update_Account"; } /** * 修改 */ @RequestMapping("/updateAccount/{id}") public String updateAccount(@PathVariable Integer id, Account account, MultipartFile img, HttpServletRequest request) throws IOException { if (!img.isEmpty()){ //获取上传路径 String realPath = request.getSession().getServletContext().getRealPath("uploads"); //判断目录是否存在 File file = new File(realPath); if (!file.exists()) { file.mkdirs(); } //上传 String filename = img.getOriginalFilename(); img.transferTo(new File(realPath, filename)); account.setHead(filename); } account.setId(id); accountService.updateAccount(account); return "redirect:/account/selectAccount"; } }

LoginController:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import com.by.pojo.User;
import com.by.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 

Project: SpringMVC - LoginController

*

Powered by scl On 2024-01-11 10:00:36

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Controller @RequestMapping("/account") public class LoginController { @Autowired private LoginService loginService; @PostMapping("/loginAccount") public String loginAccount(User user) { User userDome = loginService.login(user); if (userDome!=null)return "redirect:selectAccount"; else return "redirect:/index.jsp"; } }

二、相应的service也有两个,两个接口和两个实现类

AccountService:

package com.by.service;

import com.by.pojo.Account;
import com.by.pojo.User;

import java.util.List;

/**
 * 

Project: SpringMVC - AccountService

*

Powered by scl On 2024-01-10 16:32:52

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface AccountService { List selectAccount(); void addAccount(Account account); void deleteAccount(Integer id); Account selectAccountById(Integer id); void updateAccount(Account account); }

AccountServiceImpl:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.service;

import com.by.mapper.AccountMapper;
import com.by.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 

Project: SpringMVC - AccountServiceImpl

*

Powered by scl On 2024-01-10 16:33:40

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Service @Transactional public class AccountServiceImpl implements AccountService{ @Autowired private AccountMapper accountMapper; @Override public List selectAccount() { List accountList= accountMapper.selectAccount(); return accountList; } @Override public void addAccount(Account account) { accountMapper.addAccount(account); } @Override public void deleteAccount(Integer id) { accountMapper.deleteAccount(id); } @Override public Account selectAccountById(Integer id) { return accountMapper.selectAccountById(id); } @Override public void updateAccount(Account account) { accountMapper.updateAccount(account); } }

LoginService:

package com.by.service;

import com.by.pojo.User;

/**
 * 

Project: SpringMVC - LoginService

*

Powered by scl On 2024-01-11 10:26:40

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface LoginService { User login(User user); }

LoginServiceImpl:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.service;

import com.by.mapper.LoginMapper;
import com.by.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 

Project: SpringMVC - LoginServiceImpl

*

Powered by scl On 2024-01-11 10:27:13

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Service @Transactional public class LoginServiceImpl implements LoginService { @Autowired private LoginMapper loginMapper; @Override public User login(User user) { return loginMapper.login(user); } }

三、mapper层也就是我们说的dao层

AccountMapper:

package com.by.mapper;

import com.by.pojo.Account;

import java.util.List;

/**
 * 

Project: SpringMVC - AccountMapper

*

Powered by scl On 2024-01-10 16:34:53

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface AccountMapper { List selectAccount(); void addAccount(Account account); void deleteAccount(Integer id); Account selectAccountById(Integer id); void updateAccount(Account account); }

AccountMapper.xml:




    
        insert into account
        
            
                name,
            
            
                money,
            
            
                date,
            
            
                head,
            
        
        
            
                #{name},
            
            
                #{money},
            
            
                #{date},
            
            
                #{head},
            
        
    
    
        update account
        
            
                name=#{name},
            
            
                money=#{money},
            
            
                date=#{date},
            
            
                head=#{head},
            
        
            where id =#{id}
    
    
        delete
        from account
        where id = #{id}
    

    
    

LoginMapper:

package com.by.mapper;

import com.by.pojo.User;

/**
 * 

Project: SpringMVC - LoginMapper

*

Powered by scl On 2024-01-11 10:28:21

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface LoginMapper { User login(User user); }

LoginMapper.xml:






    

四、实体类:(Account和User)

Account:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.pojo;

import java.util.Date;

/**
 * 

Project: SpringMVC - Account

*

Powered by scl On 2024-01-10 16:31:18

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class Account { private Integer id; private String name; private Double money; private Date date; private String head; @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + ", date=" + date + ", head='" + head + '\'' + '}'; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } }

User:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.pojo;

/**
 * 

Project: SpringMVC - User

*

Powered by scl On 2024-01-11 10:23:01

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class User { private Integer id ; private String username; private String password; 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; } }

这里还用到了自定义的日期转换格式:(converter包下的DateConverter)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 

Project: SpringMVC - DateConverter

*

Powered by scl On 2024-01-10 19:39:59

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class DateConverter implements Converter { @Override public Date convert(String source) { try { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(source); } catch (Exception e) { e.printStackTrace(); } return null; } }

全局异常处理页面:GlobalException:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 

Project: SpringMVC - GlobalException

*

Powered by scl On 2024-01-10 21:15:16

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Component public class GlobalException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { /** * 1.发邮件、发信息 * 2.跳转到错误页面 */ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("msg",e.getMessage()); modelAndView.setViewName("exception"); return modelAndView; } }

接下来是jsp页面:

ssm框架整合实现简单的增删改查功能!!!_第2张图片

add_Account.jsp:

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/1/10
  Time: 19:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


账号:
存款:
日期:
头像:

exception.jsp:

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/1/10
  Time: 21:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


上传文件不能为空:${msg}



select_Account.jsp:

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/1/10
  Time: 16:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>


    Title


查询所有账户

添加
id name money date head delete
${account.id} ${account.name} ${account.money} 删除 / 修改

update_Acount.jsp:

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/1/10
  Time: 21:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>


    Title


账号:
存款:
日期:
头像:

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2023/8/14
  Time: 9:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    用户登录
    
    
    





你可能感兴趣的:(spring,mvc,java,intellij-idea)