Java ssm框架搭建实现其他功能
前言UserMapper.xmlcc0701UserDaoUserServiceUserServiceImplUserController
Webadd.jspfailure.jspindex.jspmain.jspok.jspmodify.jsp
结果登录查看删除修改注册
前言
Java用ssm框架搭建实现简单注册和删除等功能,与MySQL结合。继上一篇(Java ssm框架搭建实现登录 1)链接: https://blog.csdn.net/cmm27/article/details/107063678.
UserMapper.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.cc0701.dao.UserDao">
<select id
="findUserByName" parameterType
="String" resultType
="user">
select
* from user where name
=#
{name
}
</select
>
<delete id
="deleteById" parameterType
="Integer">
delete from user where id
=#
{id
}
</delete
>
<insert id
="add" parameterType
="User">
insert into user
(name
,password
) values
(#
{name
},#
{password
})
</insert
>
<select id
="findAll" resultType
="user">
select
* from user
</select
>
<select id
="findUserById" parameterType
="Integer" resultType
="User">
select
* from user where id
=#
{id
}
</select
>
<update id
="update" parameterType
="User">
update user set name
=#
{name
},password
=#
{password
} where id
=#
{id
}
</update
>
<select id
="searchByName" parameterType
="String" resultType
="User">
select
* from user where name like
concat('%',#
{name
},'%')
</select
>
</mapper
>
cc0701
UserDao
package com
.cc0701
.dao
;
import com
.cc0701
.bean
.User
;
import java
.util
.List
;
public interface UserDao {
User
findUserByName(String name
);
List
<User> findAll();
int deleteById(Integer id
);
int add(User user
);
int update(User user
);
User
findUserById(Integer id
);
List
<User> searchByName(String name
);
}
UserService
package com
.cc0701
.service
;
import com
.cc0701
.bean
.User
;
import java
.util
.List
;
public interface UserService {
boolean login(String name
,String password
);
boolean delete(Integer id
);
List
<User> findAll();
boolean add(User user
);
User
findById(Integer id
);
boolean update(User user
);
List
<User> searchByName(String name
);
}
UserServiceImpl
package com
.cc0701
.service
.Impl
;
import com
.cc0701
.bean
.User
;
import com
.cc0701
.dao
.UserDao
;
import com
.cc0701
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;
import java
.util
.List
;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao
;
@Override
public boolean login(String name
, String password
) {
User user
= userDao
.findUserByName(name
);
if (user
!=null
&&user
.getPassword().equals(password
)){
return true;
}
return false;
}
@Override
public boolean delete(Integer id
) {
int del
= userDao
.deleteById(id
);
if (del
>0){
return true;
}
return false;
}
@Override
public List
<User> findAll() {
return userDao
.findAll();
}
@Override
public boolean add(User user
) {
int add
= userDao
.add(user
);
if (add
>0){
return true;
}
return false;
}
@Override
public User
findById(Integer id
) {
return userDao
.findUserById(id
);
}
@Override
public boolean update(User user
) {
int upd
= userDao
.update(user
);
if(upd
>0){
return true;
}
return false;
}
@Override
public List
<User> searchByName(String name
) {
return userDao
.searchByName(name
);
}
}
UserController
package com
.cc0701
.controller
;
import com
.cc0701
.bean
.User
;
import com
.cc0701
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.ui
.Model
;
import org
.springframework
.web
.bind
.annotation
.ModelAttribute
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestParam
;
import org
.springframework
.web
.servlet
.ModelAndView
;
import java
.util
.List
;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService
;
@RequestMapping("/login.do")
public ModelAndView
login(User user
){
boolean flag
= userService
.login(user
.getName(),user
.getPassword());
ModelAndView modelAndView
= new ModelAndView();
if (flag
){
modelAndView
.setViewName("../ok");
}else {
modelAndView
.setViewName("../failure");
}
return modelAndView
;
}
@RequestMapping("/findAll.do")
public ModelAndView
findAll(@RequestParam(value
= "name",defaultValue
= "")String name
){
ModelAndView modelAndView
= new ModelAndView();
List
<User> userList
= userService
.searchByName(name
);
System
.out
.println("userService中的内容为:"+userList
);
modelAndView
.addObject("userList",userList
);
modelAndView
.setViewName("../main");
System
.out
.println("modelAndView中的内容为:"+modelAndView
);
return modelAndView
;
}
@RequestMapping("/delete.do")
public String
delete(int id
){
boolean del
= userService
.delete(id
);
if (del
){
return "redirect:findAll.do";
}
return "../failure";
}
@RequestMapping("/add.do")
public String
add(User user
){
boolean add
= userService
.add(user
);
if (add
){
return "redirect:findAll.do";
}
return "../failure";
}
@RequestMapping("/findById.do")
public String
findById(int id
, Model model
){
User user
= userService
.findById(id
);
model
.addAttribute("user",user
);
return "../modify";
}
@RequestMapping("/update.do")
public String
update(User user
){
boolean upd
= userService
.update(user
);
if (upd
){
return "redirect:findAll.do";
}
return "../failure";
}
}
Web
add.jsp
webapp下建add.jsp
<%--
Created by IntelliJ IDEA
.
User
: 18235
Date
: 2020/7/2
Time
: 14:03
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>注册
</title
>
</head
>
<body>
<h1>注册
</h1
>
<div>
<form action
="/user/add.do">
<div>
<label
for="name">用户名:
</label
>
<input name
="name" type
="text" id
="name">
</div
>
<div>
<label
for="password">密码:
</label
>
<input name
="password" type
="text" id
="password">
</div
>
<div>
<input type
="submit" value
="注册">
</div
>
<a href
="javascript:window.history.go(-1)">返回
</a
>
</form
>
</div
>
</body
>
</html
>
failure.jsp
<%--
Created by IntelliJ IDEA
.
User
: 18235
Date
: 2020/7/1
Time
: 12:12
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>操作失败
</title
>
</head
>
<body>
<h1>操作失败
</h1
>
</body
>
</html
>
index.jsp
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>$Title$
</title
>
</head
>
<body>
<h1>登录
</h1
>
<form action
="/user/login.do" method
="post">
name
:<input name
="name" type
="text">
password
:<input name
="password" type
="password">
<input type
="submit" value
="login">
</form
>
<a href
="add.jsp">注册
</a
>
<a href
="/user/findAll.do">查看
</a
>
</body
>
</html
>
main.jsp
webapp下建main.jsp
<%@ taglib prefix
="c" uri
="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA
.
User
: 18235
Date
: 2020/7/2
Time
: 9:22
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>列表
</title
>
</head
>
<body>
<div>
<form action
="/user/findAll.do">
<input id
="name" type
="search" name
="name" value
="${name}">
<button onclick
="form.submit()">搜索
</button
>
</form
>
<table>
<thead>
<tr>
<th>ID
</th
>
<th>用户名
</th
>
<th>密码
</th
>
<th>操作
</th
>
</tr
>
</thead
>
<thead>
<c
:forEach items
="${userList}" var
="user">
<tr>
<td>$
{user
.id
}</td
>
<td>$
{user
.name
}</td
>
<td>$
{user
.password
}</td
>
<td>
<a href
="${pageContext.request.contextPath}/user/delete.do?id=${user.id}">删除
</a
>
<a href
="${pageContext.request.contextPath}/user/findById.do?id=${user.id}">修改
</a
>
</td
>
</tr
>
</c
:forEach
>
</thead
>
</table
>
</div
>
</body
>
</html
>
ok.jsp
<%--
Created by IntelliJ IDEA
.
User
: 18235
Date
: 2020/7/1
Time
: 11:32
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>成功
</title
>
</head
>
<body>
<h1>成功
</h1
>
</body
>
</html
>
modify.jsp
webapp小建modify.jsp
<%--
Created by IntelliJ IDEA
.
User
: 18235
Date
: 2020/7/3
Time
: 9:40
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>修改
</title
>
</head
>
<body>
<div>
<form action
="/user/update.do">
<input name
="id" value
="${user.id}" type
="hidden">
<div>
<label
for="name">用户名:
</label
>
<input name
="name" type
="text" id
="name" value
="${user.name}">
</div
>
<div>
<label
for="password">密码:
</label
>
<input name
="password" type
="text" id
="password" value
="${user.password}">
</div
>
<div>
<input type
="submit" value
="修改">
</div
>
<a href
="javascript:window.history.go(-1)">返回
</a
>
</form
>
</div
>
</body
>
</html
>
结果
登录
查看
删除
修改
注册