Java中SSM框架实现增删改查功能代码详解

网友投稿 471 2022-11-30


Java中SSM框架实现增删改查功能代码详解

记录一下自己第一次整合smm框架的步骤。

参考博客和网站有:我没有三颗心脏 How2J学习网站

1.数据库使用的是mysql,首先创建数据库ssm1,并创建表student

create database ssm1;

use ssm1;

CREATE TABLE student(

id int(11) NOT NULL AUTO_INCREMENT,

student_id int(11) NOT NULL UNIQUE,

name varchar(255) NOT NULL,

age int(11) NOT NULL,

sex varchar(255) NOT NULL,

birthday date DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.新建java web项目,命名为ssm1,并且导入相关的jar包。

3.建立pojo类,在这里命名为student,包名为com.ssm1.pojo

package com.ssm1.pojo;

public class Student {

private int id;

private int student_id;

private String name;

private int age;

private String sex;

private String birthday;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public int getStudent_id() {

return student_id;

}

public void setStudent_id(int student_id) {

this.student_id = student_id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getBirthday() {

return birthday;

}

public void setBirthday(String birthday) {

this.birthday = birthday;

}

}

4.建立映射器接口studentMapper,包名为com.ssm1.mapper

package com.ssm1.mapper;

import java.util.List;

import com.ssm1.pojo.Student;

public interface StudentMapper {

public int add(Student student);

public void delete(int id);

public Student get(int id);

public int update(Student student);

public List list();

}

5.建立与studentMapper对应的xml文件,同样属于包com.ssm1.mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

INSERT INTO student VALUES(#{student_id},#{name}, #{age}, #{sex}, #{birthday})

delete from student where id= #{id}

select * from student where id= #{id}

UPDATE student SET student_id = #{student_id}, name = #{name},

age = #{age}, sex = #{sex}, birthday = #{birthday} WHERE id = #{id}

select * from student

6.建立studentService接口,包名为com.ssm1.service

package com.ssm1.service;

import java.util.List;

import com.ssm1.pojo.Student;

public interface StudentService {

List list();

void add(Student s);

void delete(Student s);

void update(Student s);

Student get(int id);

}

7.建立studentServiceImpl类,实现接口,包名为com.ssm1.service

package com.ssm1.service;

import java.util.List;

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

import org.springframework.stereotype.Service;

import com.ssm1.mapper.StudentMapper;

import com.ssm1.pojo.Student;

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

StudentMapper studentMapper;

@Override

public List list() {

// TODO Auto-generated method stub

return studentMapper.list();

}

@Override

public void add(Student s) {

// TODO Auto-generated method stub

studentMapper.add(s);

}

@Override

public void delete(Student s) {

// TODO Auto-generated method stub

studentMapper.delete(s.getId());

}

@Override

public void update(Student s) {

// TODO Auto-generated method stub

studentMapper.update(s);

}

@Override

public Student get(int id) {

// TODO Auto-generated method stub

return studentMapper.get(id);

}

}

8.建立studentController控制器,包名为com.ssm1.controller

package com.ssm1.controller;

import java.util.List;

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

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import com.ssm1.pojo.Student;

import com.ssm1.service.StudentService;

import com.ssm1.util.Page;

@Controller

@RequestMapping("")

public class StudentController {

@Autowired

StudentService studentService;

@RequestMapping("/index")

public ModelAndView index(Page page) {

ModelAndView mav = new ModelAndView();

List cs = studentService.list();

mav.addObject("cs", cs);

mav.setViewName("index");

return mav;

}

@RequestMapping(value = "addStudent", produces = "text/html; charset=utf-8")

// @RequestMapping("addStudent")

public ModelAndView addStudent(Student student) {

studenthttp://Service.add(student);

ModelAndView mav = new ModelAndView("redirect:/index");

return mav;

}

@RequestMapping("deleteStudent")

public ModelAndView deleteStudent(Student student) {

studentService.delete(student);

ModelAndView mav = new ModelAndView("redirect:/index");

return mav;

}

@RequestMapping("editStudent")

public ModelAndView editStudent(Student student) {

Student s=studentService.get(student.getId());

ModelAndView mav=new ModelAndView("editStudent");

mav.addObject("s",s);

return mav;

}

@RequestMapping("updateStudent")

public ModelAndView updateStudent(Student student) {

studentService.update(student);

ModelAndView mav=new ModelAndView("redirect:/index");

return mav;

}

}

9.在WEB-INF目录下建立web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

mvc-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springMVC.xml

1

mvc-dispatcher

/

CharacterEncodingFilter&lhttp://t;/filter-name>

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

mvc-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springMVC.xml

1

mvc-dispatcher

/

CharacterEncodingFilter&lhttp://t;/filter-name>

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

10.在src目录下新建applicationContext.xml文件,这是Spring的配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/ssm1?characterEncoding=UTF-8

root

admin

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/ssm1?characterEncoding=UTF-8

root

admin

11.在src目录下新增springMVC.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.jstlView" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.jstlView" />

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.jstlView" />

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.jstlView" />

value="org.springframework.web.servlet.view.jstlView" />

12.在WEB-INF下创建jsp目录,并创建文件index.jsp和editStudent.jsp

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

pageEncoding="UTF-8" import="java.util.*"%>

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

学生学号:

学生姓名:

学生年纪:

学生性别:

学生生日:

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

pageEncoding="UTF-8" import="java.util.*"%>

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

分类名称:

分类名称:

分类名称:

分类名称:

分类名称:

13.最后在tomcat上部署项目,输入路径localhost:端口号/ssm1/index即可访问


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Jmeter如何基于命令行运行jmx脚本
下一篇:Servlet开发JavaWeb工程示例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~