SSM创建配置测试超级无敌详细版本

2024-03-05 15:52

本文主要是介绍SSM创建配置测试超级无敌详细版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.创建

在这里插入图片描述

2.配置tomcat

3.创建webapp

step01,war包

在这里插入图片描述

step02

在这里插入图片描述

在这里插入图片描述

创建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">
</web-app>

4.构建SpringMVC

导入jar包

<!--springMVC-->
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.23</version>
</dependency><!--servlet-->
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope>
</dependency>

web.xml配置DispacheServlet,核心拦截器

<?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"><!--配置DispacheServlet,核心拦截器--><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></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!--扫描Controller所在包,但是注意包下的类必须有@Controller或者@RestController--><context:component-scan base-package="com.einmeer.controller"></context:component-scan>
</beans>

5.SpringIOC

创建配置文件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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"></beans>

配置监听器,该监听器的作用是在服务器启动的时候读ioc配置文件

继续在web.xml中添加

<!--为监听器配置参数,目的是为了告诉监听器,要读的文件在哪-->
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationcontext.xml</param-value>
</context-param>
<!--配置监听器读applicationcontext.xml-->
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

6.MyBatis

导入jar包

<!--MyBatis-->
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version>
</dependency><!--mysql-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version>
</dependency><!--MyBatis Spring-->
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version>
</dependency><!--orm关系映射-->
<dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>5.3.23</version>
</dependency><!--Druid连接池-->
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version>
</dependency>

在MyConfig中创建3个bean,加入IOC

applicationcontext.xml

在这里插入图片描述

<!--扫描配置类所在的包-->
<context:component-scan base-package="com.einmeer.config"></context:component-scan>

MyConfig

package com.einmeer.config;import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/**
* @author 芊嵛
* @date 2024/3/4
*/
@Configuration
public class MyConfig {/**
* 配置连接池
* @return
*/@BeanDataSource getDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://192.168.21.130:3306/test?useUnicode=true&characterEncoding=utf8");dataSource.setUsername("root");dataSource.setPassword("2459689935");;dataSource.setMinIdle(8);dataSource.setMaxActive(20);return dataSource;}@BeanSqlSessionFactoryBean getSqlSessionFactory(){SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(getDataSource());bean.setTypeAliasesPackage("com.einmeer.entity");return bean;}@BeanMapperScannerConfigurer getMapperScanner(){MapperScannerConfigurer bean = new MapperScannerConfigurer();bean.setBasePackage("com.einmeer.mapper");return bean;}
}

在这里插入图片描述

mapper.xml固定的头

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="换成上面接口的路径"></mapper>

7.简化entity

下载lombok插件

在这里插入图片描述

导入jar

<!--lombok-->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version><scope>provided</scope>
</dependency>

8.截至到目前的配置整合

在这里插入图片描述

8.1MyConfig.java

package com.einmeer.config;import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/*** @author 芊嵛* @date 2024/3/4*/
@Configuration
public class MyConfig {/*** 配置连接池** @return*/@BeanDataSource getDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://192.168.21.130:3306/test?useUnicode=true&characterEncoding=utf8");dataSource.setUsername("root");dataSource.setPassword("2459689935");;dataSource.setMinIdle(8);dataSource.setMaxActive(20);return dataSource;}@BeanSqlSessionFactoryBean getSqlSessionFactory() {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(getDataSource());// 包别名,不写的话,mapper中返回值类型要写全bean.setTypeAliasesPackage("com.einmeer.entity");return bean;}@BeanMapperScannerConfigurer getMapperScanner() {MapperScannerConfigurer bean = new MapperScannerConfigurer();// mapper别名bean.setBasePackage("com.einmeer.mapper");return bean;}
}

8.2applicationcontext.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!--扫描配置类所在的包--><context:component-scan base-package="com.einmeer.config"></context:component-scan>
</beans>

8.3springmvc.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!--扫描Controller所在包,但是注意包下的类必须有@Controller或者@RestController--><context:component-scan base-package="com.einmeer.controller"></context:component-scan>
</beans>

8.4web.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"><!--配置DispacheServlet,核心拦截器--><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></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--为监听器配置参数,目的是为了告诉监听器,要读的文件在哪--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationcontext.xml</param-value></context-param><!--配置监听器读applicationcontext.xml--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

9.测试

在这里插入图片描述

9.1Business.java

package com.einmeer.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.math.BigDecimal;/*** @author 芊嵛* @date 2024/3/4*/
@Data   // get/set方法
@AllArgsConstructor // 有参(全参)
@NoArgsConstructor  // 无参
public class Business {private Integer businessId;private String businessName;private String businessAddress;private String businessExplain;private String businessImg;private Integer orderTypeId;private BigDecimal startPrice;private BigDecimal deliveryPrice;private String remarks;}

9.2BusinessMapper.java

package com.einmeer.mapper;import com.einmeer.entity.Business;import java.util.List;/*** @author 芊嵛* @date 2024/3/4*/
public interface BusinessMapper {// 查询所有信息List<Business> list();
}

9.3BusinessMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 全路径 -->
<mapper namespace="com.einmeer.mapper.BusinessMapper"><!--id代表上面Business接口中的方法名字,必须完全一致;后面的是返回值类型,由于MyConfig文件中配置了因此不用写全路径,写名字就行,不如就得向上面那样从com开始--><select id="list" resultType="Business">select businessId,businessName,businessAddress,businessExplain,businessImg,orderTypeId,startPrice,deliveryPrice,remarksfrom business;</select>
</mapper>

9.4BusinessController

package com.einmeer.controller;import com.einmeer.entity.Business;
import com.einmeer.mapper.BusinessMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;/*** @author 芊嵛* @date 2024/3/4*/
//返回字符串
@RestController
// 多一层路径
@RequestMapping("/business")
public class BusinessController {// 自动new,只限一次@ResourceBusinessMapper businessMapper;//    调用方法的路径@GetMapping("/list")String list() {System.out.println(businessMapper.list());return "index";}
}

在这里插入图片描述

10.输出到页面上

截至目前能打印到控制台,要想出入到页面上需要继续配置

导入jar,转换成JSON字符串

<!--fastjson-->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.32</version>
</dependency>

springmvc.xml配置消息转换器与跨域

<!--配置消息转换器-->
<mvc:annotation-driven><mvc:message-converters><bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"><property name="supportedMediaTypes"><value>application/json;charset-utf-8</value></property></bean></mvc:message-converters>
</mvc:annotation-driven><!--配置跨域-->
<mvc:cors><mvc:mapping path="/**" allowed-origins="*"/>
</mvc:cors>

BusinessController.java修改一下

package com.einmeer.controller;import com.einmeer.entity.Business;
import com.einmeer.mapper.BusinessMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;/*** @author 芊嵛* @date 2024/3/4*/
//返回字符串
@RestController
// 多一层路径
@RequestMapping("/business")
public class BusinessController {// 自动new,只限一次@ResourceBusinessMapper businessMapper;//    调用方法的路径@GetMapping("/list")List<Business> list() {return businessMapper.list();}
}

在这里插入图片描述

11.把sql语句输出到控制窗口

导包

<!--输出sql到控制窗口-->
<dependency><groupId>org.duracloud</groupId><artifactId>common</artifactId><version>7.0.0</version>
</dependency>

12.一次性插入多条数据

<!--添加-->
<!--collection如果是数组array,要是集合list item自定义 以,分割-->
<insert id="businessAdd" parameterType="Business">INSERT INTO business ( businessName, businessAddress, businessExplain, businessImg, orderTypeId, startPrice,deliveryPrice, remarks )VALUES<foreach collection="list" item="abusiness" separator=",">(#{abusiness.businessName},#{abusiness.businessAddress},#{abusiness.businessExplain},#{abusiness.businessImg},#{abusiness.orderTypeId},#{abusiness.startPrice},#{abusiness.deliveryPrice},#{abusiness.remarks})</foreach>;
</insert>

这篇关于SSM创建配置测试超级无敌详细版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777022

相关文章

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

Python创建Excel的4种方式小结

《Python创建Excel的4种方式小结》这篇文章主要为大家详细介绍了Python中创建Excel的4种常见方式,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以学习一下... 目录库的安装代码1——pandas代码2——openpyxl代码3——xlsxwriterwww.cppcns.c

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL