SSM框架入门

一、介绍一下SSM框架

1.什么是SSM框架

spring+springMVC+mybatis三者的结合,是标准的MVC模式。标准的SSM框架有四层,分别是dao层(mapper),service层,controller层和View层。使用spring实现业务对象管理,使用spring MVC负责请求的转发和视图管理,mybatis作为数据对象的持久化引擎。DAO层,Service层这两个层次都可以单独开发,互相的耦合度很低。Controller,View层因为耦合度比较高,因而要结合在一起开发。

1)持久层dao(mapper)

负责与数据库进行联络的一些任务都封装在此

  • Dao层首先设计的是接口,然后再Spring的配置文件中定义接口的实现类。
  • 然后可以在模块中进行接口的调用来进行数据业务的处理。(不在关心接口的实现类是哪个类)
  • 数据源的配置以及有关数据库连接的参数都在Spring的配置文件中进行配置。

2)业务层service

负责业务模块的逻辑应用设计

  • 先设计接口然后再设计实类,然后再在Spring的配置文件中配置其实现的关联。(业务逻辑层的实现具体要调用到自己已经定义好的Dao的接口上)这样就可以在应用中调用Service接口来进行业务处理。
  • 建立好Dao之后再建立service层,service层又要在controller层之下,因为既要调用Dao层的接口又要提供接口给controller层。每个模型都有一个service接口,每个接口分别封装各自的业务处理的方法。

3)表现层controller

负责具体的业务模块流程的控制

  • 配置也同样是在Spring的配置文件里面进行,
  • 调用Service层提供的接口来控制业务流程。
  • 业务流程的不同会有不同的控制器,在具体的开发中可以将我们的流程进行抽象的归纳,设计出可以重复利用的子单元流程模块。

4)视图view

和控制层紧密结合,主要负责前台jsp页面的表示

参考来源:SSM框架超详细讲解-CSDN博客

2.什么是spring(需要补充)

Spring里面的IOC容器和AOP是我们平时使用最多的。剩下的还是不懂,下次再研究吧。

3.什么是springMVC

1.客户端发送请求到DispacherServlet(分发器)
2.由DispacherServlet控制器查询HanderMapping,找到处理请求的Controller
3.Controller调用业务逻辑处理后,返回ModelAndView
4.DispacherSerclet查询视图解析器,找到ModelAndView指定的视图
5.视图负责将结果显示到客户端

                        
原文链接:https://blog.csdn.net/admans/article/details/139585938

4.什么是mybatis

mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。

二、搭建一个SSM实例

1.创建数据库和数据表。

参照我的文章《开发一个数据管理平台cableManagementSystem》

2.在IDEA中完成框架配置

1)构建项目路径

项目路径如下图,注意java和resources和webapp的文件夹图样不一样。需要在IDEA中设置文件路径。

2)配置文件

我这里需要配置5个文件

pom.xml

引入环境所需要的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>CableManagementSystem</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CableManagementSystem Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
<!--    添加springMVC依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
<!--    spring核心类-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
<!--    servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
<!--JSP-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>   <!--服务已由服务器提供,打成WAR包不含这个包-->
    </dependency>
<!-- 日志 -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
<!-- Spring5和Thymeleaf整合包 -->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring5</artifactId>
      <version>3.0.12.RELEASE</version>
    </dependency>
    <!--  MyBatis框架核心jar包  -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.3</version>
    </dependency>
    <!-- MySQL数据库驱动包 -->
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.3.0</version>
    </dependency>
    <!-- 导入Spring的jar包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.1.RELEASE</version>
    </dependency>
    <!-- 添加对AOP的支持  -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>
    <!-- 事务的jar包    -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.1.RELEASE</version>
    </dependency>
    <!-- 操作数据库的支持	-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.1.RELEASE</version>
    </dependency>
    <!--包含web应用开发时,用到spring框架时所需的核心类、文件上传以及工具类等-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.1.RELEASE</version>
    </dependency>
    <!--与SpringMVC框架相关的jar包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.1.RELEASE</version>
    </dependency>
    <!--与json相关的jar包-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
    <!--原生的servlet的jar包-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- JSTL表达式 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!--文件上传的jar包-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <!--  log4j日志包  -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!-- spring的测试包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.1.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <!--  单元测试框架  -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!-- MyBatis框架和Spring框架转换的转换包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.3</version>
    </dependency>
    <!-- 阿里巴巴的数据源 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.21</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>CableManagementSystem</finalName>
    <resources>
      <resource>
        <!-- xml放在java目录下-->
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <!--指定资源的位置(xml放在resources下,可以不用指定)-->
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
  </build>
</project>
web.xml

配置DispatcherServlet前端控制器,编码过滤器等,配置解析URL的路径,配置springMVC的解析路径

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">


<!--  解决获取请求参数的乱码问题,可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter,但是必须在web.xml中进行注册-->
  <!--配置springMVC的编码过滤器-->
  <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>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>  <!--对所有请求都进行编码-->
  </filter-mapping>
<!-- 配置 Spring MVC 的前端控制器,对浏览器发送的请求统一处理 -->
<!--一个请求对应一个servlet  -->
  <servlet>
    <servlet-name>cableMS-Servlet</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <!-- 配置初始化参数,用于读取 Spring MVC 的配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value> <!--文件存储在resources下,并命名为spring-mvc.xml-->
    </init-param>
    <!-- 应用加载时创建,将前端控制器dispatchrServlet的初始化时间提前到服务器启动时-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>cableMS-Servlet</servlet-name>
    <url-pattern>/</url-pattern>  <!--可以匹配/login或.html或.js或.css请求路径,不能匹配.jsp路径的请求-->
  </servlet-mapping>
</web-app>
sping-mvc.xml

配置视图解析器(用的thymeleaf)和扫描控制器

<?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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--    &lt;!&ndash;采用注解驱动&ndash;&gt;-->
<!--    <mvc:annotation-driven/>-->
<!-- 这个配置文件用于配置Spring MVC的基本设置,包括扫描包路径和视图解析器的设置。-->

<!-- 扫描控制器:配置spring MVC 要扫描的包-->
<!-- 使用了context命名空间的component-scan标签来配置Spring MVC要扫描的包路径为"com.cable.controller"。    -->
    <context:component-scan base-package="com.cable.mvc.controller"/>


    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>  <!--视图优先级,有限解析哪个视图-->
        <property name="characterEncoding" value="UTF-8"/> <!--视图编码-->
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>  <!--视图前缀+视图名称+视图后缀   访问到页面-->
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>  <!--视图模板采用H5-->
                        <property name="characterEncoding" value="UTF-8" /> <!--页面编码UTF8-->
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

    <!--
   处理静态资源,例如html、js、css、jpg
  若只设置该标签,则只能访问静态资源,其他请求则无法访问
  此时必须设置<mvc:annotation-driven/>解决问题
 -->
    <mvc:default-servlet-handler/>

    <!-- 开启mvc注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- 处理响应中文内容乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

<!--定义内部资源视图解析器:负责解析控制器里从逻辑视图到物理页面的映射-->
<!--接下来使用了bean标签定义了一个InternalResourceViewResolver的bean。这个bean是Spring MVC中用于解析视图的类。-->
<!--在这里设置了两个属性:prefix和suffix,分别指定了视图文件的前缀和后缀。-->

<!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--&lt;!&ndash;        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>&ndash;&gt;-->
<!--        <property name="prefix" value="/WEB-INF/views/"/> &lt;!&ndash;路径&ndash;&gt;-->
<!--        <property name="suffix" value=".jsp"/> &lt;!&ndash;扩展名&ndash;&gt;-->
<!--    </bean>-->


</beans>

<!--最后,需要点击右上角的m加载进所需要的框架-->
mybatis-config.xml

配置数据库的连接信息和SQL的映射文件路径

<?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">
<!-- mybatis的主配置文件 -->
<configuration>
    <!--mybatis.properties配置文件-->
    <properties resource="mybatis.properties"></properties>
    <!--setttings:控制mybatis全局配置行为-->
    <settings>
        <!--设置mybatis输出日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <!--环境配置:数据库的连接信息-->
    <!--default:中配置的值需要和某个environment配置的id相同,
        通知mybatis使用具体哪一个数据库的连接信息,即具体访问对应的数据库-->
    <environments default="development">
        <!--environment:一个数据库信息的配置,环境-->
        <!--id属性:一个唯一值,自定义,表示环境的名称-->
        <environment id="development">
            <!--transactionManager属性:mybatis的事务类型-->
            <transactionManager type="JDBC" />
            <!--dataSoure属性:代表数据源,连接数据库-->
            <!--type属性:JDBC 表示使用的是使用连接池的数据源-->
            <dataSource type="POOLED">
                <!--配置驱动-->
                <property name="driver" value="${jdbc.driver}"/>
                <!--配置连接数据库的url字符串-->
                <property name="url" value="${jdbc.url}"/>
                <!--配置连接数据库的用户名-->
                <property name="username" value="${jdbc.username}"/>
                <!--配置连接数据库的密码-->
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--sql映射文件标签-->
    <mappers>
        <!--reource:从类路径开始的路径信息:target/clasess(类路径)-->
        <mapper resource="mapper/PersonMapper.xml"/>
    </mappers>
</configuration>
mybatis.properties

配置mybatis-config.xml中的引用值

#########jdbc.driver=com.mysql.jdbc.Driver

jdbc.driver=com.mysql.cj.jdbc.Driver

###jdbc.url=jdbc:mysql://192.168.206.104:3306/test?useUnicode=true&characterEncoding=utf-8

jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

3 项目文件

在路径CableManagementSystem/src/main/java/com/cable/mvc下创建??????????个文件夹,分别是

  • entity(dao层,model用于数据表的实体类构建)
  • mapper(dao层,用于构建SQL的方法)
  • service(service层,调用Dao层的接口并提供接口给controller层。每个模型都有一个service接口,每个接口分别封装各自的业务处理的方法。)
  • controller(controller层,用于处理request请求,转发视图)。

在路径CableManagementSystem/src/main/resources下创建mapper文件夹,用于存储mapper映射到SQL的xml文件。例如:

在路径CableManagementSystem/src/main/webapp/WEB-INF/templates下配置视图页面,例如

4.代码实例

1)M层的代码:使用lombok组件+@Data注解,免去在model里面写有参构造、无参构造、getter/setter方法

package com.cable.mvc.entity;

import lombok.Data;

import java.util.List;

/**
 * @Author:bingfeng
 * @Date:2024/9/23 15:07
 */

@Data

public class Cable {
    private enum statusenum {normal,faulty,spare};

    private Integer cableID ;
    private String cablename;
    private String systemID;
    private String subsystemID;
    private statusenum status;
    private String usage;
    private Integer uploadID;
    private Integer downloadID;
    private String position;
    private String description;
    private Integer batchnumber;
    private Integer drawingID;
    private Float length;
    private String lengthUnits;
    private String ownner;

    private List<Test> testInfo;

    private List<Maintenance> maintenanceList;

    private List<Installation> installationList;


    //有lombok自动生成构造函数,无需手动添加,可直接使用我使用的@Data注解
//    public Cable() {
//    }
//
//    public Cable(Integer cableID, String cablename, String systemID, String subsystemID, statusenum status, String usage, Integer uploadID, Integer downloadID, String position, String description, Integer batchnumber, Integer drawingID, Float length, String lengthUnits, String ownner,List<Test> test,List<Maintenance> maintenanceList,List<Installation> installationList) {
//        this.cableID = cableID;
//        this.cablename = cablename;
//        this.systemID = systemID;
//        this.subsystemID = subsystemID;
//        this.status = status;
//        this.usage = usage;
//        this.uploadID = uploadID;
//        this.downloadID = downloadID;
//        this.position = position;
//        this.description = description;
//        this.batchnumber = batchnumber;
//        this.drawingID = drawingID;
//        this.length = length;
//        this.lengthUnits = lengthUnits;
//        this.ownner = ownner;
//        this.testInfo = test;
//        this.maintenanceList=maintenanceList;
//        this.installationList=installationList;
//    }
//
//
//    public Integer getCableID() {
//        return cableID;
//    }
//
//    public void setCableID(Integer cableID) {
//        this.cableID = cableID;
//    }
//
//    public String getCablename() {
//        return cablename;
//    }
//
//    public void setCablename(String cablename) {
//        this.cablename = cablename;
//    }
//
//    public String getSystemID() {
//        return systemID;
//    }
//
//    public void setSystemID(String systemID) {
//        this.systemID = systemID;
//    }
//
//    public String getSubsystemID() {
//        return subsystemID;
//    }
//
//    public void setSubsystemID(String subsystemID) {
//        this.subsystemID = subsystemID;
//    }
//
//    public statusenum getStatus() {
//        return status;
//    }
//
//    public void setStatus(statusenum status) {
//        this.status = status;
//    }
//
//    public String getUsage() {
//        return usage;
//    }
//
//    public void setUsage(String usage) {
//        this.usage = usage;
//    }
//
//    public Integer getUploadID() {
//        return uploadID;
//    }
//
//    public void setUploadID(Integer uploadID) {
//        this.uploadID = uploadID;
//    }
//
//    public Integer getDownloadID() {
//        return downloadID;
//    }
//
//    public void setDownloadID(Integer downloadID) {
//        this.downloadID = downloadID;
//    }
//
//    public String getPosition() {
//        return position;
//    }
//
//    public void setPosition(String position) {
//        this.position = position;
//    }
//
//    public String getDescription() {
//        return description;
//    }
//
//    public void setDescription(String description) {
//        this.description = description;
//    }
//
//    public Integer getBatchnumber() {
//        return batchnumber;
//    }
//
//    public void setBatchnumber(Integer batchnumber) {
//        this.batchnumber = batchnumber;
//    }
//
//    public Integer getDrawingID() {
//        return drawingID;
//    }
//
//    public void setDrawingID(Integer drawingID) {
//        this.drawingID = drawingID;
//    }
//
//    public Float getLength() {
//        return length;
//    }
//
//    public void setLength(Float length) {
//        this.length = length;
//    }
//
//    public String getLengthUnits() {
//        return lengthUnits;
//    }
//
//    public void setLengthUnits(String lengthUnits) {
//        this.lengthUnits = lengthUnits;
//    }
//
//    public String getOwnner() {
//        return ownner;
//    }
//
//    public void setOwnner(String ownner) {
//        this.ownner = ownner;
//    }
//
//
//    public List<Test> getTestInfo() {
//        return testInfo;
//    }
//
//    public void setTestInfo(List<Test> testInfo) {
//        this.testInfo = testInfo;
//    }


}

mapper或者叫Dao的代码

package com.cable.mvc.mapper;

import com.cable.mvc.entity.Cable;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Author:bingfeng
 * @Date:2024/9/23 19:45
 */

@Mapper
public interface CableMapper {

    Cable getCable(int cableID);
    Cable getCableTestComplex(int cableID);
    Cable getCableMaintenanceComplex(int cableID);
    Cable getCableInstallComplex(int cableID);
}

持久化mybatis的SQL映射

<?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.cable.mvc.mapper.CableMapper">

<!--    使用resultMap进行映射:左边一排colomn是数据库里面的名称,右边是java后端里面的驼峰式;-->
    <resultMap id="complexCableTsetRes" type="com.cable.mvc.entity.Cable" >
        <id column="cableID" property="cableID"/>
        <result column="cablename" property="cablename"/>
        <result column="status" property="status"/>
        <collection property="testInfo" ofType="com.cable.mvc.entity.Test">
            <id property="cableID" column="cableID"/>
            <result property="testdate" column="testdate"/>
            <result property="operator1" column="operator1"/>
        </collection>
    </resultMap>
    <select id="getCableTestComplex" resultMap="complexCableTsetRes" parameterType="java.lang.Integer">
        SELECT
            c.cableID ,
            c.cablename ,
            c.status ,
            t.cableID  AS t_cableID,
            t.testdate ,
            t.operator1
        FROM
            cableManagementSystem.cable c
        LEFT JOIN
            cableManagementSystem.test t ON c.cableID  = t.cableID
        WHERE
            c.cableID  = #{cableID}
    </select>

    <!--    使用resultMap进行映射:左边一排colomn是数据库里面的名称,右边是java后端里面的驼峰式;-->
    <resultMap id="complexCableMaiRes" type="com.cable.mvc.entity.Cable" >
        <id column="cableID" property="cableID"/>
        <result column="cablename" property="cablename"/>
        <result column="systemID" property="systemID"/>
        <result column="subsystemID" property="subsystemID"/>
        <result column="status" property="status"/>
        <result column="usage" property="usage"/>
        <result column="uploadID" property="uploadID"/>
        <result column="downloadID" property="downloadID"/>
        <result column="position" property="position"/>
        <result column="description" property="description"/>
        <result column="batchnumber" property="batchnumber"/>
        <result column="drawingID" property="drawingID"/>
        <result column="length" property="length"/>
        <result column="lengthUnits" property="lengthUnits"/>
        <result column="ownner" property="ownner"/>
        <collection property="maintenanceList" ofType="com.cable.mvc.entity.Maintenance">
            <id column="id" property="id"/>
            <result property="cableID" column="cableID"/>
            <result property="faultdate" column="faultdate"/>
            <result property="faultDescription" column="faultDescription"/>
            <result property="repairdate" column="repairdate"/>
            <result property="repairDescription" column="repairDescription"/>
            <result property="operator1" column="operator1"/>
            <result property="operator2" column="operator2"/>
            <result property="operator3" column="operator3"/>
            <result property="status" column="status"/>
            <result property="cost" column="cost"/>
        </collection>
    </resultMap>

    <select id="getCableMaintenanceComplex" resultMap="complexCableMaiRes" parameterType="java.lang.Integer">
        SELECT
            c.cableID ,
            c.status ,
            m.faultdate ,
            m.faultDescription,
            m.repairdate,
            m.repairDescription,
            m.operator1,
            m.status,
            m.cost
        FROM
            cableManagementSystem.cable c
                LEFT JOIN
            cableManagementSystem.maintenance m ON c.cableID  = m.cableID
        WHERE
            c.cableID  = #{cableID}
    </select>

    <!--    使用resultMap进行映射:左边一排colomn是数据库里面的名称,右边是java后端里面的驼峰式;-->
    <resultMap id="complexCableInstallRes" type="com.cable.mvc.entity.Cable" >
        <id column="cableID" property="cableID"/>
        <result column="cablename" property="cablename"/>
        <result column="systemID" property="systemID"/>
        <result column="subsystemID" property="subsystemID"/>
        <result column="status" property="status"/>
        <result column="usage" property="usage"/>
        <result column="uploadID" property="uploadID"/>
        <result column="downloadID" property="downloadID"/>
        <result column="position" property="position"/>
        <result column="description" property="description"/>
        <result column="batchnumber" property="batchnumber"/>
        <result column="drawingID" property="drawingID"/>
        <result column="length" property="length"/>
        <result column="lengthUnits" property="lengthUnits"/>
        <result column="ownner" property="ownner"/>
        <collection property="installationList" ofType="com.cable.mvc.entity.Installation">
            <id column="cableID" property="cableID"/>
            <result property="installationDate" column="installationDate"/>
            <result property="operator1" column="operator1"/>
            <result property="operator2" column="operator2"/>
            <result property="operator3" column="operator3"/>
        </collection>
    </resultMap>

    <select id="getCableInstallComplex" resultMap="complexCableInstallRes" parameterType="java.lang.Integer">
        SELECT
            c.cableID ,
            i.installationDate,
            i.operator1,
            i.operator2,
            i.operator3
        FROM
            cableManagementSystem.cable c
                LEFT JOIN
            cableManagementSystem.installation i ON c.cableID  = i.cableID
        WHERE
            c.cableID  = #{cableID}
    </select>
    <select id="getCable" resultType="com.cable.mvc.entity.Cable" parameterType="java.lang.Integer">
        SELECT * FROM cableManagementSystem.cable c WHERE cableID =#{cableID}
    </select>

</mapper>

测试代码:

import com.cable.mvc.entity.Cable;
import com.cable.mvc.entity.Person;
import com.cable.mvc.mapper.CableMapper;
import com.cable.mvc.mapper.PersonMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

/**
 * @Author:bingfeng
 * @Date:2024/9/23 20:04
 */
public class testCableManagementSystem {

    @Test
    public void testfindComplexInstallById() throws Exception{
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        Cable cable=sqlSession.getMapper(CableMapper.class).getCableInstallComplex(20010003);
        System.out.println("cableID"+ cable.getCableID()+";  installInfo"+cable.getInstallationList());
        sqlSession.close();
    }

    @Test
    public void testfindComplexMaintenanceById () throws Exception{
        //1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.实例化SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.调用builder对象的builder()方法,获取SqlSessionFactory对象
        SqlSessionFactory factory = builder.build(in);
        //4.调用factory对象的openSession()方法,获取SqlSession对象
        SqlSession sqlSession = factory.openSession();

        Cable cable = sqlSession.getMapper(CableMapper.class).getCableMaintenanceComplex(20010003);
        System.out.println("cableID"+ cable.getCableID()+";  maintenance"+cable.getMaintenanceList());
        sqlSession.close();
    }


    @Test
    public void testfindComplexTestById () throws Exception{
        //1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.实例化SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.调用builder对象的builder()方法,获取SqlSessionFactory对象
        SqlSessionFactory factory = builder.build(in);
        //4.调用factory对象的openSession()方法,获取SqlSession对象
        SqlSession sqlSession = factory.openSession();

        Cable cable = sqlSession.getMapper(CableMapper.class).getCableTestComplex(20010003);
        System.out.println("cableID"+ cable.getCableID()+";  cablename"+cable.getCablename()+";  status"+cable.getStatus()+";   testinfo"+cable.getTestInfo());
        sqlSession.close();
    }


    @Test
    public void testfindById () throws Exception{
        //1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.实例化SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.调用builder对象的builder()方法,获取SqlSessionFactory对象
        SqlSessionFactory factory = builder.build(in);
        //4.调用factory对象的openSession()方法,获取SqlSession对象
        SqlSession sqlSession = factory.openSession();

        Cable cable = sqlSession.getMapper(CableMapper.class).getCable(20010001);
        System.out.println("cableID"+ cable.getCableID()+";  cablename"+cable.getCablename()+";  status"+cable.getStatus()+";   testinfo"+cable.getTestInfo());
        sqlSession.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值