之前已经实现了数据库的查询,具体见博文https://blog.csdn.net/weixin_42493072/article/details/93966070
今天,不仅实现查询功能,还实现了修改、删除、添加功能。
源代码:https://download.csdn.net/download/weixin_42493072/11267633
**
**
IntelliJ IDEA 2018
JDK1.8
tomcat 7.0.79
Mysql 5.0
Maven 3.6.0
完成后的工程结构:
具体的搭建过程参考上面的博文,与其类似,具体代码,如下:
- ssm_parent下的pom.xml
4.0.0
com.zhongruan
ssm_parent
pom
1.0-SNAPSHOT
ssm_bean
ssm_dao
ssm_service
ssm_web
UTF-8
1.7
1.7
5.0.2.RELEASE
3.2.6
1.7.7
1.2.17
0.9.5.2
1.1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.mchange
c3p0
${c3p0.version}
taglibs
standard
${taglibs.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
在ssm_web->resources下db.properties文件中配置mysql数据库(这里的数据库名,用户名,密码一定要记得修改!!!)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/paper?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
*.do
下面开始写java代码
1.bean包下新建类UserInfo.java
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String password;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "com.zhongruan.bean.UserInfo{" +
"id=" + id +
", password='" + password + '\'' +
", username='" + username + '\'' +
'}';
}
}
2.在dao包中新建接口 IUserDao.java
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public UserInfo doLogin(UserInfo userInfo);
public List findall();
public int adduser(UserInfo userinfo);
public int deleteuserbyid(int id);
public int updateuser(UserInfo userinfo);
public UserInfo getuser(int id);
}
3.在service包中新建接口 IUserService:
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public Boolean doLogin(UserInfo userInfo);
List findall();
int adduser(UserInfo userinfo);
int deleteuserbyid(int id);
int updateuser(UserInfo userinfo);
UserInfo getuser(int id);
}
4.在service包下新建impl包,并在此包中新建UserService.java
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public Boolean doLogin(UserInfo userInfo) {
UserInfo userInfo1=userDao.doLogin(userInfo);
if(null!=userInfo1){
return true;
}else {
return false;
}
}
@Override
public List findall() {
return userDao.findall();
}
@Override
public int adduser(UserInfo userinfo) {
return userDao.adduser(userinfo);
}
@Override
public int deleteuserbyid(int id) { return userDao.deleteuserbyid(id); }
@Override
public int updateuser(UserInfo userinfo){return userDao.updateuser(userinfo);}
@Override
public UserInfo getuser(int id) {
return userDao.getuser(id);
}
}
5.在controller包中新建 UserController.java
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(UserInfo userInfo){
ModelAndView mv=new ModelAndView();
Boolean flag=userService.doLogin(userInfo);
if(flag){
mv.setViewName("main");
}else{
mv.setViewName("../failer");
}
return mv;
}
@RequestMapping("/findAll.do")
public ModelAndView findall() {
List all = userService.findall();
ModelAndView mv=new ModelAndView();
mv.addObject("userInfos", all);
mv.setViewName("/user-list");//跳转到allUser
return mv;
}
@RequestMapping("/delete.do")
public String deleteuser(int id){
userService.deleteuserbyid(id);
return "redirect:findAll.do";//重定向
}
@RequestMapping("/save.do")
public String adduser(UserInfo userinfo){
userService.adduser(userinfo);
return "redirect:findAll.do";//重定向
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdateUser(int id){
UserInfo userInfo=userService.getuser(id);
ModelAndView mv=new ModelAndView();
mv.setViewName("/user-update");
mv.addObject("userInfo",userInfo);
return mv;
}
@RequestMapping("/update.do")
public String updateuser(UserInfo userinfo){
userService.updateuser(userinfo);
return "redirect:findAll.do";//重定向
}
}
6.在ssm_dao->resources->mapper包下新建 userMapper.xml
insert into userinfo(id,username,password) values (#{id},#{username},#{password})
delete from userinfo where id=#{id}
update userinfo
set username = #{username},password = #{password}
where id = #{id}
各页面设计如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
- login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
中软ETC用户管理系统
body class="hold-transition skin-blue sidebar-mini">
- user-add.jsp部分代码
用户管理 用户表单
- user-update.jsp部分代码
用户管理 用户表单
**