需求分析:搭建ssm项目实现前台对后台数据库(还是老朋友student表)的操作。
idea版本:2020.3.4
三层架构: springmvc:视图层 spring:服务层 mybatis:持久层
1.新建工程
2.导入依赖
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
jsp-api
2.2.1-b03
provided
org.springframework
spring-webmvc
5.2.5.RELEASE
org.springframework
spring-tx
5.2.5.RELEASE
org.springframework
spring-jdbc
5.2.5.RELEASE
org.springframework
spring-aspects
5.2.5.RELEASE
com.fasterxml.jackson.core
jackson-core
2.9.0
com.fasterxml.jackson.core
jackson-databind
2.9.0
org.mybatis
mybatis-spring
1.3.1
org.mybatis
mybatis
3.5.1
mysql
mysql-connector-java
5.1.9
com.alibaba
druid
1.1.12
3.插件
src/main/java
**/*.properties
**/*.xml
false
maven-compiler-plugin
3.1
1.8
4.数据库准备(id自动递增)
5.项目包结构
6.Jdbc 属性配置文件 jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.passwd=1111
7.Spring 配置文件 applicationContext.xml
8.Springmvc 配置文件:springmvc.xml
9.mybatis.xml
10.web.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:conf/springmvc.xml
1
springmvc
*.do
contextConfigLocation
classpath:conf/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceRequestEncoding
true
forceResponseEncoding
true
characterEncodingFilter
/*
11.实体类 Student
package com.domain;
public class Student {
private Integer id;
private String name;
private String time;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", time='" + time + '\'' +
'}';
}
public Student(Integer id, String name, String time) {
this.id = id;
this.name = name;
this.time = time;
}
public Student() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
12.Dao 接口和 sql 映射文件
StudentDao:
package com.dao;
import com.domain.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List selectStudents();
}
StudentDao.xml
insert into student(name,time) values (#{name},#{time})
13.Service 接口和实现类
package com.service;
import com.domain.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
List findStudents();
}
package com.service.impl;
import com.dao.StudentDao;
import com.domain.Student;
import com.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
int nums=studentDao.insertStudent(student);
return nums;
}
@Override
public List findStudents() {
return studentDao.selectStudents();
}
}
14.处理器定义
StudentController
package com.controller;
import com.domain.Student;
import com.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
@RequestMapping("/student")
@Controller
public class StudentController {
@Resource
private StudentService service;
@RequestMapping("/add.do")
public ModelAndView addStudent(Student student){
int nums=service.addStudent(student);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("nums",nums);
modelAndView.setViewName("result");
return modelAndView;
}
@ResponseBody
@RequestMapping(value = "/find.do",produces = "text/plain;charset=utf-8")
public String findStudents(){
List students=service.findStudents();
return students.toString();
}
}
15.视图层
前端自己写啦,仅展示视图层add.do对应的jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
提示:input的name属性应该与Student对象的属性名一一对应,这样做在视图层springmvc框架会自动调用set方法帮我们把这些属性一一封装到参数对象之中。