最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中SSM框架實(shí)現(xiàn)增刪改查功能代碼詳解

 更新時(shí)間:2020年07月21日 15:59:06   作者:FrankYu  
這篇文章主要介紹了Java中SSM框架實(shí)現(xiàn)增刪改查功能代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

記錄一下自己第一次整合smm框架的步驟。

參考博客和網(wǎng)站有:我沒(méi)有三顆心臟 How2J學(xué)習(xí)網(wǎng)站

1.數(shù)據(jù)庫(kù)使用的是mySql,首先創(chuàng)建數(shù)據(jù)庫(kù)ssm1,并創(chuàng)建表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項(xiàng)目,命名為ssm1,并且導(dǎo)入相關(guān)的jar包。

3.建立pojo類(lèi),在這里命名為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對(duì)應(yīng)的xml文件,同樣屬于包c(diǎn)om.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類(lèi),實(shí)現(xiàn)接口,包名為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核心:分發(fā)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 <!-- 配置@Service包的掃描 -->
 <context:annotation-config />
 <context:component-scan base-package="com.ssm1.service" />
 <!-- 配置數(shù)據(jù)庫(kù)的連接 -->
 <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  <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下創(chuàng)建jsp目錄,并創(chuàng)建文件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" >
  學(xué)生學(xué)號(hào): <input name="student_id" value="" type="text"> <br><br>
  學(xué)生姓名: <input name="name" value="" type="text"> <br><br>
  學(xué)生年紀(jì): <input name="age" value="" type="text"> <br><br>
  學(xué)生性別: <input name="sex" value="" type="text"> <br><br>
  學(xué)生生日: <input name="birthday" value="" type="text"> <br><br>
  <input type="submit" value="增加學(xué)生">
 </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">
  分類(lèi)名稱(chēng): <input name="student_id" value="${s.student_id}" type="text"> <br><br>
  分類(lèi)名稱(chēng): <input name="name" value="${s.name}" type="text"> <br><br>
  分類(lèi)名稱(chēng): <input name="age" value="${s.age}" type="text"> <br><br>
  分類(lèi)名稱(chēng): <input name="sex" value="${s.sex}" type="text"> <br><br>
  分類(lèi)名稱(chēng): <input name="birthday" value="${s.birthday}" type="text"> <br><br>
  <input type="hidden" value="${s.id}" name="id">

  <input type="submit" value="修改分類(lèi)">
 </form>

 </div>
 </div>

13.最后在tomcat上部署項(xiàng)目,輸入路徑localhost:端口號(hào)/ssm1/index即可訪問(wèn)

到此這篇關(guān)于Java中SSM框架實(shí)現(xiàn)增刪改查功能代碼詳解的文章就介紹到這了,更多相關(guān)SSM框架實(shí)現(xiàn)增刪改查功內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 帶你玩轉(zhuǎn)Kafka之初步使用

    帶你玩轉(zhuǎn)Kafka之初步使用

    最近開(kāi)發(fā)的項(xiàng)目中,kafka用的比較多,為了方便梳理,所以記錄一些關(guān)于kafka的文章,這篇文章主要給大家介紹了關(guān)于Kafka初步使用的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 詳解如何實(shí)現(xiàn)OpenAPI開(kāi)發(fā)動(dòng)態(tài)處理接口的返回?cái)?shù)據(jù)

    詳解如何實(shí)現(xiàn)OpenAPI開(kāi)發(fā)動(dòng)態(tài)處理接口的返回?cái)?shù)據(jù)

    這篇文章主要為大家介紹了OpenAPI開(kāi)發(fā)動(dòng)態(tài)處理接口的返回?cái)?shù)據(jù)如何實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Java Spring循環(huán)依賴(lài)原理與bean的生命周期圖文案例詳解

    Java Spring循環(huán)依賴(lài)原理與bean的生命周期圖文案例詳解

    這篇文章主要介紹了Spring循環(huán)依賴(lài)原理與bean的生命周期圖文案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • java實(shí)現(xiàn)高效的枚舉元素集合示例

    java實(shí)現(xiàn)高效的枚舉元素集合示例

    Set是Java集合類(lèi)的重要組成部分,它用來(lái)存儲(chǔ)不能重復(fù)的對(duì)象。枚舉類(lèi)型也要求其枚舉元素各不相同。看起來(lái)枚舉類(lèi)型和集合是很相似的。然而枚舉類(lèi)型中的元素不能隨意的增加、刪除,作為集合而言,枚舉類(lèi)型非常不實(shí)用。EnumSet是專(zhuān)門(mén)為enum實(shí)現(xiàn)的集合類(lèi),本實(shí)例將演示其用法
    2014-03-03
  • SpringBoot靜態(tài)資源CSS等修改后再運(yùn)行無(wú)效的解決

    SpringBoot靜態(tài)資源CSS等修改后再運(yùn)行無(wú)效的解決

    這篇文章主要介紹了SpringBoot靜態(tài)資源CSS等修改后再運(yùn)行無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)流程詳解

    SpringBoot集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)流程詳解

    ShedLock是一個(gè)鎖,官方解釋是他永遠(yuǎn)只是一個(gè)鎖,并非是一個(gè)分布式任務(wù)調(diào)度器。一般shedLock被使用的場(chǎng)景是,你有個(gè)任務(wù),你只希望他在單個(gè)節(jié)點(diǎn)執(zhí)行,而不希望他并行執(zhí)行,而且這個(gè)任務(wù)是支持重復(fù)執(zhí)行的
    2023-02-02
  • 解決JDK異常處理No appropriate protocol問(wèn)題

    解決JDK異常處理No appropriate protocol問(wèn)題

    這篇文章主要介紹了解決JDK異常處理No appropriate protocol問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • SpringBoot中Redis的緩存更新策略詳解

    SpringBoot中Redis的緩存更新策略詳解

    這篇文章主要介紹了SpringBoot中Redis的緩存更新策略,緩存一般是為了應(yīng)對(duì)高并發(fā)場(chǎng)景、緩解數(shù)據(jù)庫(kù)讀寫(xiě)壓力,而將數(shù)據(jù)存儲(chǔ)在讀寫(xiě)更快的某種存儲(chǔ)介質(zhì)中(如內(nèi)存),以加快讀取數(shù)據(jù)的速度,需要的朋友可以參考下
    2023-08-08
  • Java Map如何根據(jù)key取value以及不指定key取出所有的value

    Java Map如何根據(jù)key取value以及不指定key取出所有的value

    這篇文章主要介紹了Java Map如何根據(jù)key取value以及不指定key取出所有的value,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 淺談Thread.sleep(0)到底有什么用

    淺談Thread.sleep(0)到底有什么用

    為什么要用sleep,主要是為了暫停當(dāng)前線(xiàn)程,把cpu片段讓出給其他線(xiàn)程,減緩當(dāng)前線(xiàn)程的執(zhí)行,本文主要介紹了Thread.sleep(0)到底有什么用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06

最新評(píng)論

赤峰市| 通江县| 儋州市| 陵水| 丰镇市| 嫩江县| 桂东县| 建始县| 紫金县| 大化| 西宁市| 会宁县| 永春县| 根河市| 彭水| 扎囊县| 民丰县| 新闻| 方山县| 桂阳县| 新巴尔虎左旗| 如皋市| 浑源县| 榆社县| 阳高县| 河东区| 台北县| 华坪县| 永平县| 兰西县| 奉节县| 交口县| 博爱县| 木兰县| 宣城市| 舒兰市| 彩票| 黄平县| 出国| 兴仁县| 信丰县|