@[TOC]宁波实习第四天
基于SSM实现数据库的增删改查
今天教的是在SSM基础上实现对数据库的增删改查操作。
代码的总体结构
数据库结构
具体代码实现
一、具体流程
以添加方法为例:
(1)在IUserInfoDao中声明方法public void add(UserInfo userInfo);//添加方法
(2)在IUserInfoService中声明方法public void add(UserInfo userInfo);
(3)在UserInfoServiceImpl中实现
(4)在UserInfoMapper.xml里面实现方法
(5)在UserInfoController中控制跳转实现
二、具体代码
1.IUserInfoDao.java
public interface IUserInfoDao {
public List<UserInfo> findAll();
public UserInfo login(UserInfo userInfo);//登录方法
void add(UserInfo userInfo);//添加方法
UserInfo findUserById(int id);
public void update(UserInfo userInfo);
void delete(int id);//删除
UserInfo findUserByName(String name);
void deleteAll(List list);
}
2.UserInfoServiceImpl.java
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);
}
}
3.IUserInfoService.java
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.UserInfoMapper.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.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>
5.UserInfoController.java
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>
8.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>