create database ccut;
create table student(
id int(11) not null auto_increment,
name varchar(20) not null,
primary key (id)
)default charset=utf8;
insert into student values(null,"jake");
insert into student values(null,"eric");
insert into student values(null,"tom");
insert into student values(null,"marry");
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;
}
}
import com.ccut.pojo.Student;
/**
*
*对数据的一些操作
*/
public interface StudentMapper {
//查询
public List list();
}
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/ccut?useUnicode=true&characterEncoding=UTF-8
root
root
package com.ccut.service;
import java.util.List;
import com.ccut.pojo.Student;
public interface StudentService {
List list();
}
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();
}
}
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;
}
}
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
mvc-dispatcher
/
<%@ 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}
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中显示数据