SSM框架整合入门案例

1:整体概述

2:整合步骤

2.1:创建数据库

create database ccut;

2.2:创建数据表-学生表

create table student(
    id int(11) not null auto_increment,
    name varchar(20) not null,
    primary key (id)
    )default charset=utf8;

2.3:插入数据

insert into student values(null,"jake");
insert into student values(null,"eric");
insert into student values(null,"tom");
insert into student values(null,"marry");

2.4:创建eclipse工程

SSM框架整合入门案例_第1张图片

2.5:导入我们需要的jar包放到lib目录下(下载地址)

SSM框架整合入门案例_第2张图片

2.6:创建实体类pojo

package com.ccut.pojo;

public class Student {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

2.6:创建StudentMapper接口对数据库的一些操作(在单独是Mybatits中,没这个,我们在Mybatis和Spring整合的时候,Spring通过包扫描,实例化一个类,完成相关的操作)

import com.ccut.pojo.Student;

/**
 * 
 *对数据的一些操作
 */
public interface StudentMapper {
	//查询
	public List list();
}

2.7:编写StudentMapper.xml文件(mybatits)





  

2.8:编写配置文件(我们将Mybatis和Spring整和,Session不用我们自己去创建了) applicationContext.xml




	

	
		
			com.mysql.jdbc.Driver
		
		
			jdbc:mysql://localhost:3306/ccut?useUnicode=true&characterEncoding=UTF-8
			

		
		
			root
		
		
			root
		
	

	
		
		
             
	
	
    
	
		
	
部分步骤解释
上面红色字体


Spring帮我们做了什么

SSM框架整合入门案例_第3张图片

SSM框架整合入门案例_第4张图片

现在上图的一切我们都交给了Spring了,
 
	
		
	
上面这个包扫描会通过动态代理的方式实例化一个接口,创建一个类,供我们使用

SSM框架整合入门案例_第5张图片


2.9:我们与Spring进行整合
   2.9.1:创建servletl
package com.ccut.service;

import java.util.List;

import com.ccut.pojo.Student;

public interface StudentService {
	List list();
}
2.9.2:创建实现类ServletImpl.class
package com.ccut.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ccut.mapper.StudentMapper;
import com.ccut.pojo.Student;
import com.ccut.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	StudentMapper studentMapper;

	@Override
	public List list() {
		return studentMapper.list();
	}

}
2.9.3:创建controller
package com.ccut.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ccut.pojo.Student;
import com.ccut.service.StudentService;

@org.springframework.stereotype.Controller
@RequestMapping("")
public class Controller {
	@Autowired
	StudentService studentService;
   // 被controller表示,自动装配StudentService实现StudentService(我们在配置文件做了这件事context:component-scan base-package="com.ccut.service"/>)
	@RequestMapping("listStudent")
	public ModelAndView listStudent() {
		ModelAndView modelAndView = new ModelAndView();
		// 业务逻辑的实现
		List ls = studentService.list();
		modelAndView.addObject("cs", ls);
		modelAndView.setViewName("listStudent");
		return modelAndView;
	}
}
2.9.4:编写web.xml


	
	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
	
		mvc-dispatcher
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:springMVC.xml
		
		1
	
	
		mvc-dispatcher
		/
	
	
2.9.5: 编写SpringMVC配置文件


	
	
	
		
	

	
	
	
	
	
		
		
		
	

2.9.10:编写Jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



Insert title here




    
id name
${c.id} ${c.name}
2.10.11:部署启动

SSM框架整合入门案例_第6张图片

3:流程整体分析

1:我们浏览器访问/listStudent

2:tomcat根据web.xml的配置信息,拦截到listStudent,将这个请求交给DispatcherServlet处理

3:Dispatcher根据SpringMVC的配置,将这个请求交由StudentController处理,Spring会将这个Controller实例化

4:实例化过程我们注入StudentService(自动装配实现servlice接口的实例,所以我们实现StudentServlceImpl)

5:实例化StudentServiceimpl我们有会注入StudentMapper,Mybatis帮我们创建


	
		
	

6:根据application的配置信息,将StudentMapper和StudentMapper.xml结合起来

7:这样Controller中拿到各个实例化的东西,得到数据

8:将数据放到cs上,根据视图返回相应的servlet中去

9:最后在servlet中显示数据

四:源代码下载









你可能感兴趣的:(javaweb)