首页 > 其他分享 >SSM创建配置测试超级无敌详细版本

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

时间:2024-03-04 23:46:11浏览次数:27  
标签:xml 超级 无敌 SSM springframework org import com dataSource

1.创建

image

2.配置tomcat

3.创建webapp

step01,war包

image

step02

image

image

创建web.xml

image

<?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

image

<?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/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util-4.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://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

image

<?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/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util-4.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://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

image

<!--扫描配置类所在的包-->
<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
*/
    @Bean
    DataSource 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;
    }

    @Bean
    SqlSessionFactoryBean getSqlSessionFactory(){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(getDataSource());
        bean.setTypeAliasesPackage("com.einmeer.entity");
        return bean;
    }

    @Bean
    MapperScannerConfigurer getMapperScanner(){
        MapperScannerConfigurer bean = new MapperScannerConfigurer();
        bean.setBasePackage("com.einmeer.mapper");
        return bean;
    }
}

image

mapper.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="换成上面接口的路径">

</mapper>

7.简化entity

下载lombok插件

image

导入jar

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

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

image

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
     */
    @Bean
    DataSource 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;
    }

    @Bean
    SqlSessionFactoryBean getSqlSessionFactory() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(getDataSource());
        // 包别名,不写的话,mapper中返回值类型要写全
        bean.setTypeAliasesPackage("com.einmeer.entity");
        return bean;
    }

    @Bean
    MapperScannerConfigurer 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/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util-4.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://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/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util-4.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://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.测试

image

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 mapper
        PUBLIC "-//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,
        remarks
        from 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,只限一次
    @Resource
    BusinessMapper businessMapper;

    //    调用方法的路径
    @GetMapping("/list")
    String list() {
        System.out.println(businessMapper.list());
        return "index";
    }
}

image

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,只限一次
    @Resource
    BusinessMapper businessMapper;

    //    调用方法的路径
    @GetMapping("/list")
    List<Business> list() {
        return businessMapper.list();
    }
}

image

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>

标签:xml,超级,无敌,SSM,springframework,org,import,com,dataSource
From: https://www.cnblogs.com/qy-blog/p/18053039

相关文章

  • 使用xml文件配置SSM整合
    使用XML文件配置SSM整合。缺点:xml解析低,降低项目响应效率。配置web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLoc......
  • SSM整合之使用配置类替换xml配置文件(2)
    SSM整合就是将MVC三层架构和框架核心API组件交给SpringIoC容器管理!一般需要配置两个IoC容器进行三层架构组件管理。容器名盛放组件web容器web相关组件(controller,springmvc核心组件)root容器业务和持久层相关组件(service,aop,tx,dataSource,mybatis,mapper等)w......
  • SQL Server 管理套件(SSMS)
    当您按照之前章节的步骤顺利安装完SQLServer2014后,要做的第一件事就是需要打开SQLServer管理套件,并且要知道怎样去使用它。SQLServer管理套件是什么?SQLServer的管理套件(SSMS)是SQLServer的主管理控制台;SSMS中您可以创建数据库对象(如数据库,表,存储过程,视图等),在数据......
  • windows系统下 nssm 注册jar为windows服务并且守护进程
    1、下载nssmhttp://nssm.cc/download下载nssm2.24(2014-08-31)即可解压放在E:\nssm-2.242、新建启动jar脚本的bat文件D:\code2\1.bat内容是java-jarD:\code2\app.jar>log.log2>&1&\3、用nssm命令挂服务cdE:\nssm-2.24\win64nssminstallceshi"D:\code2\1.b......
  • 联通光猫-重置超级密码
    目的实现光猫超级密码重置,通过10010人工客服也不能获取到超级登陆密码。需要reset光猫环境运营商->中国联通设备型号->ZXHNF677V9地区->杭州步骤记录光猫相关信息背面记录,默认WIFI、登陆信息记录PPPoE联机名称x_INTERNET_R_VIP_tag//tag很重要认证注册LOID......
  • 2024年全网最新的百度网盘超级会员SVIP底价购买一个月仅需18元起低价购买的方式,百度网
    由于资源敏感,如果有需要可以关注公众号“沙雕表情包专业户”回复**百度网盘SVIP**,即可获得低于市场价格的优惠渠道。  **新用户:**svip会员连续包年188元,可省110元,平均一天0.51元svip会员连续包季48元,可省40元,平均一天0.53元svip会员连续包月18元,可省12元,平均一天0.6元**老......
  • 2024年全网最新的百度网盘超级会员SVIP一个月仅需18元起低价购买的方式,百度网盘SVIP大
    百度网盘如果是从官方渠道购买的话价格会比较贵**官方渠道:**SVIP一个月要40元,连续包月也要30元,SVIP一个季度要88元,连续包季也要68元SVIP一个年度要360元,连续包年也要263元 由于资源敏感,如果有需要可以关注公众号“沙雕表情包专业户”回复**百度网盘SVIP**,即可获得低于市场价格......
  • 揭秘!KubeSphere 背后的“超级大脑”:etcd 的魅力与力量
    作者:尹珉,KubeSphereAmbassador&Contributor,KubeSphere社区用户委员会杭州站站长。1.开篇:揭开神秘面纱,etcd如何驱动KubeSphere高效运转在云原生时代,etcd作为Kubernetes生态中不可或缺的核心组件,扮演着KubeSphere集群“神经系统”的角色。它利用Raft一致性算法提......
  • 华为云GaussDB支撑农行超级网银业务,性能和稳定性备受认可
    数据库作为数据驱动业务创新和智慧银行建设的关键基础设施,在银行数字化变革中具有举足轻重的作用。在金融科技发展和国家政策引领下,银行纷纷加快推进数字化转型时代,正在经历一场以科技引领、数据赋能、数字经营为目标的智慧银行数字化变革。数据库作为数据驱动业务创新和智慧银......
  • 基于SSM的网上商城系统毕业设计论文【范文】
    摘要在数字经济时代背景下,电子商务迅猛发展,网上商城作为其重要组成部分,对于促进商业活动、满足消费者需求起到了关键作用。本文围绕基于Spring、SpringMVC和MyBatis(简称SSM)框架的网上商城系统的设计与实现进行研究,旨在提供一个高效、稳定、易维护的电子商务平台。文章首先介绍了......