Java 进阶必备之ssm框架全面整合

1.导入依赖

   
        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.springframework
        spring-webmvc
        5.1.9.RELEASE
    
    
    
        org.springframework
        spring-jdbc
        5.1.9.RELEASE
    
 
    
    
        org.springframework
        spring-context
        5.1.9.RELEASE
    
 
    
        com.fasterxml.jackson.core
        jackson-databind
        2.12.1
    
          
    
        com.alibaba
        druid-spring-boot-starter
        1.1.10
    
 

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

2.创建实体类

package pojo;
 
public class books {
    private int bookId;
    private String bookName;
    private int bookCounts;
    private String detail;
 
    public books(int bookId, String bookName, int bookCounts, String detail) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }
 
    public books() {
    }
 
    public int getBookId() {
        return bookId;
    }
 
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
 
    public String getBookName() {
        return bookName;
    }
 
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
 
    public int getBookCounts() {
        return bookCounts;
    }
 
    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }
 
    public String getDetail() {
        return detail;
    }
 
    public void setDetail(String detail) {
        this.detail = detail;
    }
}

3.写dao层接口

这里暂时只有一个方法

package Dao;
 
import pojo.books;
 
import java.util.List;
 
public interface BooksMapper {
    List selectbooks();
}

4.写mybatis核心配置文件和接口配置文件

这个是接口配置文件





 

 
 

mybatis核心配置文件




    
    
        
    

5.用spring整合Mybatis层也就是Dao层

这个是spring整合Mybatis的xml文件



 
    
    
    
 
    
    
    
        
        
        
        
        
 
        
        
        
        
        
        
        
        
        
    
 
    
    
        
        
        
        
    
 
 
    
    
        
        
        
        
    
 

数据库配置文件,这里你们改一下数据库就OK

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild
jdbc.username=root
jdbc.password=root

6.spring整合Service层

写接口和实现类

package Service;
 
import org.springframework.stereotype.Service;
import pojo.books;
 
import java.util.List;
 
public interface BooksService {
        List selectbooks();
    }
 

实现类

 @Service
 public class BooksServicelmpl implements BooksService{
   /*这里是把Dao的接口引进来了因为Service层调用Dao层*/
     @Autowired
    private BooksMapper booksMapper;
    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }
    @Override
    public List selectbooks() {
        return booksMapper.selectbooks();
    }
}

写spring配置文件



 
    
    
    
    
        
    
 

关联spring配置文件,我们写个总的配置文件到进去就ok



 
    
    

7.spring整合Conteoller层



 
    
    
    
    
    
    
 
 
    
    
        
        
        
        
    
 
    
        
            
                
            
            
                
                    
                        
                    
                
            
        
    
 
 

写Controller的类

@RestController
public class bookController {
    @Autowired
    private BooksService booksService;
    public void setBooksService(BooksService booksService) {
        this.booksService = booksService;
    }
    @RequestMapping("/books")
    public List selectbooks()
    {
        List list = booksService.selectbooks();
        return list;
    }
}

把三个配置文件关联



 
    
    
    

8.添加web支持

Java 进阶必备之ssm框架全面整合_第1张图片

Java 进阶必备之ssm框架全面整合_第2张图片

写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
    
 

创建一个lib包把依赖导进去

Java 进阶必备之ssm框架全面整合_第3张图片

添加工件测试

Java 进阶必备之ssm框架全面整合_第4张图片

9.完整的目录结构

Java 进阶必备之ssm框架全面整合_第5张图片

到此这篇关于Java 进阶必备之ssm框架全面整合的文章就介绍到这了,更多相关Java ssm框架内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Java 进阶必备之ssm框架全面整合)