SSM框架整合

SSM框架整合

这个项目呢是对咋们以前学习的一个大整合里面呢如果有什么比较重要的东西呢我也会列出来给大家讲解,第一部呢就是保证你环境变量和tomcat,maven,jdk一个版本的兼容问题这些工作准备好以后呢就可以开始写咋们这次的项目了,也就相当于是一个大练习吧。

  • 这里把项目结构给大家拿过来

在这里插入图片描述

  • 准备工作完成后大家去创建一个干净的maven项目然后导入依赖

    <!--导入依赖 junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring-->
    <dependencies>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--数据库连接池:c3p0,dbcp-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--Servlet   JSP-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
         <!--偷懒 lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>
    <!--在build中配置resources,来防止我们资源导出失败的问题-->
    <build>
        <finalName>SSMDemo</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
  • 最重要的一个步骤就是去创建一个数据库

在这里插入图片描述

以上工作准备好了以后,我们呢就要开始去整合Mybatis层了

Mybatis整合

  • 项目的搭建结构

在这里插入图片描述

  • pojo包下的Vip实体类要和数据库中的变量对应
package com.cloud.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

//这三个注解的意思呢应该是都明白,如果有什么不明白的呢大家可以去看看我以前的文章就都明白了
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Vip {
    private String name;
    private String id;
    private String old;
    private String date;
}

  • 接下来呢就是对应的mapper接口
package com.cloud.mapper;

import com.cloud.pojo.Vip;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface VipMapper {
    //查询全部用户
    List<Vip> getVip();
    //增加用户
    int insertID(Vip vip);
    //删除用户
    int deleteID(@Param("id") int id);
    //id查询用户
    int selectID(@Param("id") int id);
    //修改用户
    int updateID(Vip vip);
}

  • 接口完了就是对应的VipMapper.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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.cloud.mapper.VipMapper">
    <!--select查询语句-->
    <select id="getVip" resultType="Vip">
        select * from ssm.vip;
    </select>
    <!--添加用户-->
    <insert id="insertID" parameterType="Vip">
        insert into ssm.vip (name, id, old, date)
        values (#{name},#{id},#{old},#{date});
    </insert>
    <!--修改用户-->
    <update id="updateID" parameterType="Vip">
        update ssm.vip
        set name=#{name},old=#{old},date=#{date}
        where id=#{id};
    </update>
    <!--删除用户-->
    <delete id="deleteID" parameterType="Vip">
        delete from ssm.vip where id=#{id};
    </delete>
    <!--根据用户ID查询用户-->
    <select id="selectID" resultType="Vip">
        select *from ssm.vip where id=#{id}
    </select>


</mapper>
  • 下一步呢就是去mybatis-config.xml中去注册
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--配置数据源交给Spring去做-->

    <!--这个呢是引入一个外部文件-->
    <properties resource="db.properties">

    </properties>

    <!--这里呢起一个别名让它自动去扫描-->
    <typeAliases>
        <package name="com.cloud.pojo"/>
    </typeAliases>

<mappers>
    <mapper class="com.cloud.mapper.VipMapper"/>
</mappers>



</configuration>
  • 最后一步呢是去编写业务层service中的ServiceVip接口以及ServiceVipImpl类
package com.cloud.service;

import com.cloud.pojo.Vip;

import java.util.List;

public interface ServiceVip {
    //查询全部用户
    List<Vip> getVip();
    //增加用户
    int insertID(Vip vip);
    //删除用户
    int deleteID( int id);
    //id查询用户
    int selectID( int id);
    //修改用户
    int updateID(Vip vip);
}

package com.cloud.service;

import com.cloud.mapper.VipMapper;
import com.cloud.pojo.Vip;

import java.util.List;

public class ServiceVipImpl implements ServiceVip {

    //Service调Dao:组合Dao
    private VipMapper vipMapper;

    public void setVipMapper(VipMapper vipMapper) {
        this.vipMapper = vipMapper;
    }

    public List<Vip> getVip() {
        return vipMapper.getVip();
    }

    public int insertID(Vip vip) {
        return vipMapper.insertID(vip);
    }

    public int deleteID(int id) {
        return vipMapper.deleteID(id);
    }

整合Spring

  • 项目结构
    在这里插入图片描述

  • 这一步呢实现类Spring,Mybatis的整合把原本在Mybatis配置文件和工具类里面做的拿到了Spring里面完成Spring整合Mapper层

<?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:annotation-config/>

    <!--关联数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--数据库连接池
        dbcp:半自动操作,不能自动连接
        c3po:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中!)
        druid:hikari
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.drive}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--绑定Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--配置dao接口扫描包,动态实现了Dao接口可以注入到Spring容器中!-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的dao包-->
        <property name="basePackage" value="com.cloud.mapper"/>
    </bean>
    </beans>


  • Spring整合Service层
<?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:annotation-config/>

    <!--扫描service下的包-->
    <context:component-scan base-package="com.cloud.service"/>

    <!--将我们的业务类注入到我们的Spring中-->
    <bean id="ServiceVipImpl" class="com.cloud.service.ServiceVipImpl">
        <property name="vipMapper" ref="vipMapper"/>
    </bean>

    <!--声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>
  • 最后呢把所有文件呢都整合到一个里面方便调用
<?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:annotation-config/>
    <import resource="spring-service.xml"/>
    <import resource="spring-dao.xml"/>

</beans>

到这里呢咋们的Spring也就整合完毕了

整合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/cache/spring-mvc.xsd">

    <!--开启注解支持-->
    <context:annotation-config/>

    <!--注解驱动-->
    <mvc:annotation-driven/>

    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--扫描包:controller-->
    <context:component-scan base-package="com.cloud.controller"/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  • 去web.xml中去配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--DispatchServlet-->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--乱码过滤-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>
  • 最后呢把SpringMVC.xml文件引用到applicationContext.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:annotation-config/>
    <import resource="spring-service.xml"/>
    <import resource="spring-dao.xml"/>
    <import resource="spring-mvc.xml"/>

</beans>

到这里呢一个框架就搭建完成了也整合完了,接下来就要开始写业务了。

  • 查询全部书籍(项目结构)
    在这里插入图片描述
    在这里插入图片描述

  • VipController

package com.cloud.controller;

import com.cloud.pojo.Vip;
import com.cloud.service.ServiceVip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/vip")
public class VipController {
    //controller调用service
    @Autowired
    @Qualifier("ServiceVipImpl")
    private ServiceVip serviceVip;
    //查询全部书籍,并且返回一个前端展示页面
    @RequestMapping("/allVip")
    public String list(Model model){
        List<Vip> vip = serviceVip.getVip();
        model.addAttribute("list",vip);
        return "allVip";
    }
    //跳转到增加书籍界面
    @RequestMapping("/toAddVip")
    public String ToAddVip(){
        return "addVip";
    }
    //添加书籍的请求
    @RequestMapping("/addVip")
    public String AddVip(Vip vip){
        serviceVip.insertID(vip);
        return "redirect:/vip/allVip";//重定向到一个 @RequestMapping("/allVip")请求:
    }

    //跳转到修改书籍界面
    @RequestMapping("/deletesVip")
    public String deleteAddVip(){
        return "redirect:/vip/allVip";
    }
    //跳转到修改书籍界面
    @RequestMapping("/deleteVipallVip")
    public String deleteAdd1Vip(int name,Model model){
        Vip vip = serviceVip.selectID(name);
        model.addAttribute("QVip",vip);
        return "deleteVipallVip";
    }
}

  • jsp下的页面也给大家拿过来(addVIP.jsp,allVip.jsp,deleteVipallVip.jsp)
<%--
  Created by IntelliJ IDEA.
  User: 28073
  Date: 2021/4/11
  Time: 15:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>增加书籍</title>
    <link  href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 colum">
            <div class="page-header">
                <h1>
                    <small>书籍增加页面</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/vip/addVip" method="post">
        <div class="from-group">
            <label>书籍编号:</label>
            <input type="text" name="name" class="form-control" required>
        </div>
        <div class="from-group">
            <label>书籍名称:</label>
            <input type="text" name="id" class="form-control" required>
        </div>
        <div class="from-group">
            <label>书籍数量:</label>
            <input type="text" name="old" class="form-control" required>
        </div>
        <div class="from-group">
            <label>书籍详情:</label>
            <input type="text" name="date" class="form-control" required>
        </div>
        <div class="from-group">
            <label></label>
            <input type="submit" class="form-control" value="添加">
        </div>

    </form>
</div>


</body>
</html>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 28073
  Date: 2021/4/6
  Time: 8:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书籍展示</title>

    <%--BootStrap美化界面--%>
    <link  href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container">
    <div class="row clearfix">
        <div class="col-md-12 colum">
            <div class="page-header">
                <h1>
                    <small>书籍展示页面—————————— 显示所有书籍</small>
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 colum">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>书籍编号</th>
                    <th>书籍名称</th>
                    <th>书籍数量</th>
                    <th>书籍详情</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach var="vip" items="${list}">
                    <tr>
                        <td>${vip.name}</td>
                        <td>${vip.id}</td>
                        <td>${vip.old}</td>
                        <td>${vip.date}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/vip/deleteVipallVip?name=${vip.name}">修改</a>
                            &nbsp;|&nbsp;
                            <a href="${pageContext.request.contextPath}/vip/deleteVipallVip">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>


        </div>

    </div>


</div>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: 28073
  Date: 2021/4/11
  Time: 15:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>增加书籍</title>
    <link  href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 colum">
            <div class="page-header">
                <h1>
                    <small>书籍增加页面</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/vip/addVip" method="post">
        <div class="from-group">
            <label>书籍编号:</label>
            <input type="text" name="name" class="form-control" value="${QVip.name}" required>
        </div>
        <div class="from-group">
            <label>书籍名称:</label>
            <input type="text" name="id" class="form-control"value="${QVip.id} required>
        </div>
        <div class="from-group">
            <label>书籍数量:</label>
            <input type="text" name="old" class="form-control"value="${QVip.old} required>
        </div>
        <div class="from-group">
            <label>书籍详情:</label>
            <input type="text" name="date" class="form-control"value="${QVip.date} required>
        </div>
         <div class="from-group">
            <label></label>
            <input type="submit" class="form-control" value="修改">
        </div>
    </form>
</div>


</body>
</html>

  • index.jsp
<%--
  Created by IntelliJ IDEA.
  User: 28073
  Date: 2021/4/4
  Time: 21:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
    <style>
      a{
        text-decoration: none;
        color: burlywood;
        font-size: 18px;
      }
      h2{
        width: 180px;
        height: 38px;
        margin: 100px auto;
        text-align: center;
        line-height: 38px;
        background: cornflowerblue;
        border-radius: 5px;
      }

    </style>
  </head>
  <body>
<h2>
<a href="${pageContext.request.contextPath}/vip/allVip">书籍查询</a>
</h2>

<h2>
  <a href="${pageContext.request.contextPath}/vip/toAddVip">增加书籍</a>
</h2>
<h2>
    <a href="${pageContext.request.contextPath}/vip/deletesVip">修改和删除书籍</a>
</h2>
<h2>
    <a href="${pageContext.request.contextPath}/vip/toAddVip">搜索书籍</a>
</h2>

  </body>
</html>

到这里呢我们的增加和查询全部就已经全部书写完毕了,大家呢先可以去联系一下接下来的修改和删除我会尽快补全,对了最后呢给大家留了一个彩蛋有一个很简单的小错误需要大家解决不会的可以看看我以前的文章,这个问题呢会在我下次更新的时候一并解决。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值