SSM框架模板整合_增删改查

SSM框架模板整合_增删改查_第1张图片

简介

在学习完SSM三大架构以后,可能苦于不会整合,而在网上找寻实战项目但实战项目大多都过于复杂,不便于初学者理解,此案例为初学者提供ssm整合模板,并用书籍的增删改查展现基本功能。

案例展示

SSM框架模板整合_增删改查_第2张图片

准备

  • 框架:SSM
  • 数据库:MySQL8.0
  • 前端框架:Bootstrap快速搭 搭建JSP页面
  • 项目管理:Maven
  • 开发工具:Intellij IDEA
  • 开发环境:Windows

码云

开发流程

mysql数据库

建立ssm数据库并建立book表然后插入数据。

CREATE DATABASE `ssm`;

USE `ssm`;

DROP TABLE IF EXISTS `book`;

CREATE TABLE `book` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT  INTO `book`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

sql文件
或者直接连接我的远程数据库 sql.wsfdb.cn:3306/5201314ssm 密码:123456
如果用远程数据库请更改database.properties文件

项目结构

此案例是哟个Maven项目项目结构为
SSM框架模板整合_增删改查_第3张图片

环境搭建

maven的pom.xml



    4.0.0

    org.example
    ssm
    1.0-SNAPSHOT

    
        8
        8
    
    
        
        
            junit
            junit
            4.12
        
        
        
            mysql
            mysql-connector-java
            8.0.25
        
        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp
            jsp-api
            2.2
        
        
            javax.servlet
            jstl
            1.2
        

        
        
            org.mybatis
            mybatis
            3.5.7
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
        
            org.springframework
            spring-webmvc
            5.3.9
        
        
            org.springframework
            spring-jdbc
            5.3.9
        

        
        
            org.projectlombok
            lombok
            1.18.20
        

    


    
        
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
    

数据库相关信息配置database.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456

mybatis-config.xml




    
    
        
    
    
        
        
    
    
        
        
    

spring-dao.xml




    
    
    

    
    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
    
        
        
        
        
    


spring-service.xml




    
    


    
    
        
        
    


spring-mvc.xml




    
    
    
    
    

    
    
        
        
        
    

    
    


applicationContext.xml




    
    
    


代码编写

前端

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    首页
    


点击进入列表页

allBook.jsp

<%--
  Created by IntelliJ IDEA.
  User: 26365
  Date: 2021/8/19 0019
  Time: 23:59
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    书籍列表
    
    
    



书籍编号 书籍名字 书籍数量 书籍详情 操作
${book.getBookID()} ${book.getBookName()} ${book.getBookCounts()} ${book.getDetail()} 更改 | 删除

addBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>



    新增书籍
    
    
    


书籍名称:


书籍数量:


书籍详情:


updateBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    修改信息
    
    
    


书籍名称: 书籍数量: 书籍详情:

后端

实体类 Book.java

package top.wx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

dao层 BookMapper.java BookMapper.xml

package top.wx.mapper;

import top.wx.pojo.Book;

import java.util.List;

public interface BookMapper {
    //增加一个Book
    int addBook(Book book);

    //根据id删除一个Book
    int deleteBookById(int id);

    //更新Book
    int updateBook(Book books);

    //根据id查询,返回一个Book
    Book queryBookById(int id);

    //查询全部Book,返回list集合
    List queryAllBook();

    //按bookName查询,返回一个Book
    Book queryBookByName(String bookName);
}





    
    
        insert into book(bookName,bookCounts,detail)
        values (#{bookName}, #{bookCounts}, #{detail})
    

    
    
        delete from book where bookID=#{bookID}
    

    
    
        update book
        set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
        where bookID = #{bookID}
    

    
    

    
    

    

sevice层 BookService.java BookServiceImpl.java

package top.wx.service;

import top.wx.pojo.Book;

import java.util.List;

//BookService:底下需要去实现,调用dao层
public interface BookService {
    //增加一个Book
    int addBook(Book book);

    //根据id删除一个Book
    int deleteBookById(int id);

    //更新Book
    int updateBook(Book book);

    //根据id查询,返回一个Book
    Book queryBookById(int id);

    //查询全部Book,返回list集合
    List queryAllBook();
    //按bookName查询,返回book
    Book queryBookByName(String bookName);
}
package top.wx.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import top.wx.mapper.BookMapper;
import top.wx.pojo.Book;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    //调用dao层的操作,设置一个set接口,方便Spring管理
    private BookMapper bookMapper;
    @Autowired
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public int addBook(Book book) {
        return bookMapper.addBook(book);
    }

    public int deleteBookById(int id) {
        return bookMapper.deleteBookById(id);
    }

    public int updateBook(Book book) {
        return bookMapper.updateBook(book);
    }

    public Book queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    public List queryAllBook() {
        return bookMapper.queryAllBook();
    }

    public Book queryBookByName(String bookName) {
        return bookMapper.queryBookByName(bookName);
    }
}

控制层 BookController.java

package top.wx.controller;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import top.wx.pojo.Book;
import top.wx.service.BookService;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Qualifier("bookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model) {
        List list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }

    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addPaper(Book book) {
        System.out.println(book);
        bookService.addBook(book);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
        Book books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("book", books);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model, Book book) {
        System.out.println(book);
        bookService.updateBook(book);
        Book books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/queryBookByName")
    public String queryBookByName(String bookName, Model model) {
        List list = new ArrayList<>();
        Book book = bookService.queryBookByName(bookName);
        list.add(book);
        if (bookName == null||book==null) {
            list = bookService.queryAllBook();
        }
        model.addAttribute("list", list);
        return "allBook";
    }
}

运行

http://localhost:8080/

你可能感兴趣的:(java,ssm,java,开发语言,后端)