Spring+SpringMVC+MyBatis整合

一、思路

SSH --> Spring+SpringMVC+MyBatis

SM整合 --> SS整合 -->SSM

Spring+SpringMVC+MyBatis整合_第1张图片

二、大体框架

Spring+SpringMVC+MyBatis整合_第2张图片

三、Spring整合MyBatis

SqlSessionFactory ->SqlSession -> StudentMapper ->CRUD

MyBatis最终通过SqlSessionFactory 来操作数据库,Spring整合MyBatis,其实就是将MyBatis的SqlSessionFactory交给Spring。

1、jar包

Spring+SpringMVC+MyBatis整合_第3张图片

2、配置

与spring整合时,mybatis的配置文件conf.xml(数据源+mapper.xml)可省,将其配置在applicationContext.xml中。




	
		
			
				classpath:db.properties
			
		
	

	
	
		
		
		
		
	 
	
	
	
		
		
	 

	
		
	
	
	
	
		  
		
		  
	

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=orcl
password=orcl

3、编写代码

(1)entity

package com.guor.entity;

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

	...
}

(2)mapper

package com.guor.mapper;

import com.guor.entity.Student;

public interface StudentMapper {
	public void addStudent(Student student);

	public Student queryStudentByStuNo(int id);
}



	
	
	
		insert into student(id,name,age) values (#{id},#{name},#{age})
	

(3)service(面向接口编程)

package com.guor.service;

import com.guor.entity.Student;

public interface IStudentService {
	public void addStudent(Student student);
	public Student queryStudentByStuNo(int id);
}
package com.guor.service.impl;

import com.guor.entity.Student;
import com.guor.mapper.StudentMapper;
import com.guor.service.IStudentService;

public class StudentServiceImpl implements IStudentService {
	private StudentMapper studentMapper;
	
	public void setStudentMapper(StudentMapper studentMapper) {
		this.studentMapper = studentMapper;
	}

	@Override
	public void addStudent(Student student) {
		studentMapper.addStudent(student);
	}

	@Override
	public Student queryStudentByStuNo(int id) {
		return studentMapper.queryStudentByStuNo(id);
	}
}

(4)controller

package com.guor.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.guor.entity.Student;
import com.guor.service.IStudentService;

@Controller
@RequestMapping("studentController")
public class StudentController {
	@Autowired
	@Qualifier("studentService")
	private IStudentService studentService;

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	@RequestMapping("queryStudentByStuNo/{id}")//映射
	public String queryStudentByStuNo(@PathVariable("id") Integer id,Map map) {
		Student student = studentService.queryStudentByStuNo(id);
		map.put("student", student);
		return "result";
	}
}

四、Spring整合SpringMVC

1、jar包

一个就行,spring-webmvc-4.3.9.RELEASE.jar

2、配置



	
	
	
	
	
	
		
		
	
	
	
	
	
	
		
			
				
			
		
	
	
		
			
				text/html;charset=UTF-8
			
		
	
	
	
		
		
			
				
				
					error
				
				
					error
				
			
		
	

3、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	queryStudentByStuNo

4、运行

Spring+SpringMVC+MyBatis整合_第4张图片

你可能感兴趣的:(SSM)