ssm整合(简单的增删改查)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: ssm整合(简单的增删改查)

1 创建maven web项目;

2 添加jar包(所有的依赖)

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--json依赖-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>
    <!--添加springwebmvc依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <!--mybatis依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!--mybatis的spring接口实现依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!--数据源依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <!--数据库驱动依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
    </dependency>
    <!--druid依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.26</version>
    </dependency>
    <!--jstl依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!---->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.4.0.Final</version>
    </dependency>
  </dependencies>


添加最主要的Tomact目录

<plugins>
      <!--添加tomcat-plugin插件用于web发布执行-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/</path>
          <port>8080</port>
        </configuration>
      </plugin>
    </plugins>



3 补充目录及配置文件

我们用的是idea所以目录是不完整的,需要我们自己来配置,




4 整合spring与mybatis


spring-mybatis.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--4.1加载数据库参数文件-->
    <context:property-placeholder location="classpath:res/db.properties"/>
    <!--4.2配置数据库连接池druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--4.3配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:mapping/*.xml"/>
    </bean>
    <!--4.4 配置mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--配置dao扫描接口-->
        <property name="basePackage" value="mapper"/>
    </bean>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mmmm
jdbc.username=root
jdbc.password=root


mapper路径下的DeptController

public interface DeptMapper {
    public List<Dept> findAll();
    public Dept findById(int deptno);
    public boolean updateDept(Dept dept);
    public boolean insertDept(Dept dept);
    public boolean deleteDept(int deptno);
}


resources资源目录下的mapping文件夹的DeptMapper.xml

<?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.zyzy.day54.mapper.DeptMapper">
    <select id="findAll" resultType="com.zyzy.day54.po.Dept">
        SELECT *
        FROM DEPT
    </select>
    <select id="findById" parameterType="int" resultType="com.zyzy.day54.po.Dept">
        SELECT *
        FROM DEPT
        WHERE deptno = #{deptno}
    </select>
    <update id="updateDept" parameterType="com.zyzy.day54.po.Dept">
        UPDATE DEPT
        SET dname=#{dname},loc=#{loc}
        WHERE deptno=#{deptno}
    </update>
    <insert id="insertDept" parameterType="com.zyzy.day54.po.Dept">
        INSERT INTO
        DEPT
        (dname,loc)
        VALUES (#{dname},#{loc})
    </insert>
    <delete id="deleteDept" parameterType="int" >
        DELETE FROM
        DEPT
        WHERE deptno=#{deptno}
    </delete>
</mapper>


po里面我们封装的实体类

public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;
    public Dept(Integer deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }
    public Dept() {
    }
    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
    public Integer getDeptno() {
        return deptno;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public String getLoc() {
        return loc;
    }
    public void setLoc(String loc) {
        this.loc = loc;
    }
}


5 整合web容器与spring

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--5整合spring和web容器 begin-->
  <!--配置全局参数上下文-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring-*.xml</param-value>
  </context-param>
  <!--7.1 配置编码过滤器-->
  <filter>
    <filter-name>characterFilter</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>characterFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--配置侦听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--整合spring和web容器 end-->
  <!--7 整合springmvc+web容器begin-->
  <!--因为web.xml书写规范必须要求过滤器在监听前所以提到前面-->
  <!--7.2配置核心处理器-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <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>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


先完成service和mapper层的解耦

public interface DeptService {
    public List<Dept> findDepts();
    public Dept findById(int deptno);
    public boolean updateDept(Dept dept);
    public boolean insertDept(Dept dept);
    public boolean deleteDept(int deptno);
}
service的实现类
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findDepts() {
        List<Dept> all = deptMapper.findAll();
        return all;
    }
    @Override
    public Dept findById(int deptno) {
        Dept dept = deptMapper.findById(deptno);
        return dept;
    }
    @Override
    public boolean updateDept(Dept dept) {
        boolean b = deptMapper.updateDept(dept);
        return b;
    }
    @Override
    public boolean insertDept(Dept dept) {
        boolean b = deptMapper.insertDept(dept);
        return b;
    }
    @Override
    public boolean deleteDept(int deptno) {
        boolean b = deptMapper.deleteDept(deptno);
        return b;
    }
}


配置spring-service.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.zyzy.day54.service"/>
</beans>



完成controller和service解耦

DeptController

@Controller
public class DeptController {
    @Autowired
    public DeptService deptService;
    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    public String findAll(Model model){
        List<Dept> depts = deptService.findDepts();
        model.addAttribute("depts",depts);
        return "list";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String findById(@RequestParam("deptno") int deptno ,Map map){
        Dept dept = deptService.findById(deptno);
        map.put("dept",dept);
        return "edit";
    }
    @RequestMapping( value = "/update" , method = RequestMethod.POST)
    public String updateDept( Dept dept){
            boolean b = deptService.updateDept(dept);
            if (b){
                return "redirect:/depts";
            }
            return null;
    }
    @RequestMapping(value = "/add" , method = RequestMethod.POST)
    public String insertDept(Dept dept){
            boolean b = deptService.insertDept(dept);
            if (b){
                return "redirect:/depts";
            }
        return null;
    }
    @RequestMapping(value = "/delete" , method = RequestMethod.GET)
    public String deleteDept(@RequestParam("deptno") int deptno){
        boolean b = deptService.deleteDept(deptno);
        if (b){
            return "redirect:/depts";
        }else
            return null;
    }
}


6 完善springmvc配置

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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--6.1 配置controller(c处理器)扫描包路径-->
    <context:component-scan base-package="com.zyzy.day54.controller"/>
    <!--6.2 配置注解驱动-->
    <mvc:annotation-driven/>
    <!--6.3 配置静态资源处理器-->
    <mvc:default-servlet-handler/>
    <!--5.4 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>



7 整合web容器与springmvc


web.xml(看第五步)


8 检验整合效果


附上所用的前端页面

add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="add" method="post">
    部门名称<input type="text" name="dname" >
    <br>
    部门地址<input type="text" name="loc" >
    <br>
    <input type="submit" value="add">
</form>
</body>
</html>


edit.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="update" method="post">
    部门id<input type="text" name="deptno" value="${dept.deptno}" readonly><br>
    部门名称<input type="text" name="dname" value="${dept.dname}">
    <br>
    部门地址<input type="text" name="loc" value="${dept.loc}">
    <br>
    <input type="submit" value="update">
</form>
</body>
</html>


index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="/depts">查看全部信息</a>
<br>
<a href="/add.jsp">添加</a>
</body>
</html>


list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:forEach items="${depts}" var="dept">
        ${dept.deptno}
        ${dept.dname}
        ${dept.loc}
        <a href="/edit?deptno=${dept.deptno}">修改</a>
        <a href="/delete?deptno=${dept.deptno}">删除</a>
        <br>
    </c:forEach>
</body>
</html>


    水平有限,有什么错误希望大家指出!!!


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
1
分享
相关文章
Python中使用Flask-SQLAlchemy对数据库的增删改查简明示例
这样我们就对Flask-SQLAlchemy进行了一次简明扼要的旅程,阐述了如何定义模型,如何创建表,以及如何进行基本的数据库操作。希望你在阅读后能对Flask-SQLAlchemy有更深入的理解,这将为你在Python世界中从事数据库相关工作提供极大的便利。
247 77
Android数据库的使用(增删改查)
本文介绍了一个简单的数据库操作Demo,包含创建数据库、增删改查功能。通过5个按钮分别实现创建数据库、插入数据、删除数据、更新数据和查询数据的操作。代码结构清晰,适合初学者学习Android SQLite数据库基础操作。
Android常用的room增删改查语句(外部数据库)
本文分享了将一个原生数据库驱动的单词APP重构为使用Room库的过程及遇到的问题,重点解决了Room中增删改查的常用语句实现。文章通过具体示例(以“forget”表为例),详细展示了如何定义实体类、Dao接口、Database类以及Repository和ViewModel的设计与实现。同时,提供了插入、删除、更新和查询数据的代码示例,包括模糊查询、分页加载等功能。此外,针对外部数据库导入问题,作者建议可通过公众号“计蒙不吃鱼”获取更多支持。此内容适合有一定Room基础的开发者深入学习。
Android常用的room增删改查语句(外部数据库)
Unity连接Mysql数据库 增 删 改 查
在 Unity 中连接 MySQL 数据库,需使用 MySQL Connector/NET 作为数据库连接驱动,通过提供服务器地址、端口、用户名和密码等信息建立 TCP/IP 连接。代码示例展示了如何创建连接对象并执行增删改查操作,确保数据交互的实现。测试代码中,通过 `MySqlConnection` 类连接数据库,并使用 `MySqlCommand` 执行 SQL 语句,实现数据的查询、插入、删除和更新功能。
HarmonyOs开发:关系型数据库封装之增删改查
每个方法都预留了多种调用方式,比如使用callback异步回调或者使用Promise异步回调,亦或者同步执行,大家在使用的过程中,可以根据自身业务需要进行选择性调用,也分别暴露了成功和失败的方法,可以针对性的判断在执行的过程中是否执行成功。
259 13
使用Py2neo进行Neo4j图数据库的增删改查操作
使用Py2neo进行Neo4j图数据库的增删改查操作
301 5
低代码开发工具-学生管理系统-老师管理增删改查实现
低代码开发工具-学生管理系统-老师管理增删改查实现
121 5
javamvc配置,增删改查,文件上传下载。
【10月更文挑战第4天】javamvc配置,增删改查,文件上传下载。
78 1
[新手入门]todolist增删改查:vue3+ts版本!
【10月更文挑战第15天】[新手入门]todolist增删改查:vue3+ts版本!
Data jpa 增删改查的方法分别有哪些
Data jpa 增删改查的方法分别有哪些
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问