SSM整合、环境配置以及详细综合测试(单表查询、多表查询和数据分页、前后端分离、Vue3)

news2024/10/7 12:28:38

SSM整合、环境配置以及基础综合测试

准备:创建maven项目以及项目框架准备

在这里插入图片描述

SSM整合简介

介绍:

  • SSM(Spring+SpringMVC+MyBatis) 整合,就是三个框架协同开发。
  • Spring整合Mybatis就是将Mybatis核心配置文件当中数据源的配置、事务处理、以及工厂的配置,Mapper接口的实现类等交给Spring管理。即spring.xml
  • Spring整合SpringMVC,就是在web.xml当中添加监听器 ,当服务启动,监听触发,监听器执行了Spring的核心配置文件,核心配置文件被加载。即springmvc.xml

整合核心步骤:

  1. Spring基础框架单独运行
  2. SpringMVC框架单独运行
  3. Spring整合SpringMVC框架
  4. Spring整合Mybatis框架

添加依赖和配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.etime</groupId>
    <artifactId>day13</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-version>5.2.5.RELEASE</spring-version>
        <mybatis-version>3.4.6</mybatis-version>
    </properties>
    <dependencies>
        <!--mybatis相关包-->
        <!--mysql的驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
            <scope>runtime</scope>
        </dependency>
        <!--mybatis核心-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis-version}</version>
        </dependency>
        <!--连接池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--日志包-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>

        <!--spring相关的-->
        <!--springIOC包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!--织入器包:-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <!--springmvc依赖:-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!--解析器包-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!--文件上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--spring整合mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--spring整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
    </dependencies>

</project>

jdbc.properties:与数据库建立连接的密码驱动等的属性配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_418?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=h123456

spring.xml配置文件配置扫描包

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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: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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.etime.service"></context:component-scan>
</beans>

新建实体类Student

package com.etime.pojo;

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

//注解注入无参构造函数
@NoArgsConstructor
//注解注入全参构造函数
@AllArgsConstructor
//注解注入get,set等方法
@Data
public class Student {
    private int sid;
    private String sname;
    private int cid;
}

创建接口StudentService.java:查询所有学生信息

package com.etime.service;

import com.etime.pojo.Student;

import java.util.List;

public interface StudentService {
    List<Student> getAllStudent();
}

创建StudentService.java接口的实现类StudentServiceImpl.java并继承StudentService且实现里面的方法getAllStudent

package com.etime.service.impl;

import com.etime.pojo.Student;
import com.etime.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> getAllStudent() {
        System.out.println("这是service实现类里面的方法");
        return null;
    }
}

测试:测试通过spring的配置是否可行

package com.etime.test;

import com.etime.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//通过@RunWith注解的方式加载注入SpringJunit4ClalssRunner.class文件对spring进行测试
@RunWith(SpringJUnit4ClassRunner.class)
//通过注解@ContextConfiguration注解加载文件spring.xml
@ContextConfiguration("classpath:spring.xml")
public class SsmTest {

    //使用自动注入注解@Autowired对接口StudentService进行注入
    @Autowired
    private StudentService studentService;

    //使用@Test单元注解的方式对实现层的方法调用
    @Test
    public void t01(){
        studentService.getAllStudent();
    }
}

单独测试service结果是spring的单独测试下面将对它进行整合:如下的测试结果表名确实是通过spring.xml配置文件可以测试当前接口实现类里面的方法

在这里插入图片描述

在web.xml中配置核心控制器(DispatcherServlet):通过web.xml中配置核心控制器控制加载springmvc的配置文件,加载web.xml同时也加载springmvc.xml文件

<!--配置前端控制器: -->
<!--    将配置文件springmvc.xml文件整合到项目中当web.xml被加载的同时,springmvc.xml也同样被控制器内进行加载拿到配置文件内的内容-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加载springmvc的配置文件:-->
        <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的映射路径-->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

创建springmvc核心配置文件:配置扫描控制层的处理文件,以及处理器,但是后期用了前后端分离的模式来讲解,所以其实,配置视图解析器的但是没有用到。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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: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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置注解扫描器-->
<!--    目的是通过扫描器能找到控制层包下的所有文件,然后根据文件对其进行其它操作-->
    <context:component-scan base-package="com.etime.controller"></context:component-scan>
    <!--处理器配置-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--配置视图解析器:-->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

创建控制器整理数据,对数据进行简单处理

package com.etime.controller;

import com.etime.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("t02")
    @ResponseBody
    public Student test(){
        return new Student(1,"mchh",2);
    }
}

这里测试工具我使用Apifox进行接口测试:如果想要使用该工具的,大家可以去该路径下我有写在哪可以下载,以及使用:

https://blog.csdn.net/m0_56245143/article/details/130270652?spm=1001.2014.3001.5501

运行结果:

在这里插入图片描述

整合Spring 和 SpringMVC:web.xml中添加监听器的方式当在加载的web.xml的时候就能将所需要加载的配置加载上,使用servletContext的监听器,是在该项目第一次被访问的时候就加载的,监听到的时候就 对该文件的其它配置进行加载

在web.xml中进行配置:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <!--配置了一个监听器: ServletContext-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

修改service实现类方法代码及控制器代码并测试:准备一些简单测试数据对进行测试,因为目前所做的还是为了后面整合ssm做准备的。

在StudentServiceImpl.java中进行修改

package com.etime.service.impl;

import com.etime.pojo.Student;
import com.etime.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> getAllStudent() {
        //准备多个学生数据信息进行测试
        Student student1= new Student(1,"cc",2);
        Student student2 = new Student(2,"mc",3);
        Student student3 = new Student(3,"mm",4);
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        return list;
    }
}

对控制层也进行微量的调节,获取到service返回的数据

对StudentController.java进行修改:

package com.etime.controller;

import com.etime.pojo.Student;
import com.etime.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    
    //本来如果没有用注解的情况下,如果要对service的数据进行访问时,就只能进行对StudentService类进行创建新对象
    //可以使用@Autowired注解的方式对service进行引入
    @Autowired
    private StudentService studentService;

    @RequestMapping("t02")
    @ResponseBody
    public List<Student> test(){
        return studentService.getAllStudent();
    }
}

运行结果:

在这里插入图片描述

该次测试将打通数据访问层将mybatis也整合进来:
编写数据访问层的接口

StudentMapper.java

package com.etime.mapper;

import com.etime.pojo.Student;

import java.util.List;

public interface StudentMapper {
    List<Student> getAllStudent();
}

编写mybatis映射配置文件:这里有两种方式,一种使用配置文件的方式,另一种方式就是使用纯注解的方式进行数据 访问层的操作。我这里采用的方式是直接使用配置文件的方式:

如图所示的创建StudentMappe.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.etime.mapper.StudentMapper">
    <select id="getAllStudent" resultType="Student">
        select * from student
    </select>
</mapper>

将Spring和mybatis配置进行整合

准备连接数据的配置属性文件,注意根据个人即将测试的数据,修改该数据库的名称,密码和用户等

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_418?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=h123456

在spring.xml中配置连接数据库,准备数据源

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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: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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.etime.service"></context:component-scan>

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

    <!--数据源-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--mybatis的核心工厂对象-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--别名-->
        <property name="typeAliasesPackage" value="com.etime.pojo"/>
        <!--mapper 文件的位置-->
        <property name="mapperLocations" value="classpath:com/etime/mapper/*.xml"/>
        <!--配置分页插件-->
        <property name="plugins">
            <array>
                <bean  class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
</beans>

原本是需要对spring.xml中配置的事务进行处理的使用注解权限,但是这里用不到,配置上一方用到:

<!--事务管理器平台-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入一个数据源-->
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--开启注解式事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

修改service实现类中的方法

StudentServiceImpl.java

package com.etime.service.impl;

import com.etime.mapper.StudentMapper;
import com.etime.pojo.Student;
import com.etime.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    
    //使用@Autowired注解将StudentMapper接口引入
    @Autowired
    private StudentMapper studentMapper;
    
    @Override
    public List<Student> getAllStudent() {
        //准备多个学生数据信息进行测试
        return studentMapper.getAllStudent();
    }
}

启动服务进行测试整合结果:如下图的结果说明还是比较成功的打通了ssm的所有整合过程

在这里插入图片描述

SSM整合测试

使用前后端分离的技术对整合的ssm进行测试:

案例一、使用VScode、idea、js、vue3进行前后端分离的技术对查询所有学生信息进行页面展示:即简单单表操作

使用vue.global.js 、 axios.min.js 、进行页面编写

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/axios.min.js"></script>
    <script src="js/vue.global.js"></script>
</head>
<body>
    <div id="app">
        <table>
            <tr>
                <td>学号</td>
                <td>姓名</td>
                <td>班级编号</td>
            </tr>
            <tr v-for="stu in students">
                <td>{{stu.sid}}</td>
                <td>{{stu.sname}}</td>
                <td>{{stu.cid}}</td>
            </tr>
        </table>
    </div>
    <script>
        const vueApp = Vue.createApp({
            data(){
                return{
                    students:""
                }
            },
            created(){
                axios({
                    url:"http://localhost:8080/day13_war_exploded/student/t02",
                    method:"get"
                }).then(resp => {
                    this.students = resp.data;
                });
            }
        });
        vueApp.mount("#app");
    </script>
</body>
</html>

控制层:

StudentController.java

package com.etime.controller;

import com.etime.pojo.Student;
import com.etime.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
//使用注解@CrossOrigin方式来解决跨域问题
@CrossOrigin
public class StudentController {

    //本来如果没有用注解的情况下,如果要对service的数据进行访问时,就只能进行对StudentService类进行创建新对象
    //可以使用@Autowired注解的方式对service进行引入
    @Autowired
    private StudentService studentService;

    @RequestMapping("t02")
    @ResponseBody
    public List<Student> test(){
        return studentService.getAllStudent();
    }
}

运行结果:由运行结果可以看出单表操作没有问题

在这里插入图片描述

接下来进行 多表查询并进行分页测试,由上可知的 配置中咱们是使用了mybatis分页插件的,可以利用这一工具进行数据分页到html上进行展示:

案例二:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/axios.min.js"></script>
    <script src="js/vue.global.js"></script>
</head>
<body>
    <div id="app">
        <table align="center">
            <tr>
                <td>学号</td>
                <td>姓名</td>
                <td>班级</td>
            </tr>
            <tr v-for="stu in students" align="center">
                <td>{{stu.sid}}</td>
                <td>{{stu.sname}}</td>
                <td>{{stu.classes.cname}}</td>
            </tr>
        </table>
        <div style="width: 500px;margin: auto;">
            <a href="javascript:void(0)" @click="getStudentByPage(1)">首页</a>
            <a href="javascript:void(0)" @click="getStudentByPage(prevPage)">上一页</a>
            {{pageNum}}/{{pageTotal}}
            <a href="javascript:void(0)" @click="getStudentByPage(nextPage)">下一页</a>
            <a href="javascript:void(0)" @click="getStudentByPage(pageTotal)">尾页</a>
        </div>
    </div>
    <script>
        const vueApp = Vue.createApp({
            data(){
                return{
                    students:"",
                    pageNum:"",
                    pageTotal:"",
                    prevPage:"",
                    nextPage:""
                }
            },
            methods:{
                getStudentByPage(page){
                    axios({
                    url:"http://localhost:8080/day13_war_exploded/student/t02?pageNum="+page+"&pageSize=2",
                    method:"get"
                }).then(resp => {
                    console.log(resp)
                    this.students = resp.data.list;
                    this.pageNum = resp.data.pageNum;
                    this.pageTotal = resp.data.pages;
                    this.prevPage = resp.data.prePage;
                    if(this.prevPage <= 0){
                        this.prevPage =1;
                    }
                    this.nextPage = resp.data.nextPage;
                });
                }
            },
            created(){
                this.getStudentByPage(1);
            }
        });
        vueApp.mount("#app");
    </script>
</body>
</html>

StudentController.java

package com.etime.controller;

import com.etime.pojo.Student;
import com.etime.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
//使用注解@CrossOrigin方式来解决跨域问题
@CrossOrigin
public class StudentController {

    //本来如果没有用注解的情况下,如果要对service的数据进行访问时,就只能进行对StudentService类进行创建新对象
    //可以使用@Autowired注解的方式对service进行引入
    @Autowired
    private StudentService studentService;

    @RequestMapping("t02")
    @ResponseBody
    public PageInfo<Student> test(int pageNum,int pageSize){
        return studentService.getAllStudent(pageNum,pageSize);
    }
}

StudentService.java

package com.etime.service;

import com.etime.pojo.Student;
import com.github.pagehelper.PageInfo;


public interface StudentService {
    PageInfo<Student> getAllStudent(int pageNum,int pageSize);
}

StudentServiceImpl.java

package com.etime.service.impl;

import com.etime.mapper.StudentMapper;
import com.etime.pojo.Student;
import com.etime.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    //使用@Autowired注解将StudentMapper接口引入
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageInfo<Student> getAllStudent(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list = studentMapper.getAllStudent();
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}

StudentMapper.java

package com.etime.mapper;

import com.etime.pojo.Student;

import java.util.List;

public interface StudentMapper {
   List<Student> getAllStudent();
}

StudentMapper.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.etime.mapper.StudentMapper">
    <select id="getAllStudent" resultMap="studentAndClasses">
        select * from student s,classes c where s.cid = c.cid
    </select>

    <resultMap id="studentAndClasses" type="Student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="cid" column="cid"></result>
        <association property="classes" javaType="Classes">
            <id property="cid" column="cid"></id>
            <result property="cname" column="cname"></result>
        </association>
     </resultMap>
</mapper>

并在pojo中创建Classes.java班级实体类

package com.etime.pojo;

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

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Classes {
    private int cid;
    private String cname;
}

测试结果:到目前为止基本的ssm整合对基础的查询都能成功

ntMapper">

select * from student s,classes c where s.cid = c.cid

<resultMap id="studentAndClasses" type="Student">
    <id property="sid" column="sid"></id>
    <result property="sname" column="sname"></result>
    <result property="cid" column="cid"></result>
    <association property="classes" javaType="Classes">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
    </association>
 </resultMap>
```

并在pojo中创建Classes.java班级实体类

package com.etime.pojo;

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

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Classes {
    private int cid;
    private String cname;
}

测试结果:到目前为止基本的ssm整合对基础的查询都能成功

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/441871.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

OpenGL入门教程之 深入三角形

一、引言 本教程使用GLEW和GLFW库。  通过本教程&#xff0c;你能轻松的、深入的理解OpenGL如何绘制一个三角形。  如果你不了解OpenGL是什么&#xff0c;可以阅读OpenGL深入理解。 二、基本函数和语句介绍 通过阅读以下的函数&#xff0c;你的大脑里能留下关于OpenGL基本函…

通过CSIG—走进合合信息探讨生成式AI及文档图像处理的前景和价值

一、前言 最近有幸参加了由中国图象图形学学会&#xff08;CSIG&#xff09;主办&#xff0c;合合信息、CSIG文档图像分析与识别专业委员会联合承办的“CSIG企业行——走进合合信息”的分享会&#xff0c;这次活动以“图文智能处理与多场景应用技术展望”为主题&#xff0c;聚…

安全防御第四天:防病毒网关

一、恶意软件 1.按照传播方式分类 &#xff08;1&#xff09;病毒 病毒是一种基于硬件和操作系统的程序&#xff0c;具有感染和破坏能力&#xff0c;这与病毒程序的结构有关。病毒攻击的宿主程序是病毒的栖身地&#xff0c;它是病毒传播的目的地&#xff0c;又是下一次感染的出…

尚融宝21-整合springcloud

目录 一、整合注册中心nacos 二、整合openFeign &#xff08;一&#xff09;准备工作 &#xff08;二&#xff09;导入依赖 &#xff08;三&#xff09;接口的远程调用 &#xff08;四&#xff09;配置超时控制和日志打印 三、整合Sentinel 四、整合gateway服务网关 …

【Spring从成神到升仙系列 五】从根上剖析 Spring 循环依赖

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱敲代码的小黄&#xff0c;独角兽企业的Java开发工程师&#xff0c;CSDN博客专家&#xff0c;阿里云专家博主&#x1f4d5;系列专栏&#xff1a;Java设计模式、数据结构和算法、Kafka从入门到成神、Kafka从成神到升仙…

基于SpringBoot+Vue家乡特色推荐系统

您好&#xff0c;我是码农飞哥&#xff08;wei158556&#xff09;&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f4aa;&#x1f3fb; 1. Python基础专栏&#xff0c;基础知识一网打尽&#xff0c;9.9元买不了吃亏&#xff0c;买不了上当。 Python从入门到精…

【李老师云计算】HBase+Zookeeper部署及Maven访问(HBase集群实验)

索引 前言1. Zookeeper1.1 主机下载Zookeeper安装包1.2 主机解压Zookeeper1.3 ★解决解压后文件缺失1.4 主机配置Zookeeper文件1.4.1 配置zoo_sample.cfg文件1.4.2 配置/data/myid文件 1.5 主机传输Zookeeper文件到从机1.6 从机修改Zookeeper文件1.6.1 修改zoo.cfg文件1.6.2 修…

一文带你了解MySQL的前世今生,架构,组成部分,特点,适用场景

文章目录 一、MySQL的由来二、MySQL的架构2.1 客户端2.2 服务器 三、 MySQL的主要组成部分3.1 连接管理器3.2 查询缓存3.3 解析器3.4 查询优化器3.5 执行器3.6 存储引擎 四、MySQL的特点五、MySQL的应用场景六、总结 一、MySQL的由来 MySQL最初是由瑞典公司MySQL AB的Michael …

4年功能测试,我一进阶python接口自动化测试,跳槽拿了20k......

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 很多人在这求职市…

让ChatGPT告诉你Java的发展前景

Java版电商购物系统项目实战 最近很多人问我Java的发展前景怎么样&#xff1f;该怎么学Java基础&#xff1f;java这么卷还该不该学等等。那今天老王以电商场景为例&#xff0c;再结合ChatGPT的回答和大家聊的一下Java有哪些应用前景和技术层面的落地方案。&#xff08;在收获干…

【Spring】-- 02 -- Spring中Bean的配置、作用域

一、Bean的配置 Spring用于生产和管理Spring容器中的Bean&#xff0c;需要开发者对Spring的配置文件进行配置。在实际开发中&#xff0c;最常采用XML格式的配置方式&#xff0c;即通过XML文件来注册并管理Bean之间的依赖关系。 在Spring中&#xff0c;XML配置文件的根元素是…

iOS问题记录 - Xcode 14.3版本运行项目报错

文章目录 前言开发环境问题描述问题分析解决方案最后 前言 看到Xcode有新版本&#xff0c;没忍住点了升级&#xff0c;然后问题来了。 开发环境 macOS 13.3Xcode: 14.3 问题描述 Xcode 14.2版本运行项目一切正常&#xff0c;升级到14.3版本后运行报错。 运行到模拟器的报…

【PWN刷题__ret2text】——CTFHub之 简单的 ret2text

萌新第一阶段自然是了解做题的套路、流程&#xff0c;简单题要多做滴 目录 前言 一、checksec查看 二、IDA反汇编 三、exp编写 前言 经典的ret2text流程 一、checksec查看 64位程序&#xff0c;什么保护都没有&#xff0c;No canary found——可以栈溢出控制返回 二、IDA反汇…

“MySQL5.6”、“索引优化”,其实都是索引下推

如果你在面试中&#xff0c;听到“MySQL5.6”、“索引优化” 之类的词语&#xff0c;你就要立马get到&#xff0c;这个问的是“索引下推”。 什么是索引下推 索引下推(Index Condition Pushdown&#xff0c;简称ICP)&#xff0c;是MySQL5.6版本的新特性&#xff0c;它能减少回…

学习实践-Alpaca-Lora (羊驼-Lora)(部署+运行+微调-训练自己的数据集)

Alpaca-Lora模型GitHub代码地址 1、Alpaca-Lora内容简单介绍 三月中旬&#xff0c;斯坦福发布的 Alpaca &#xff08;指令跟随语言模型&#xff09;火了。其被认为是 ChatGPT 轻量级的开源版本&#xff0c;其训练数据集来源于text-davinci-003&#xff0c;并由 Meta 的 LLaMA …

OpenAI对实现强人工智能AGI的规划:《Planing for AGI and beyond》

OpenAI对实现AGI的长期和短期的计划&#xff1a;《Planing for AGI and beyond》 返回论文和资料目录 原文地址 1.导读 OpenAI最近这些年发布了很多令人印象深刻的模型&#xff0c;毫无疑问&#xff0c;OpenAI已经走在了人工智能领域的最前沿。但是很多人只注意到这些模型&…

Nacos Docker Kubernetes ⽣态

博主介绍&#xff1a;✌全网粉丝4W&#xff0c;全栈开发工程师&#xff0c;从事多年软件开发&#xff0c;在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战、定制、远程&#xff0c;博主也曾写过优秀论文&#xff0c;查重率极低&#xff0c;在这方面…

概率密度函数的非参数估计方法

概率密度函数的非参数估计方法 1. Parzen窗方法2. kn近邻估计 \qquad 直接由样本来估计概率密度 p ( x ) p(\boldsymbol{x}) p(x) 的方法&#xff0c;称为非参数方法 (non-parametric method) \text{(non-parametric method)} (non-parametric method)。 \quad ● \quad 概率…

数学建模第三天:数学建模算法篇之线性规划及matlab的实现

目录 一、前言 二、线性规划简介 1、线性规划模型介绍与特征 2、线性规划模型的一般形式 三、单纯形法 1、标准化 2、单纯形法解题 四、matlab解决问题1、matlab线性规划函数 2、解题代码 一、前言 数学建模&#xff0c;本意就是用来解决生活中的问题&#xff0c;我们今…

二叉树的前中后序遍历写法归纳

如题&#xff0c;对应力扣题目如下&#xff1a; 144.二叉树的前序遍历145.二叉树的后序遍历94.二叉树的中序遍历 1.递归 1.1 先序遍历 根 -> 左 -> 右 所以,这个递归函数先打印根节点的值,然后递归地遍历左子树,最后递归地遍历右子树。如果传入的根节点是空,则直接返回…