栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM框架整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM框架整合

目录

1、新建Maven项目, 添加web的支持

2、导入相关的pom依赖

3、Maven资源过滤设置

4、建立基本结构和配置框架!

6、Spring层

7、SpringMVC层

8、编写Controller层

9、编写jsp

10、运行排错


1、新建Maven项目, 添加web的支持

2、导入相关的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.springframework
           spring-webmvc
           5.1.9.RELEASE
       
       
           org.springframework
           spring-jdbc
           5.1.9.RELEASE
       

3、Maven资源过滤设置

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

4、建立基本结构和配置框架!

4.1、tln.pojo

4.2、tln.dao

4.3、tln.service

4.4、tln.controller

4.5、mybatis-config.xml

4.6、applicationContext.xml,也就是Spring的配置文件spring.xml 

4.7、spring-dao.xml,整合dao层和连接数据库

4.8、spring-service.xml,整合service层

4.9、spring-mvc.xml,整合controller层 

5、Mybatis层编写

5.1、编写数据库表

create table `books`(
    `bookID` int(10) NOT NULL  AUTO_INCREMENT COMMENT '书id',
    `bookName` varchar(50) 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,'从删库到跑路');

5.2、IDEA连接数据库

5.3、编写Mybaties核心配置文件




    

    
    
        
        
    
    
    
        
        
        
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
    
    
        springmvc
        /
    
    
    
        encoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        encoding
        /*
    

2、配置spring-mvc.xml



    
    
    
    
    
    
    
    
    
    
    
    
    

8、Spring层

1、配置Spring整合MyBatis,这里数据源使用c3p0连接池,导入相关依赖,随后在spring-dao.xml文件中配置;

2、配置spring-dao.xml文件

首先创建数据库配置文件 database.properties,spring-dao中会进行数据库连接

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


  
  
  
  
  
      
      
      
      
      
      
      
      
      
      
      
      
      
  

  
  
      
      
      
      
  

  
  
  
      
      
      
      
  

3、spring整合service层



    
    
    
    
        
    
    
    
        
        
    
    

4、spring关联spring-dao和spring-service和spring-mvc




    
    
    
    

9、编写jsp




    
        
            
                
                    书籍列表--------显示所有书籍
                
            
        
    
    
        
            
                <%--                从数据库查询出来的,从list中遍历出来--%>
                
                    
书籍编号 书籍名称 书籍数量 书籍详情
${book.bookID} ${book.bookName} ${book.bookCounts} ${book.detail}

10、运行排错

当Bean找不到时:

        1、查看这个Bean注入是否成功(spring-service中已经将其注入到spring)

        2、junit单元测试,看代码是否能够查询出结果

public class BookServiceImplTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        BookService bookServiceImpl = (BookService) context.getBean("BookServiceImpl");
        for (Books books : bookServiceImpl.queryAll()) {
            System.out.println(books);
        }
    }
}

        3、问题出现在spring层,springMVC整合的时候没调用到service层的bean
             3.1、spring.xml中注入bean
             3.2、web.xml中,绑定配置文件时DispatchServlet应绑定spring.xml总配置文件路径

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755187.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号