【ssm】配置地狱ssm整合

【ssm】配置地狱ssm整合,第1张

【ssm】配置地狱ssm整合

记录一下ssm整合的步骤

搭建数据库导入依赖静态资源导出连接数据库创建包与核心配置文件编写实体类Mapper接口Servise接口完善数据库配置修改web.xml配置文件将所有的spring的xml导入到总配置中

环境:

IDEA 2021.2.4MySQL 8.0.22Tomcat 8.5.73Maven 3.8.4

搭建数据库
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,'SUN公司开发的一种面向对象语言'),
(2,'MySQL',1,'热门的关系型数据库'),
(3,'Linux',1,'轻量级电脑系统');
导入依赖

Junit
数据库驱动
连接池
servlet
jsp
MyBatis
mybatis-spring
spring


    
        junit
        junit
        4.12
        test
    
    
        mysql
        mysql-connector-java
        8.0.22
    
    
        javax.servlet
        servlet-api
        2.5
    
    
        javax.servlet.jsp
        jsp-api
        2.2
    
    
        javax.servlet
        jstl
        1.2
    
    
        org.mybatis
        mybatis
        3.5.6
    
    
        org.mybatis
        mybatis-spring
        2.0.6
    
    
        org.springframework
        spring-webmvc
        5.3.3
    
    
        org.springframework
        spring-jdbc
        5.3.3
    
    
        com.mchange
        c3p0
        0.9.5.2
    
        
            org.projectlombok
            lombok
            1.18.16
        

静态资源导出

    
        
            src/main/resources
            
                ***.xml
            
            true
        
        
            src/main/java
            
                ***.xml
            
            true
        
    

连接数据库

创建包与核心配置文件

包结构如下:

applicationContext.xml(作为整合配置文件的总文件)




mybatis-config.xml(作为Mybatis的配置文件)




    
        
    

database.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=
编写实体类

Books.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}
Mapper接口

BookMapper.java

public interface BookMapper {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(@Param("bookID") int id);
    //更新一本书
    int updateBook(Books books);
    //查询一本书
    Books queryById(@Param("bookID") int id);
    //查询全部书
    List queryAllBook();
}

实现BookMapper借接口

BookMapper.xml




    
        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};
    

    
        select * from ssmbuild.books;
    


注册接口

mybatis-config.xml





    
        
    

    
        
    

Servise接口

BookService.java

public interface BookService {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(int id);
    //更新一本书
    int updateBook(Books books);
    //查询一本书
    Books queryById(int id);
    //查询全部书
    List queryAllBook();
}

实现service接口

BookServiceImpl.java

package com.paramaki.service;

import com.paramaki.dao.BookMapper;
import com.paramaki.pojo.Book;

import java.util.List;

public class BookServiceImpl implements BookService{

    // service层调dao层
    private BookMapper bookMapper;

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

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

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

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

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

    @Override
    public List queryAllBook() {
        return bookMapper.queryAllBook();
    }
}
完善数据库配置

spring-mapper.xml




    
    

    
    
        
        
        
        

        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
        
        
    


spring-service.xml




    
    

    
    
        
    

    
    
        
        
    

    


然后添加web支持

修改web.xml配置文件

web.xml



    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext.xml
        
        1
    
    
        springmvc
        /
    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        encodingFilter
        /*
    
    
        15
    

spring-mvc.xml




    
    
    
    
    
    
    
    
        
        
    


将所有的spring的xml导入到总配置中

ApplicationContext.xml




    
    
    


到这里,就整合结束了,还是比较折磨人的

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5703451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存