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

【整合SSM框架】

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

【整合SSM框架】

整合SSM框架
  • 1. SSM框架是哪三个
  • 2. SSM中的两个容器
  • 整合步骤:
    • 1. 新建maven项目,创建的是web项目
    • 2. 加入依赖(SpringMVC,Spring,Mybatis三个框架的依赖,还有jackson依赖,mysql驱动,driud连接池依赖,Jsp,Servlet依赖,等依赖)
    • 3. 配置web.xml文件,主要配置以下三个对象
    • 4. 创建包,Dao, Service, Entity ,Controller包
    • 5. 配置SpringMVC,Spring,Mybatis的配置文件
    • 6.数据库连接属性文件jdbc.properties
    • 7.写代码,mapper接口和Mapper文件,Service接口和它的实现类,Controller,实体类
  • 3. 效果截图

1. SSM框架是哪三个
  • Spring业务层:用于管理Service,dao,工具类等对象
  • SpringMVC:视图层,界面层,负责接收用户请求,显示处理结果
  • Mybatis持久层:用于访问数据库的,跟数据库进行对接
2. SSM中的两个容器
  • SSM整合也叫SSI(IBatis就是Mybatis的前身),在整合的过程中有两个容器
    1. 第一个容器SpringMVC容器,管理Controller控制器对象的
    2. 第二个容器Spring容器,管理Service,Dao,工具类对象的
    3. 我们要做的是将我们要使用的对象交给合适的容器去创建,管理,把Controller还有web开发的相关对象交给SpringMVC容器,这些web要用到对象写在SpringMVC的配置文件中,将Service,Dao对象定义在Spring的配置文件中,让Spring来管理这些对象。
    4. SpringMVC容器和Spring容器是有关系的,即SpringMVC容器是Spring容器的子容器,类似于java中的继承,在子容器中的controller是可以访问父容器中的Service对象,就可以实现Controller去使用Service对象。
整合步骤: 1. 新建maven项目,创建的是web项目

2. 加入依赖(SpringMVC,Spring,Mybatis三个框架的依赖,还有jackson依赖,mysql驱动,driud连接池依赖,Jsp,Servlet依赖,等依赖)

< !–设置jdk的版本为1.8–>
< properties >
UTF-8
1.8
1.8
< /properties >

此处使用的Spring 的版本为 5.3.16

 
    
    
      jstl
      jstl
      1.2
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
    
      org.springframework
      spring-webmvc
      5.3.16
    

    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    

    
    
      org.springframework
      spring-jdbc
      5.3.16
    

    
    
      org.springframework
      spring-tx
      5.3.16
    

    
    
      org.springframework
      spring-test
      5.3.16
    

    
    
      org.springframework
      spring-aspects
      5.3.16
    

    
    
      org.mybatis
      mybatis
      3.4.5
    

    
    
      org.mybatis
      mybatis-spring
      1.3.1
    

    
    
      mysql
      mysql-connector-java
      8.0.27
    
    
    
      junit
      junit
      4.12
      test
    

    
    
      com.alibaba
      druid
      1.1.12
    
  

  
    
    
      
        src/main/java
        
          ***.properties
        
      
      
        src/main/resources
        
          ***.properties
        
      
    
  
3. 配置web.xml文件,主要配置以下三个对象
1) SpringMVC核心对象**DispatcherServlet**对象
2) 配置**CharacterEncodingFilter**对象来解决中文乱码
3) 配置Spring监听器**ContextLoaderListener**,当tomcat一启动就创建Spring容器

web.xml文件:

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

    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
        
            encoding
            UTF-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        contextConfigLocation
        classpath:applicationContext*.xml
    
4. 创建包,Dao, Service, Entity ,Controller包

5. 配置SpringMVC,Spring,Mybatis的配置文件
  • SpringMVC配置文件,SpringMVC.xml:


    
    
    
    
    
    

  • Mybatis的配置文件,mybatis-config.xml:



    
        
        
        
        
    
    
    
        
    

  • Spring的配置文件我分成了两个,一个是专门管理Service层的applicationContext_service.xml,一个是专门管理mapper层或者叫dao层的applicationContext.xml

    1.applicationContext.xml文件



    
    
    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
        
        
    

  1. applicationContext_service.xml文件:


    
    
    
    
    
        
        
    
    
    
        
            
            
            
            

            
            
            

            
            
            

            
            
            
            
        
    
    
    
        
        
        
        
    

6.数据库连接属性文件jdbc.properties

按照自己的数据库连接信息进行填写即可

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test2?zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=root
7.写代码,mapper接口和Mapper文件,Service接口和它的实现类,Controller,实体类

假设:
1. 实体类:

package org.example.entity;
//用户类
public class User {
    private String userId;
    private String cardType;
    private String cardNo;
    private String userName;
    private String userSex;
    private String userAge;
    private String userRole;
    //无参构造,有参构造,set,get,toString方法已经省略...
    }

2.mapper接口和Mapper文件:

//mapper接口
public interface UserMapper {
   //查询所有用户
   List selectAll();
}




	

  1. Service接口以及实现类
//service接口
public interface UserService {
	//查询所有用户
	List findAll();
}
//service实现类
import org.example.dao.UserMapper.UserMapper;
import org.example.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
//@Service用于创建service实现类对象,默认名字叫做实现类首字母小写userServiceImpl
@Service
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserMapper userMapper;//业务层需要调用数据访问层的方法,所以将数据访问层对象作为成员变量
	
    @Override
    public List findAll() {
    //调用数据访问层的方法
        return userMapper.selectAll();
    }
}
  1. Controller层
@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	//控制器中需要调用业务层中的方法,所以需要业务层对象作为成员变量
   private UserService userServiceImpl;

	@RequestMapping("/findAll")
	@ResponseBody//该注解用于返回json数据,发送ajax请求时需要使用
	public List find(){
		return userServiceImpl.findAll();
	}
}
3. 效果截图

发送ajax请求返回的是json数组, 成功

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

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

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