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

打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

1.创建


2.配置tomcat

3.创建webapp

step01,war包

step02


创建web.xml
  1. [/code][size=5]4.构建SpringMVC[/size]
  2. [indent]导入jar包
  3. [/indent][code]    org.springframework    spring-webmvc    5.3.23    javax.servlet    javax.servlet-api    4.0.1    provided
复制代码
web.xml配置DispacheServlet,核心拦截器
  1.                 springmvc        org.springframework.web.servlet.DispatcherServlet                            contextConfigLocation            classpath:springmvc.xml                        springmvc        /   
复制代码
springmvc.xml
  1.         
复制代码
5.SpringIOC

创建配置文件applicationcontext.xml
  1. [/code][indent]配置监听器,该监听器的作用是在服务器启动的时候读ioc配置文件
  2. 继承在web.xml中添加
  3. [/indent][code]    contextConfigLocation    classpath:applicationcontext.xml    org.springframework.web.context.ContextLoaderListener
复制代码
6.MyBatis

导入jar包
  1.     org.mybatis    mybatis    3.5.6    mysql    mysql-connector-java    8.0.28    org.mybatis    mybatis-spring    2.0.6    org.springframework    spring-orm    5.3.23    com.alibaba    druid    1.2.8
复制代码
在MyConfig中创建3个bean,加入IOC
applicationcontext.xml
  1. [/code][indent]MyConfig
  2. [/indent][code]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*/@Configurationpublic 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;    }}
复制代码

mapper.xml固定的头
  1. [/code][size=5]7.简化entity[/size]
  2. [indent]下载lombok插件
  3. [/indent][align=center][img]https://img2024.cnblogs.com/blog/2815669/202403/2815669-20240304233346563-767183280.png[/img][/align]
  4. [indent]导入jar
  5. [/indent][code]    org.projectlombok    lombok    1.18.24    provided
复制代码
8.停止到如今的配置整合


8.1MyConfig.java
  1. 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 */@Configurationpublic 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
  1.         
复制代码
8.3springmvc.xml
  1.         
复制代码
8.4web.xml
  1.                 springmvc        org.springframework.web.servlet.DispatcherServlet                    contextConfigLocation            classpath:springmvc.xml                        springmvc        /                    contextConfigLocation        classpath:applicationcontext.xml                    org.springframework.web.context.ContextLoaderListener   
复制代码
9.测试


9.1Business.java
  1. 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
  1. package com.einmeer.mapper;import com.einmeer.entity.Business;import java.util.List;/** * @author 芊嵛 * @date 2024/3/4 */public interface BusinessMapper {    // 查询所有信息    List list();}
复制代码
9.3BusinessMapper.xml
  1.                 select businessId,        businessName,        businessAddress,        businessExplain,        businessImg,        orderTypeId,        startPrice,        deliveryPrice,        remarks        from business;   
复制代码
9.4BusinessController
  1. 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";    }}
复制代码

10.输出到页面上

停止如今能打印到控制台,要想出入到页面上需要继承配置
导入jar,转换成JSON字符串
  1.     com.alibaba    fastjson    2.0.32
复制代码
springmvc.xml配置消息转换器与跨域
  1.                                         application/json;charset-utf-8                           
复制代码
BusinessController.java修改一下
  1. 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 list() {        return businessMapper.list();    }}
复制代码

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

导包
  1.     org.duracloud    common    7.0.0
复制代码
12.一次性插入多条数据
  1.     INSERT INTO business ( businessName, businessAddress, businessExplain, businessImg, orderTypeId, startPrice,    deliveryPrice, remarks )    VALUES            (#{abusiness.businessName},        #{abusiness.businessAddress},        #{abusiness.businessExplain},        #{abusiness.businessImg},        #{abusiness.orderTypeId},        #{abusiness.startPrice},        #{abusiness.deliveryPrice},        #{abusiness.remarks}        )    ;
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表