增删改查的SSM小项目
经过将近一个月的摸索,终于算是勉强完成了关于增删改查的SSM项目。
github源码地址:https://github.com/123456abcdefg/Login
好了,话不多说,写一下具体代码。


实现代码:
1.controller:
登录:
package com.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.entity.user;
import com.service.userService; @Controller
@RequestMapping("/admin") public class userController {
// 注入UserService
@Autowired
private userService userService; @RequestMapping("/index")
public String index(){
return "index";
} // 登录
@RequestMapping("/login")
public String login(HttpServletRequest request,String username,String password){
user user =userService.getUser(username,password);
ModelAndView modelandview = new ModelAndView();
modelandview.addObject(user);
if(user.getIdent().equals("1"))
return "redirect:/admin/user/userlist.action";
else
return "redirect:/admin/index.action";
} @RequestMapping("/error")
public String error(){
return "error";
}
}
package com.controller;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.entity.user;
import com.service.userService; @Controller
@RequestMapping("/admin/user") public class userlistController {
// 注入UserService
@Autowired
private userService userService; @RequestMapping("/userlist")
public ModelAndView userlist(HttpServletRequest request,Integer search){
user user = new user();
user.setId(search);
List<user> userlist = userService.findUserList(user);
ModelAndView modelandview = new ModelAndView();
modelandview.addObject("userlist",userlist);
modelandview.setViewName("login");
return modelandview;
} //新增页面
@RequestMapping("/add")
public String add(HttpServletRequest request){
return "add_do";
} //增加用户
@RequestMapping("/add_do")
public String add_do(HttpServletRequest request,
String username,String password,String ident){
user user = new user();
user.setUsername(username);
user.setPassword(password);
user.setIdent(ident);
userService.addUser(user);
return "forward:userlist.action";
} //删除用户
@RequestMapping("/delete_do")
public String delete_do(HttpServletRequest request,Integer id){
userService.deleteUser(id);
return "forward:userlist.action";
} //修改页面
@RequestMapping("/update")
public ModelAndView update(Integer id){
ModelAndView modelandview = new ModelAndView();
user user=userService.getUserById(id);
modelandview.addObject(user);
modelandview.setViewName("add_do");
return modelandview;
} //修改用户信息
@RequestMapping("/update_do")
public String update_do(HttpServletRequest request,Integer id,String username,String password,String ident){
user user = new user();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setIdent(ident);
userService.updateUser(user);
return "forward:userlist.action";
}
}
2.user:
package com.entity;
import java.util.List; public class user {
private Integer id;
private String username;
private String password;
private String ident; public String getIdent() {
return ident;
}
public void setIdent(String ident) {
this.ident = ident;
}
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;
}
}
3.mapper:
package com.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.entity.user; public interface userMapper {
public user getUser(String username,String password);
public List<user> findUserList(user user);
public void addUser(user user);
public void deleteUser(Integer id);
public user getUserById(Integer id);
public void updateUser(user user);
}
mapper.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.mapper.userMapper">
<resultMap type="com.entity.user" id="userMapper">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="ident" property="ident" jdbcType="VARCHAR"/>
</resultMap> <select id="getUser" parameterType="com.entity.user" resultType="com.entity.user">
select *
from user
where username=#{0} and password=#{1}
</select> <select id="findUserList" parameterType="com.entity.user" resultType="com.entity.user">
select *
from user
where 1=1 and id like '%${id}%'
</select> <insert id="addUser" parameterType="com.entity.user">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="password != null">
password,
</if>
<if test="ident != null">
ident,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="ident != null">
#{ident,jdbcType=VARCHAR},
</if>
</trim>
</insert> <delete id="deleteUser" parameterType="com.entity.user">
delete
from user
where id=#{id,jdbcType=INTEGER}
</delete> <select id="getUserById" parameterType="com.entity.user" resultType="com.entity.user">
select *
from user
where id=#{id}
</select> <update id="updateUser" parameterType="com.entity.user">
update user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="ident != null">
ident = #{ident,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update> </mapper>
4.service:
userService:
package com.service;
import java.util.List;
import com.entity.user; public interface userService {
public user getUser(String username,String password);
public List<user> findUserList(user user);
public void addUser(user user);
public void deleteUser(Integer id);
public user getUserById(Integer id);
public void updateUser(user user);
}
userServiceimp:
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.user;
import com.mapper.userMapper; public class userServiceimp implements userService{
// 注入User
@Autowired
private userMapper usermapper; @Override
public user getUser(String username,String password){
return usermapper.getUser(username,password);
} @Override
public List<user> findUserList(user user){
return usermapper.findUserList(user);
} @Override
public void addUser(user user){
usermapper.addUser(user);
} @Override
public void deleteUser(Integer id){
usermapper.deleteUser(id);
} @Override
public user getUserById(Integer id){
return usermapper.getUserById(id);
} @Override
public void updateUser(user user){
usermapper.updateUser(user);
}
}
剩下的配置文件我就不粘了,大家可以去我github上下载源码查看。
登陆页面:
登陆成功页面: 
新增 页面:

大致就是这样了,实现了基本的增删改查操作,以后要是写出好的项目也会同样放在github上,欢迎大家下载讨论。
项目就先告一段落了,接下来的任务就是把SSM从头到尾看一遍,彻底搞懂SSM框架具体怎么实现。
增删改查的SSM小项目的更多相关文章
- SpringBoot-Vue实现增删改查及分页小DEMO
前言 主要通过后端 Spring Boot 技术和前端 Vue 技术来简单开发一个demo,实现增删改查.分页功能以及了解Springboot搭配vue完成前后端分离项目的开发流程. 开发栈 前端 开 ...
- mybatis的增删改查返回值小析(六)
本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...
- 无框架JavaWeb简单增删改查,纯 jsp小练习
地址 : 纯本人手码 jsp练习>>>>>
- ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查
三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...
- nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- springboot+layui实现PC端用户的增删改查 & 整合mui实现app端的自动登录和用户的上拉加载 & HBuilder打包app并在手机端下载安装
springboot整合web开发的各个组件在前面已经有详细的介绍,下面是用springboot整合layui实现了基本的增删改查. 同时在学习mui开发app,也就用mui实现了一个简单的自动登录和 ...
- 进入全屏 nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- 国产化之路-统信UOS + Nginx + Asp.Net MVC + EF Core 3.1 + 达梦DM8实现简单增删改查操作
专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
随机推荐
- 以@GetMapping为例,SpringMVC 组合注解
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写.该注解将HTTP Get 映射到 特定的处理方法上.
- Python Argparse模块
argparse模块 在Python中,argparse模块是标准库中用来解析命令行参数的模块,用来替代已经过时的optparse模块.argparse模块能够根据程序中的定义从sys.argv中解析 ...
- 李昊大佬的CV模板
#include<cstdio> #include<iostream> #include<cstdlib> #include<iomanip> #inc ...
- LOJ6433 [PKUSC2018] 最大前缀和 【状压DP】
题目分析: 容易想到若集合$S$为前缀时,$S$外的所有元素的排列的前缀是小于$0$的,DP可以做到,令排列前缀个数小于0的是g[S]. 令f[S]表示$S$是前缀,转移可以通过在前面插入元素完成. ...
- ramdom 中的 seed 的使用
实例 1 import ramdom # random.seed(10) # 未加 seed 的时候 for i in range(5): print(random.random()) # 每次输出结 ...
- python学习日记(深浅copy)
赋值 #赋值,指向同一内存地址 l1 = [1,2,3,4,5] l2 = l1 print(l1,l2) print(id(l1),id(l2)) 浅copy #浅copy,第一层互相独立,创建了新 ...
- 【hjmmm网络流24题补全计划】
本文食用方式 按ABC--分层叙述思路 可以看完一步有思路后自行思考 飞行员配对问题 题目链接 这可能是24题里最水的一道吧... 很显然分成两个集合 左外籍飞行员 右皇家飞行员 跑二分图最大匹配 输 ...
- HNOI2017 抛硬币 (FakeBeng)
除了队长快跑外最难的题吧. 除了需要写\(exLucas\)之外,还教会了我大量的卡常技巧. 首先\(70\)分就是个直接按题意模拟,易得\(ans=\sum_{j=0}^{b} C_{b}^{j}\ ...
- HAOI2016 简要题解
「HAOI2016」食物链 题意 现在给你 \(n\) 个物种和 \(m\) 条能量流动关系,求其中的食物链条数. \(1 \leq n \leq 100000, 0 \leq m \leq 2000 ...
- 添加默认安全组规则-openstack
if [ "$1" ] ;then neutron security-group-rule-create --direction ingress --ethertype ipv4 ...