SSM整合增删查改

阅读更多

一:项目结构
SSM整合增删查改_第1张图片
   二:导入jar包

     pom.xml文件

 


  4.0.0
  BookManage
  BookManage
  war
  1.0-SNAPSHOT
  BookManage Maven Webapp
  http://maven.apache.org
  
    
    4.0.2.RELEASE
    
    3.2.6
    
    1.7.7
    1.2.17
  

  

    
    
      commons-dbcp
      commons-dbcp
      1.2.2
    
    
    
      javax
      javaee-api
      7.0
    
    
    
      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
    
    
      junit
      junit
      4.11
      test
    
    
    
      org.mybatis
      mybatis
      3.2.6
    


    
    
      mysql
      mysql-connector-java
      5.1.30
    
    
    
      org.codehaus.jackson
      jackson-core-asl
      1.9.13
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.1.0
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.1.0
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.1.0
    

    
    
      jstl
      jstl
      1.2
    



    
    
    
      log4j
      log4j
      ${log4j.version}
    
    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    

    
      commons-fileupload
      commons-fileupload
      1.2.1
    

    
    
      dom4j
      dom4j
      1.6
    

 

  
  
    BookManage
  

   三、配置文件

 

    web,xml文件

 



    
    
        useChinese
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        useChinese
        *.do
    
    
        contextConfigLocation
        classpath:config/mybatis.xml
    
    
        org.springframework.web.context.ContextLoaderListener
                                                                       
    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:config/mvc.xml
        
        1
    
    
        SpringMVC
        *.do
    
    
        login/index.jsp
    

    jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/bookmanage?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
#jdbc.password=111111
jdbc.password=root
#定义初始连接数
initialSize=0  
#定义最大连接数
maxActive=20  
#定义最大空闲
maxIdle=20  
#定义最小空闲
minIdle=1  
#定义最长等待时间
maxWait=60000  

   mvc.xml



    
    

    
    
    
    
        
        
    

   mybatis.xml



    
    
    
    
    
    
    
        
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
         ->
        
        
        
    
    
    
        
        
        
    
    
    
        
    
    

    

  四、定义数据库操作

 

   实体Test.class

 

package main.dao.entity;

/**
 * Created by swordbone on 2017/11/29.
 */
public class Test {
    private Integer id;
    private String name;
    private Integer age;
    private String nickname;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", nickName='" + nickname + '\'' +
                '}';
    }
}

 

 

    testMapper.xml文件

 




    
        
        
        
        
    
    
    
        INSERT INTO test
        
            
                ID,
            
            
                NAME,
            
            
                AGE,
            
            
                NICKNAME
            
        
        
            
                #{id,jdbcType=INTEGER},
            
            
                #{name,jdbcType=VARCHAR},
            
            
                #{age,jdbcType=INTEGER},
            
            
                #{nickname,jdbcType=VARCHAR}
            
        
    
    
        UPDATE test
        
            
                NAME=#{name},
            
            
                AGE=#{age,jdbcType=INTEGER},
            
            
                NICKNAME=#{nickname},
            
        
        WHERE ID=#{id}
    
    
        DELETE FROM test WHERE
        
            ID=#{id,jdbcType=INTEGER}
        
    

  在同一目录下创建dao接口,注意接口中的方法名与map.xml文件中的sql语句id相同,参数返回值都要对应

 

   TestMapper.class

 

package main.dao.idao;

import main.dao.entity.Test;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by swordbone on 2017/11/29.
 */
@Repository
public interface TestMapper {
    Integer createTest(Test test);
    List queryTest(Test test);
    Integer updateTest(Test test);
    Integer deleteTest(Integer id);
}

  五、service层

 

    service接口:TestService.class

 

package main.service.iService;

import main.dao.entity.Test;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by swordbone on 2017/11/29.
 */

public interface TestService {
    Integer createTest(Test test);
    List queryTest(Test test);
    Integer updateTest(Test test);
    Integer deleteTest(Integer id);
}

   service实现类:TestServiceImpl.class

package main.service.serviceImpl;

import main.dao.entity.Test;
import main.dao.idao.TestMapper;
import main.service.iService.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by swordbone on 2017/11/29.
 */
@Repository
public class TestServiceImpl implements TestService {
//自动装载dao接口
    @Autowired
    private TestMapper testMapper;
    public Integer createTest(Test test) {
        return testMapper.createTest(test);
    }

    public List queryTest(Test test) {
        return testMapper.queryTest(test);
    }

    public Integer updateTest(Test test) {
        return testMapper.updateTest(test);
    }

    public Integer deleteTest(Integer id) {
        return testMapper.deleteTest(id);
    }
}

 六、jsp页面

 

  新建test页面:create.jsp

 

<%--
  Created by IntelliJ IDEA.
  User: 老牛鼻子的山
  Date: 2017/11/30
  Time: 14:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    create page


create new
id:
name:
age:
nickname:

  查询界面:query.jsp

 

 

<%--
  Created by IntelliJ IDEA.
  User: 老牛鼻子的山
  Date: 2017/11/30
  Time: 10:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>


    query page



create
select id name age nickname update delete
${li.id} ${li.name} ${li.age} ${li.nickname} update delete

 编辑页面:updatepage.jsp

 

 

<%--
  Created by IntelliJ IDEA.
  User: 老牛鼻子的山
  Date: 2017/12/4
  Time: 11:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    updatepage


update one
id:
name:
age:
nickname:

  页面入口index.jsp

 

 



create

query

 

 

 

七、controller

  TestController.class

 

package main.controller.test;

import main.dao.entity.Test;
import main.service.iService.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by swordbone on 2017/11/29.
 */
@Controller
@RequestMapping("/test")
public class TestController {
//   自动装载TestService接口
    @Autowired
    TestService testService;
//  查询所有的test
    @RequestMapping("/query")
    public String queryTest(ModelMap map, HttpServletRequest request, Test test){
        List list = testService.queryTest(test);
        map.addAttribute("list",list);
        return null;
    }
//    新建test
    @RequestMapping("/create")
    public String createTest(ModelMap map,HttpServletRequest request,Test test){
        Integer returnnum = testService.createTest(test);
        map.addAttribute("returnnum",returnnum);
        return "redirect:/test/query.do";
    }
//    编辑test
    @RequestMapping("/{id}/update")
    public String updateTest (ModelMap map, HttpServletRequest request, 
                                                     Test test, @PathVariable String id){
        Integer intId = Integer.valueOf(id);
        Test test1 = new Test();
        test1.setId(intId);
        List list = testService.queryTest(test1);
        map.addAttribute("test",list.get(0));
        return "/test/updatepage";
    }
//    更改test
    @RequestMapping("/updateForm")
    public String updateForm(ModelMap map,HttpServletRequest request,Test test){
        Integer returnint = testService.updateTest(test);
        return "redirect:/test/query.do";
    }
//    删除test
    @RequestMapping("/{id}/delete")
    public String deleteTest(ModelMap map,HttpServletRequest request,
                                                    Test test,@PathVariable String id){
        Integer intId = Integer.valueOf(id);
        Integer returnnum = testService.deleteTest(intId);
        map.addAttribute("returnnum",returnnum);
        return "redirect:/test/query.do";
    }
}

 项目页面:

 

   index.jsp

SSM整合增删查改_第2张图片
   点击query跳转到query.do

SSM整合增删查改_第3张图片

 点击create跳转到create.jsp

SSM整合增删查改_第4张图片
  新建一条数据

SSM整合增删查改_第5张图片

SSM整合增删查改_第6张图片
 编辑
SSM整合增删查改_第7张图片

SSM整合增删查改_第8张图片

 

  

   

  • SSM整合增删查改_第9张图片
  • 大小: 24.1 KB
  • SSM整合增删查改_第10张图片
  • 大小: 20.1 KB
  • SSM整合增删查改_第11张图片
  • 大小: 20.3 KB
  • SSM整合增删查改_第12张图片
  • 大小: 15.4 KB
  • SSM整合增删查改_第13张图片
  • 大小: 15.4 KB
  • SSM整合增删查改_第14张图片
  • 大小: 19.2 KB
  • SSM整合增删查改_第15张图片
  • 大小: 16.7 KB
  • SSM整合增删查改_第16张图片
  • 大小: 17.2 KB
  • 查看图片附件

你可能感兴趣的:(SSM整合增删查改)