Java 整合模板彻底解决ssm配置难题

Spring+SpringMVC+Mybatis

环境配置
IDEA
MySQL 5.7
Tomcat 8.5
Maven 3.6
创建数据库

CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books` (
`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 `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

Java 整合模板彻底解决ssm配置难题_第1张图片

编写数据库对应的实体类com.longdi.pojo.Books
导入lombok在pom.xml中

package com.longdi.pojo;

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

/**
 * @author: 龍弟
 * @description
 * @date: 2021/9/27 20:22
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

导入相关的pom依赖


        
        
            junit
            junit
            4.12
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            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.2
        
        
            org.mybatis
            mybatis-spring
            2.0.2
        
        
            org.aspectj
            aspectjweaver
            1.8.4
        
        
        
            org.springframework
            spring-webmvc
            5.1.9.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.9.RELEASE
        
        
            org.projectlombok
            lombok
            1.18.16
            compile
        
    

资源过滤设置


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

Mybatis层编写
创建mybatis-config.xml





    
        
    

    
        
    
    
        
    

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

编写Dao层的Mapper接口

package com.longdi.dao;

import com.longdi.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author: 龍弟
 * @description
 * @date: 2021/9/27 20:32
 */
public interface BookMapper {
    //增加一本书
    int addBook(Books books);

    //删除一本书
    int deleteBookById(@Param("bookId") int id);

    //更新一本书
    int updateBook(Books books);

    //查询一本书
    Books queryBookById(@Param("bookId")int id);

    //查询全部的书
    List queryAllBook();

    Books queryBookByName(@Param("bookName")String bookName);

}

编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;




    
       insert into ssmbuild.books(bookName,bookCounts,detail)
       values(#{bookName},#{bookCounts},#{detail});
    

    
        delete from ssmbuild.books where bookID=#{bookId}
    
    
    
        update ssmbuild.books
        set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
        where bookID=#{bookID};

    

    

    
    
    
    

编写Service层的接口

package com.longdi.service;

import com.longdi.pojo.Books;


import java.util.List;

/**
 * @author: 龍弟
 * @description
 * @date: 2021/9/27 21:03
 */
public interface BookService {
    //增加一本书
    int addBook(Books books);

    //删除一本书
    int deleteBookById( int id);

    //更新一本书
    int updateBook(Books books);

    //查询一本书
    Books queryBookById(int id);

    //查询全部的书
    List queryAllBook();

    Books queryBookByName(String bookName);
}

Service层的实现类

package com.longdi.service;

import com.longdi.dao.BookMapper;
import com.longdi.pojo.Books;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 龍弟
 * @description
 * @date: 2021/9/27 21:05
 */

public class BookServiceImpl implements BookService{

    private BookMapper bookMapper;
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

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

    public int updateBook(Books books) {
        System.out.println("BookServiceImpl:updateBook=>"+books);
        return bookMapper.updateBook(books);
    }

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

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

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

Spring整合Mybatis spring-dao.xml
以下为spring层




    
    
    

    
    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
        
        
        
    



spring-service.xml文件




    
    

    
    
        
    

    
    
        
        
    
    

    
    

    
        
            
        
    

    
    
        
        
    


以下为SpringMVC层
配置web.xml




    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            
            classpath:applicationContext.xml
        
        1
    
    
        DispatcherServlet
        /
    

    
    
        encodingFilter
        
            org.springframework.web.filter.CharacterEncodingFilter
        
        
            encoding
            utf-8
        
    
    
        encodingFilter
        /*
    

    
    
        15
    


spring-mvc.xml




    
    
    
    
    

    
    
        
        
        
    

    
    


Spring配置整合文件,创建applicationContext.xml




    
    
    


以上为SSM的环境配置,下面编写Controller层和视图层,实现书籍的CRUD功能

controller层 书籍的增删改查功能

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

/**
 * @author: 龍弟
 * @description
 * @date: 2021/9/27 22:47
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @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 addBook(Books books){
        System.out.println("addBook=>"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    //跳转到修改页面
    @RequestMapping("toUpdateBook")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("book",books);
        return "updateBook";
    }
    //修改书籍
    @RequestMapping("updateBook")
    public String updateBook(Books books){
        System.out.println("updateBook=>"+books);
        int i=bookService.updateBook(books);
        if(i>0){
            System.out.println("添加books成功"+books);
        }

        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/deleteBook/{bookId}")
    public String deleBook(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
         Books books=bookService.queryBookByName(queryBookName);
        System.err.println("queryBook=>"+books);
         List list=new ArrayList();
         list.add(books);

         if(books==null){
             list=bookService.queryAllBook();
             model.addAttribute("error","未查到");
         }

         model.addAttribute("list",list);
         return "allBook";
    }

}

编写首页index.jsp

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



  首页
  


进入到书籍页面

编写书籍列表页面allBook.jsp

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


  书籍列表
  
  
  



<%--查询书籍--%>
${error}
书籍编号 书籍名字 书籍数量 书籍详情 操作
${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" %>


    修改信息
    
    
    


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

Java 整合模板彻底解决ssm配置难题_第2张图片

Java 整合模板彻底解决ssm配置难题_第3张图片

Java 整合模板彻底解决ssm配置难题_第4张图片

项目部署到tomcat中

Java 整合模板彻底解决ssm配置难题_第5张图片

项目结构图

Java 整合模板彻底解决ssm配置难题_第6张图片

Java 整合模板彻底解决ssm配置难题_第7张图片

gitee网站下载源码
github网站下载源码

到此这篇关于Java 整合模板彻底解决ssm配置难题的文章就介绍到这了,更多相关Java ssm配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Java 整合模板彻底解决ssm配置难题)