IDEA SSM框架整合配置及步骤详解

参考

狂神说SpringMVC05:整合SSM框架

https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247484004&idx=1&sn=cef9d881d0a8d7db7e8ddc6a380a9a76&scene=19#wechat_redirect

前言

根据自己的环境参考狂神的视频进行了SSM框架整合,用于备忘

SSM框架整合步骤

1. 创建数据库

IDEA SSM框架整合配置及步骤详解_第1张图片

2. IDEA创建maven项目.在pom.xml中设设置java conpiler版本(jdk13)

IDEA SSM框架整合配置及步骤详解_第2张图片

3. 导入依赖 jnuit 数据库驱动 数据库连接 Severlet JSp

4. maven 资源过滤



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

 org.example
 ssm
 1.0-SNAPSHOT
 
  13
  13
 
 
  
  
   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.6
  

  
  
   org.springframework
   spring-webmvc
   5.3.4
  
  
   org.springframework
   spring-jdbc
   5.3.4
  
 

5. 创建两个项目的目录结构以及Spring 和 Mybatis的配置文件

IDEA SSM框架整合配置及步骤详解_第3张图片








Mybatis

6. 配置数据库属性

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

7. 创建Book实体类(使用lombok插件),编写Dao层mapper接口,在mybatis配置中注册mapper

package com.projectname.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;

}
package com.projectname.dao;

import com.projectname.pojo.Book;

import java.util.List;

public interface DaoBook {
 //增加一个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();
}





 
 
  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}
 

 
 

 
 







8. 编写service层的接口和实现类代码

package com.projectname.service;

import com.projectname.pojo.Book;

import java.util.List;

public interface BookService {
 //增加一个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();
}
package com.projectname.service;

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

import java.util.List;

public class BookServiceImpl implements BookService {

 //调用dao层的操作,设置一个set接口,方便Spring管理
 private BookMapper bookMapper;

 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 books) {
  return bookMapper.updateBook(books);
 }

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

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

Spring

9. 使用Spring编写Mybatis配置文件:spring-dao.xml



 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
 
 
  
  
  
  
 
 
 
 
  
  
  
  
 

10. Spring 整合service层

spring-service




 
 

 
 
  
 

 
 
  
  
 

Spring MVC层

11. configuate the 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
 

12. spring-mvc.xml



 
 
 
 
 

 
 
  
  
  
 
 
 

13. configuate applicationContext.xml




 
 
 

一些杂七杂八的配置

14. 添加日志功能


 

15. add lib

File -> Project Structure->Artifacts
Type -> Bundle

new directory lib

IDEA SSM框架整合配置及步骤详解_第4张图片

到此这篇关于IDEA SSM框架整合配置及步骤详解的文章就介绍到这了,更多相关IDEA SSM框架整合配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(IDEA SSM框架整合配置及步骤详解)