整合SSM

什么是SSM?

spring             业务层   逻辑(声明式事务)

springMVC     表示层   跟用户交互

mybatis           持久层   对数据库操作

整合思路

使用spring(容器)来整合springMVC和mybatis

具体实现步骤

1.先各自搭建SSM的环境

1.1 搭建mybatis环境

①创建数据库和数据表,准备数据环境

-- 创建学生表
CREATE TABLE student(
	number VARCHAR(10) UNIQUE,   -- 学号
	name VARCHAR(10),            -- 姓名
	birthday DATE,               -- 生日
	address VARCHAR(200)         -- 地址
);
-- 添加数据
INSERT INTO student VALUES ('hm001','张三','1995-05-05','北京市昌平区');
INSERT INTO student VALUES ('hm002','李四','1996-06-06','北京市海淀区');
INSERT INTO student VALUES ('hm003','王五','1997-07-07','北京市朝阳区');
INSERT INTO student VALUES ('hm004','赵六','1998-08-08','北京市丰台区');
INSERT INTO student VALUES ('hm005','周七','1999-09-09','北京市顺义区');
INSERT INTO student VALUES ('hm006','孙悟空','2000-01-01','花果山水帘洞');
INSERT INTO student VALUES ('hm007','猪八戒','2001-02-02','高老庄翠兰家');
INSERT INTO student VALUES ('hm008','沙和尚','2002-03-03','流沙河河底');
INSERT INTO student VALUES ('hm009','唐玄奘','2003-04-04','东土大唐');

②创建web项目,导入依赖



        
        
            mysql
            mysql-connector-java
            5.1.6
        
		
            com.alibaba
            druid
            1.1.15
        
        
            org.mybatis
            mybatis
            3.5.1
        


        
        
            org.springframework
            spring-context
            5.1.6.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.6.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.7
        
        
            org.springframework
            spring-test
            5.1.6.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.1.6.RELEASE
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
        


        
        
            org.mybatis
            mybatis-spring
            2.0.5
        

        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.8
        

        
        
            junit
            junit
            4.12
        
		
		  org.projectlombok
		  lombok
		  1.18.22
		
        
            log4j
            log4j
            1.2.17
        
    

整合SSM_第1张图片

③创建实体类

package com.ning.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private String number;//学号
    private String name;//姓名

    //手动指定json转换格式 设置好日期格式和时区
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
    private Date birthday;//生日
    private String address;//住址

 整合SSM_第2张图片

 ④创建mapper

package com.ning.mapper;

import com.ning.domain.Student;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {

    //查询所有
    @Select("select * from student ")
    List findAll();
}

整合SSM_第3张图片

⑤ 加入配置文件




    
    
    
    
    
        
            
            
            
            
                
                
                
                
            
        
    
    
    
    
        
    

整合SSM_第4张图片

⑥加入日志配置文件

### 设置###
log4j.rootLogger = debug,stdout

### è¾åºä¿¡æ¯å°æ§å¶æ¬ ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

整合SSM_第5张图片

 ⑦测试

package com.ning.test;

import com.ning.domain.Student;
import com.ning.mapper.StudentMapper;
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.Test;

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

public class StudentMapperTest {

    @Test
    public void testFindAll() throws IOException {
        //1.读取配置文件,将配置文件读成数据流
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        //2.获取sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //3.获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //4.获取Mapper,执行操作
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List studentList = mapper.findAll();
        for (Student student : studentList) {
            System.out.println(student);
        }

        //5.提交事务
        sqlSession.commit();

        //6.释放资源
        sqlSession.close();
    }
}

整合SSM_第6张图片

1.2 搭建Spring环境  

①创建service接口

package com.ning.service;

import com.ning.domain.Student;

import java.util.List;

public interface StudentService {

    //查询全部
    List findAll();
}

整合SSM_第7张图片

 ②创建service实现类

package com.ning.service.impl;

import com.ning.domain.Student;
import com.ning.mapper.StudentMapper;
import com.ning.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    //todo 这块代码暂时缺失,等到spring和mybatis整合完毕,就可以使用了
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List findAll() {
        //todo 这块代码暂时缺失,等到spring和mybatis整合完毕,就可以使用了
        //return studentMapper.findAll();
        System.out.println("查询成功");
        return null;
    }
}

整合SSM_第8张图片

 ③加入配置文件





    
    

整合SSM_第9张图片

④测试

package com.ning.test;

import com.ning.domain.Student;
import com.ning.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class StudentServiceTest {
    @Autowired
    private StudentService studentService;

    @Test
    public void testFindAll() {
        List studentList = studentService.findAll();
        if (studentList != null) {
            for (Student student : studentList) {
                System.out.println(student);
            }
        }
    }
}

 整合SSM_第10张图片

1.3 搭建SpringMVC环境

① 创建配置文件springmvc.xml




    
    

    
    


整合SSM_第11张图片

 ②配置web.xml




	
	
		dispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
	
	
		dispatcherServlet
		/
	

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

整合SSM_第12张图片

 ③编写处理器

package com.ning.controller;

import com.ning.domain.Student;
import com.ning.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class StudentController {
    //todo 整合之后再来获取
    //@Autowired
    private StudentService studentService;
    
     @GetMapping("/user")
    public List findAll(){

        //todo 整合之后再来获取
        //List studentList = studentService.findAll();

        List studentList = new ArrayList();
        studentList.add(new Student("bm001","网三",new Date(),"北京"));
        studentList.add(new Student("bm002","网四",new Date(),"北京"));
        return studentList;
    }
}

整合SSM_第13张图片

④测试

整合SSM_第14张图片

2.使用spring整合mybatis

2.1转移配置文件





    
    

    
    

    
    
        
        
        
        
    

    
    
        
    

    
    
        
    

    
    
        
    


整合SSM_第15张图片

 2.2 spring中注入mapper :就是把service的代码补充完整 

整合SSM_第16张图片

2.3 测试

整合SSM_第17张图片

3.使用spring整合springMVC

3.1 在web.xml配置一个监听器:在xml里,配置监视器


	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

整合SSM_第18张图片

 3.2 在controller中注入service:把controller里的代码补全 

整合SSM_第19张图片

3.3 测试:部署tomcat,测试

整合SSM_第20张图片

 

你可能感兴趣的:(Java,1024程序员节)