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

 更新时间:2020年07月21日 15:59:06   作者:FrankYu  
这篇文章主要介绍了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<Student> list();

}

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ssm1.mapper.StudentMapper">

 <insert id="add" parameterType="Student">
 INSERT INTO student VALUES(#{student_id},#{name}, #{age}, #{sex}, #{birthday})
 </insert>
 <!-- <insert id="add" parameterType="com.ssm1.pojo.Student" useGeneratedKeys="true" keyProperty="id">
  insert into student
  <trim prefix="(" suffix=")" suffixOverrides="," >
  <if test="student_id!=null">
   student_id,
  </if>
  <if test="name!=null and name!=''">
   name,
  </if>
  <if test="age!=null">
   age,
  </if>
  <if test="sex!=null and sex!=''">
   sex,
  </if>
  <if test="birthday!=null and birthday !=''">
   birthday,
  </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides="," >
  <if test="student_id!=null">
   #{student_id},
  </if>
  <if test="name!=null and name!=''">
   #{name},
  </if>
  <if test="age!=null">
   #{age},
  </if>
  <if test="sex!=null and sex!=''">
   #{sex},
  </if>
  <if test="birthday!=null and birthday !=''">
   #{birthday},
  </if>
  </trim>
 </insert> -->


 <delete id="delete" parameterType="Student">
 delete from student where id= #{id}
 </delete>

 <select id="get" parameterType="_int" resultType="Student">
 select * from student where id= #{id}
 </select>

 <update id="update" parameterType="Student">
 UPDATE student SET student_id = #{student_id}, name = #{name},
 age = #{age}, sex = #{sex}, birthday = #{birthday} WHERE id = #{id}
 </update>
 <select id="list" resultType="Student">
 select * from student
 </select>
</mapper>

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

package com.ssm1.service;

import java.util.List;
import com.ssm1.pojo.Student;

public interface StudentService {
 List<Student> 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<Student> 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<Student> 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) {
 studentService.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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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">

 <!-- spring的配置文件-->
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>


 <!-- spring mvc核心:分发servlet -->
 <servlet>
 <servlet-name>mvc-dispatcher</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!-- spring mvc的配置文件 -->
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springMVC.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>mvc-dispatcher</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 <filter>
 <filter-name>CharacterEncodingFilter</filter-name>
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 <init-param>
 <param-name>encoding</param-name>
 <param-value>utf-8</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>CharacterEncodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 配置@Service包的扫描 -->
 <context:annotation-config />
 <context:component-scan base-package="com.ssm1.service" />
 <!-- 配置数据库的连接 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName">
  <value>com.mysql.jdbc.Driver</value>
 </property>
 <property name="url">
  <value>jdbc:mysql://localhost:3306/ssm1?characterEncoding=UTF-8</value>

 </property>
 <property name="username">
  <value>root</value>
 </property>
 <property name="password">
  <value>admin</value>
 </property>
 </bean>

 <!-- 配置SQLSessionFactory -->
 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="typeAliasesPackage" value="com.ssm1.pojo" />
 <property name="dataSource" ref="dataSource"/>
 <property name="mapperLocations" value="classpath:com/ssm1/mapper/*.xml"/>
 </bean>

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.ssm1.mapper"/>
 </bean>
</beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:annotation-config/>

 <context:component-scan base-package="com.ssm1.controller">
  <context:include-filter type="annotation"
  expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>

 <mvc:annotation-driven />

 <mvc:default-servlet-handler />

 <bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewClass"
  value="org.springframework.web.servlet.view.JstlView" />
 <property name="prefix" value="/WEB-INF/jsp/" />
 <property name="suffix" value=".jsp" />
 </bean>
</beans>

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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <table align='center' border='1' cellspacing='0'>
 <tr>
 <td>id</td>
 <td>student_id</td>
 <td>name</td>
 <td>age</td>
 <td>sex</td>
 <td>birthday</td>
 <td>编辑</td>
 <td>删除</td>
 </tr>
 <c:forEach items="${cs}" var="c" varStatus="st">
 <tr>
  <td>${c.id}</td>
  <td>${c.student_id}</td>
  <td>${c.name}</td>
  <td>${c.age}</td>
  <td>${c.sex}</td>
  <td>${c.birthday}</td>
  <td><a href="editStudent?id=${c.id}" rel="external nofollow" >编辑</a></td>
  <td><a href="deleteStudent?id=${c.id}" rel="external nofollow" >删除</a></td>

 </tr>
 </c:forEach>
</table>
 <div style="text-align:center;margin-top:40px">
 <form method="post" action="addStudent" >
  学生学号: <input name="student_id" value="" type="text"> <br><br>
  学生姓名: <input name="name" value="" type="text"> <br><br>
  学生年纪: <input name="age" value="" type="text"> <br><br>
  学生性别: <input name="sex" value="" type="text"> <br><br>
  学生生日: <input name="birthday" value="" type="text"> <br><br>
  <input type="submit" value="增加学生">
 </form>
 </div>
 <div style="text-align:center; margin-top:20px">
 <form action="${pageContext.request.contextPath }/index" method="post">
  <input value="刷新" type="submit">
 </form>
 </div>
</body>
</html>
<%@ 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"%>

 <div style="width:500px;margin:0px auto;text-align:center">


 <div style="text-align:center;margin-top:40px">

 <form method="post" action="updateStudent">
  分类名称: <input name="student_id" value="${s.student_id}" type="text"> <br><br>
  分类名称: <input name="name" value="${s.name}" type="text"> <br><br>
  分类名称: <input name="age" value="${s.age}" type="text"> <br><br>
  分类名称: <input name="sex" value="${s.sex}" type="text"> <br><br>
  分类名称: <input name="birthday" value="${s.birthday}" type="text"> <br><br>
  <input type="hidden" value="${s.id}" name="id">

  <input type="submit" value="修改分类">
 </form>

 </div>
 </div>

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

到此这篇关于Java中SSM框架实现增删改查功能代码详解的文章就介绍到这了,更多相关SSM框架实现增删改查功内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JDBC之PreparedStatement类中预编译的综合应用解析

    JDBC之PreparedStatement类中预编译的综合应用解析

    SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句
    2013-07-07
  • 详解IDEA社区版(Community)和付费版(UItimate)的区别

    详解IDEA社区版(Community)和付费版(UItimate)的区别

    这篇文章主要介绍了详解IDEA社区版(Community)和付费版(UItimate)的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Spring Boot的properties配置文件读取

    Spring Boot的properties配置文件读取

    这篇文章主要介绍了Spring Boot的properties配置文件读取,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • gRPC中interceptor拦截器的使用教程

    gRPC中interceptor拦截器的使用教程

    gRPC中的interceptor拦截器分为客户端拦截器和服务端拦截器,分别是在客户端和服务端的请求被发送出去之前进行处理的逻辑,下面就跟随小编一起学习一下interceptor拦截器的具体使用吧
    2023-08-08
  • 关于scanner.nextInt()等next()和scanner.nextIine()连用注意事项

    关于scanner.nextInt()等next()和scanner.nextIine()连用注意事项

    这篇文章主要介绍了关于scanner.nextInt()等next()和scanner.nextIine()连用注意事项,具有很好的参考价值,希望对大家有所帮助。
    2023-04-04
  • Java详细分析LCN框架分布式事务

    Java详细分析LCN框架分布式事务

    这篇文章主要介绍了Java LCN框架分布式事务,分布式事务是指事务的参与者、支持事务的服务器、资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上
    2022-07-07
  • SpringBoot浅析依赖管理与自动配置概念与使用

    SpringBoot浅析依赖管理与自动配置概念与使用

    一般来讲SpringBoot项目是不需要指定版本,而SSM项目是需要指定版本,SpringBoot的核心依赖就是spring-boot-starter-parent和spring-boot-starter-web两个依赖,这篇文章主要介绍了SpringBoot依赖管理与自动配置概念与使用
    2022-10-10
  • Java实现石头剪刀布游戏

    Java实现石头剪刀布游戏

    这篇文章主要为大家详细介绍了Java实现石头剪刀布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • Java的MyBatis框架中XML映射缓存的使用教程

    Java的MyBatis框架中XML映射缓存的使用教程

    MyBatis程序在做好XML映射后能够有缓存的功能,这样映射过SQL语句的配置以后就可以拿过来直接用了,这里我们来一起总结一下Java的MyBatis框架中XML映射缓存的使用教程
    2016-06-06
  • 手把手带你实现一个萌芽版的Spring容器

    手把手带你实现一个萌芽版的Spring容器

    大家好,我是老三,Spring是我们最常用的开源框架,经过多年发展,Spring已经发展成枝繁叶茂的大树,让我们难以窥其全貌,这节,我们回归Spring的本质,五分钟手撸一个Spring容器,揭开Spring神秘的面纱
    2022-03-03

最新评论