0
点赞
收藏
分享

微信扫一扫

宁波实习培训记录(四)——基于SSM实现数据库的增删改查


在昨天的基础上继续实现增删改查功能。完整项目代码见​​完整代码​​

数据库结构如图

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_spring

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_java_02


我的端口是3305总的文件结构图如下

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_spring_03

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_spring_04

一、删除功能

  1. 在IUserInfoDao中声明方法public void delete(int id);
  2. 在IUserInfoService中声明方法public void delete(int id);
  3. 在UserInfoServiceImpl中实现方法

@Override
public void delete(int id) {
userInfoDao.delete(id);

}

4.在UserInfoMapper.xml里面实现方法

<delete id="delete" parameterType="int">
delete from userinfo where id=#{id}
</delete>

5.在UserInfoController中控制跳转实现

@RequestMapping("delete.do")
public ModelAndView delete(int id){
userInfoService.delete(id);
return findAll();
}

二、修改、添加功能

1、修改大致步骤与删除相似,需要注意的点是,修改我们写了新的页面,涉及到带参(也就是需要修改的id)进行跳转,多了toUpdate方法,还有就是findUserById方法用于查找到需要修改的对象,此方法实现流程和其他方法一样。然后就是用于更新数据的update方法,实现的步骤和delete一样
2、添加功能主要是实现insert方法,特别的地方在于,由于添加是跳转到新的页面,因此多了toInsert方法,这个方法由于不涉及数据库操作,直接在controller层实现即可

三、代码展示

1.dao层的IUserInfoDao

package com.zhongruan.dao;

import com.zhongruan.bean.UserInfo;

import javax.xml.registry.infomodel.User;
import java.util.List;

public interface IUserInfoDao {
public List<UserInfo> findAll();
public void toUpdate(int id);
public void insert(UserInfo userInfo);
public void delete(int id);
public UserInfo findUserById(int id);
public void update(UserInfo userInfo);

}

2、mapper中UserInfoMapper.xml用于实现dao中方法的相关配置

<?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.zhongruan.dao.IUserInfoDao" >
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo
</select>
<insert id="insert" parameterType="com.zhongruan.bean.UserInfo" >
insert into userinfo(name,password) values(#{name},#{password})
</insert>
<update id="update" parameterType="com.zhongruan.bean.UserInfo">
update userinfo set name=#{name},password=#{password} where id=#{id}
</update>
<delete id="delete" parameterType="int">
delete from userinfo where id=#{id}
</delete>
<select id="findUserById" resultType="com.zhongruan.bean.UserInfo" parameterType="int">
select * from userinfo where id=#{id}
</select>


</mapper>

3、IUserInfoService相关代码

package com.zhongruan.service;

import com.zhongruan.bean.UserInfo;

import java.util.List;

public interface IUserInfoService {
public List<UserInfo> findAll();
public void toUpdate(int id);
public void insert(UserInfo userInfo);
public void delete(int id);
public UserInfo findUserById(int id);
public void update(UserInfo userInfo);
}

4、UserInfoServiceImpl实现接口

package com.zhongruan.service.impl;

import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserInfoDao;
import com.zhongruan.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userInfoServiceImpl")
public class UserInfoServiceImpl implements IUserInfoService {

@Autowired
IUserInfoDao userInfoDao;
@Override
public List<UserInfo> findAll() {
return userInfoDao.findAll();
}

@Override
public void toUpdate(int id) {
userInfoDao.toUpdate(id);
}

@Override
public void insert(UserInfo userInfo) {
userInfoDao.insert(userInfo);
}


@Override
public void delete(int id) {
userInfoDao.delete(id);

}

@Override
public UserInfo findUserById(int id) {
return userInfoDao.findUserById(id);
}

@Override
public void update(UserInfo userInfo) {
userInfoDao.update(userInfo);
}


}

5、UserInfoController控制跳转

package com.zhongruan.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserInfoService;
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 org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("user")

public class UserInfoController {

@Autowired
IUserInfoService userInfoService;

@RequestMapping("findAll.do")
public ModelAndView findAll(){
List<UserInfo> users = userInfoService.findAll();
ModelAndView mv =new ModelAndView();
mv.addObject("users",users);
mv.setViewName("allUser");
return mv;
}

@RequestMapping("delete.do")
public ModelAndView delete(int id){
userInfoService.delete(id);
return findAll();
}

@RequestMapping("toUpdate.do")
public ModelAndView toUpdate(int id){
UserInfo userInfo=userInfoService.findUserById(id);
ModelAndView mv =new ModelAndView();
mv.addObject("userInfo",userInfo);
mv.setViewName("updateUser");
return mv;

}

@RequestMapping("update.do")
public ModelAndView update(UserInfo userInfo){
userInfoService.update(userInfo);
return findAll();
}

@RequestMapping("insert")
public ModelAndView insert(UserInfo userInfo){
userInfoService.insert(userInfo);
return findAll();
}

@RequestMapping("toInsert")
public ModelAndView toInsert(){
ModelAndView mv =new ModelAndView();
mv.setViewName("addUser");
return mv;
}



}

6、addUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
<title>新增用户</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于SSM框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>

<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增用户</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/user/insert.do"
method="post">
用  户  id:<input type="text" name="id"><br><br><br>
用户姓名:<input type="text" name="name"><br><br><br>
用户密码:<input type="text" name="password"><br><br><br>
<input type="submit" value="添加" >
</form>

</div>

7、updateUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>修改论文</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于SSM框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>

<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改用户</small>
</h1>
</div>
</div>
</div>

<form action="${pageContext.request.contextPath}/user/update.do"
method="post">
<input type="hidden" name="id" value="${userInfo.id}"/>
用户姓名:<input type="text" name="name" value="${userInfo.name}"/>
用户密码:<input type="text" name="password" value="${userInfo.password}"/>
<input type="submit" value="提交" />
</form>
</div>

四、结果展示

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_SSM_05


宁波实习培训记录(四)——基于SSM实现数据库的增删改查_SSM_06


添加后

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_SSM_07


修改

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_List_08


修改后

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_spring_09


删除

宁波实习培训记录(四)——基于SSM实现数据库的增删改查_spring_10


举报

相关推荐

0 条评论