写在前面,在昨天已经学习过了ssm框架,也能够创建出基本的框架,但是对于具体的代码实现却不太懂,通过今天的学习,在前面的基础上,已经能够实现数据库简单的增删改查功能了。
首先框架也是昨天的框架,这里先给出我所有创建的包和类等各种文件:
下面从导入jar包和配置环境开始(会附带具体的代码实现)
首先是pom.xml,这个文件主要是用来导入jar包的,代码实现如下:
4.0.0
com.SX
ssm0628
1.0-SNAPSHOT
war
ssm0628 Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
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
ssm0628
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
接下来配置系统环境(web.xml):
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
然后是配置spring环境(applicationContext):
接着配置spring-mvc环境(spring-mvc):
接着连接数据库(db.properties):注意这里替换成自己的数据库信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/idea?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=caiyuan
最后就是日志文件了(log4j.properties):
# 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
下面开始写代码实现了,首先在bean中建立实体类user,如下:
package com.SX.bean;
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
先在userdao中建立操作对数据库操作的接口
package com.SX.dao;
import com.SX.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public List findAll();
public int addUser(UserInfo userInfo);
public int deleteUser(int id);
public List findOnebyID(int id);
public int updateUser(UserInfo userInfo);
}
完成之后开始在mapper中的usermapper文件中实现对数据库的操作,如下所示:
insert into userinfo(username,password) value (#{username},#{password})
delete from userinfo where id=#{id}
update userinfo set username=#{username},password=#{password} where id=#{id}
紧接着就是增删改查这些操作的接口以及实现了(service层里面的)
package com.SX.service;
import com.SX.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List findAll();
public int addUser(UserInfo userInfo);
public int deleteUser(int id);
public List findOnebyID(int id);
public int updateUser(UserInfo userInfo);
}
package com.SX.service.serviceimpl;
import com.SX.bean.UserInfo;
import com.SX.dao.IUserDao;
import com.SX.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
IUserDao userDao;
@Override
public List findAll() {
return userDao.findAll();
}
@Override
public int addUser(UserInfo userInfo) {
return userDao.addUser(userInfo);
}
@Override
public int deleteUser(int id) {
return userDao.deleteUser(id);
}
@Override
public List findOnebyID(int id) {
return userDao.findOnebyID(id);
}
@Override
public int updateUser(UserInfo userInfo) {
return userDao.updateUser(userInfo);
}
}
然后根据给的jsp文件实现网页的跳转(jsp文件不是自己写的,这里也给出代码)
初始界面(index):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
Hello World!
查询所有用户
查询所有用户(alluser):
<%@ 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" %>
user列表
增加用户(adduser):
<%@ 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" %>
新增用户
基于SSM框架的管理系统:简单实现增、删、改、查。
新增用户
修改用户(updateuser):
<%@ 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" %>
修改用户
基于SSM框架的管理系统:简单实现增、删、改、查。
修改用户
所有jsp网页写完之后就能直接通过controller来控制跳转完成整个项目了。
controller的代码实现如下:
package com.SX.controller;
import com.SX.bean.UserInfo;
import com.SX.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("/findAll.do")
public ModelAndView findAll()
{
List userInfos=userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("ui",userInfos);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("/toAddUser.do")
public String toAddUser()
{
return "addUser";
}
@RequestMapping("/save.do")
public String addUser(UserInfo userInfo)
{
userService.addUser(userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("/delete.do")
public String deleteUser(int id)
{
userService.deleteUser(id);
return "redirect:/user/findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id)
{
List userInfo=userService.findOnebyID(id);
ModelAndView mv=new ModelAndView();
mv.addObject("userInfo",userInfo.get(0));
mv.setViewName("updateUser");
return mv;
}
@RequestMapping("/update.do")
public String updateUser(UserInfo userInfo)
{
userService.updateUser(userInfo);
return "redirect:/user/findAll.do";
}
}
到这里就实现了ssm框架下的简单数据库的增删改查操作了。
下面给出几个完成之后操作的结果图:
运行程序:
查询所有用户:
增加用户:
增加用户完成:
修改用户:
修改用户完成:
删除用户: