使用SSM框架实现简单的增删改查

实现增删改查的流程

要完成一个功能:

先写实体类entity,定义对象的属性,(可以参照数据库中表的字段来设置,数据库的设计应该在所有编码开始之前)。
写Mapper.xml(Mybatis),其中定义你的功能,对应要对数据库进行的那些操作,比如 insert、selectAll、selectByKey、delete、update等。
写Mapper.java,将Mapper.xml中的操作按照id映射成Java函数。
写Service.java,为控制层提供服务,接受控制层的参数,完成相应的功能,并返回给控制层。
写Controller.java,连接页面请求和服务层,获取页面请求的参数,通过自动装配,映射不同的URL到相应的处理函数,并获取参数,对参数进行处理,之后传给服务层。
写JSP页面调用,请求哪些参数,需要获取什么数据。
DataBase ===> Entity ===> Mapper.xml ===> Mapper.Java ===> Service.java ===> Controller.java ===> Jsp.

准备条件

新建Maven项目,项目结构如图所示使用SSM框架实现简单的增删改查_第1张图片
在新建项目之前,先设计好数据库
例如:
使用SSM框架实现简单的增删改查_第2张图片
新建Maven项目

配置pom.xml文件

如下:

`


  4.0.0

  com.zhongruan
  ssm1
  1.0-SNAPSHOT
  war

  ssm1 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
  

    
      
      junit
      junit
      4.11
      test
    
      
      
        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
      

    


  
    ssm1
    
      
        
          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
        
      
    
  

等待自动下载jar包

1、建立实体类

package com.ssm1.bean;

public class UserInfo {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2、创建Dao接口

package com.ssm1.dao;

import com.ssm1.bean.UserInfo;

import java.util.List;

public interface IUserDao {
    public List findAll();
    public int addUser(UserInfo userInfo);
    public int updateUser(UserInfo userInfo);
    public  UserInfo queryById(int id);
    public int deleteUserById(int id);
}

3、在mapper.xml文件中实现接口





    
    
        INSERT INTO userinfo(id,username,password) VALUE (#{id},#{username}, #{password})
    
    
    UPDATE userinfo
    SET id = #{id},username = #{username}, password= #{password}
    WHERE  id = #{id}

    
    
        DELETE FROM userinfo WHERE id=#{id}
    

        

4、配置文件

applicationContext.xml



    
    

    
    
        
        
        
        
        
        
    

    
    
        
        
        
        

        
        

    

    
    
    
        
        
        
        
    



    
    


    
    
    
        
    
    
    


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=pass1234

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

spring-mvc.xml




    
    

    
    
    

    
    
        
        
    

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
  


5、service层

定义接口IUserService

package com.ssm1.service;

import com.ssm1.bean.UserInfo;

import java.util.List;

public interface IUserService {
    public List findAll();
    public int addUser(UserInfo userInfo);
    public int updateUser(UserInfo userInfo);
    public UserInfo queryById(int id);
    public int deleteUserById(int id);
}

实现接口

import com.ssm1.dao.IUserDao;
import com.ssm1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UsrServiceImpl  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 updateUser(UserInfo userInfo) {
        return  userDao.updateUser(userInfo);
    }

    @Override
    public UserInfo queryById(int id) {
        return userDao.queryById(id);
    }

    @Override
    public int deleteUserById(int id) {
       return userDao.deleteUserById(id);
    }

}


6、实现控制层

实现类Userconntroller

package com.ssm1.controller;

import com.ssm1.bean.UserInfo;
import com.ssm1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.enterprise.inject.Model;
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 ModelAndView toAddUser(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("addUser");//设置跳转的页面
        return mv;
    }
    @RequestMapping("/toUpdate.do")
    public ModelAndView toUpdate(int id){
        ModelAndView mv = new ModelAndView();
        mv.addObject("updateid",id);
        mv.setViewName("updateUser");//设置跳转的页面
        return mv;
    }
    @RequestMapping("/save.do")
    public String AddUser(UserInfo userInfo){
        userService.addUser(userInfo);
        return "redirect:/user/findAll.do";
    }

    @RequestMapping("/update.do")
    public String UpdateUser(UserInfo userInfo){
       // UserInfo User1=new UserInfo();
     // User1= userService.queryById(userInfo.getId());
        userService.updateUser(userInfo);
        //model.addAttribute("paper", paper);
        return "redirect:/user/findAll.do";
    }
    @RequestMapping("/delete.do")
    public String DeleteUserById(int id){
        userService.deleteUserById(id);
        return "redirect:/user/findAll.do";
    }
}

7、添加jsp页面

8、配置tomcat,并启动

Run–editconfiguratiion–添加tomcat–选择本地local–使用SSM框架实现简单的增删改查_第3张图片
选择tomcat
在Deployment下添加,如图所示
使用SSM框架实现简单的增删改查_第4张图片
最后,点击Apply,点击ok,启动tomcat
页面如图所示:
使用SSM框架实现简单的增删改查_第5张图片
完成!

你可能感兴趣的:(使用SSM框架实现简单的增删改查)