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

SSM整合

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

SSM整合

1 搭建Spring环境 1.1 创建Maven工程并导入相关依赖 1.2 编写三层架构

1.3 编写applicationContext.xml



    
    
        
        
    

1.4 使用注解提交对象给容器管理

1.5 测试(Spring整合JUnit)

2 搭建SpringMVC环境 2.1 编写web.xml



    Archetype Created Web Application

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

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


2.2 编写springmvc.xml



    
    
        
    

    
    
        
        
        
        
    

    
    
    
    

    
    

2.3 编写控制器代码
package com.hc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping(value = "/findAll")
    public String findAll() {
        System.out.println("FindAll is running!");
        return "list";
    }
}

2.4 测试

3 Spring整合SpringMVC

目的:在controller中能成功的调用service对象中的方法。
方法:在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。步骤如下:

  • 1 配置ContextLoaderListener
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

注:配置监听器加载applicationContext.xml后,相当于启动了Spring的容器,那么Spring的注解就可以用了。

  • 2 在controller中注入service对象,调用service对象的方法进行测试
4 搭建MyBatis环境 4.1 编写SqlMapConfig.xml



    
    
        
        
    

    
    
        
        
    

    
    
        
            
            
                
                
                  
                
            
        
    

    
    
        
    

4.2 编写UserDao.xml



    
        select *
        from user;
    

注意包的目录结构:

4.3 测试
package com.hc;

import com.hc.dao.UserDao;
import com.hc.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class testMyBatis {

    private InputStream in;
    private SqlSessionFactoryBuilder builder;
    private SqlSessionFactory factory;
    private SqlSession session;
    private UserDao userDao;

    @Before
    public void init() throws Exception {
        // 1.从 XML 中构建 SqlSessionFactory
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        builder = new SqlSessionFactoryBuilder();
        factory = builder.build(in);
        // 2.使用SqlSessionFactory生产SqlSession对象
        session = factory.openSession(true);
        // 3.使用SqlSession创建dao接口的代理对象
        userDao = session.getMapper(UserDao.class);
    }

    @After
    public void destroy() throws Exception {
        // 关闭资源
        session.close();
        in.close();
    }

    @Test
    public void testFindAll() {
        List users = userDao.findAll();
        for (User u : users) {
            System.out.println(u);
        }
    }
}

5 Spring整合MyBatis

需要在service层调用MyBatis生成的代理对象。

5.1 applicationContext.xml整合SqlMapConfig.xml

目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中。




    
    
        
        
    

    
    
    
        
        
        
        
    
    
    
        
        
        
        
    
    
    
        
    

    
    
    
        
    

    
    
        
            
            
        
    

    
        
        
        
        
    

5.2 dao、service和controller层的数据注入



5.3 测试


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

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

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