zoukankan      html  css  js  c++  java
  • SpringBoot整合SSM(代码实现Demo)

    SpringBoot整合SSM

    如图所示:

    一、数据准备:

    数据库文件:数据库名:saas-export,表名:ss_company

    创建表语句:

    DROP TABLE IF EXISTS ss_company;
    CREATE TABLE ss_company (
      id varchar(40) NOT NULL COMMENT 'ID',
      name varchar(255) DEFAULT NULL COMMENT '公司名称',
      expiration_date datetime DEFAULT NULL COMMENT '到期时间',
      address varchar(255) DEFAULT NULL COMMENT '公司地址',
      license_id varchar(255) DEFAULT NULL COMMENT '营业执照-图片',
      representative varchar(255) DEFAULT NULL COMMENT '法人代表',
      phone varchar(255) DEFAULT NULL COMMENT '公司电话',
      company_size varchar(255) DEFAULT NULL COMMENT '公司规模',
      industry varchar(255) DEFAULT NULL COMMENT '所属行业',
      remarks varchar(255) DEFAULT NULL COMMENT '备注',
      state int(2) DEFAULT '1' COMMENT '状态',
      balance double DEFAULT NULL COMMENT '当前余额',
      city varchar(20) DEFAULT NULL,
      PRIMARY KEY (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    数据插入语句:

    INSERT INTO ss_company VALUES ('1', '字节跳动', null, '北京', 'xxx002', '张某', '123', '10000人以上', '互联网', '互联网公司', '1', '100', '北京');
    INSERT INTO ss_company VALUES ('2', '百度', null, '北京市海淀区', 'bzd001', '李某', '110', '5000-10000人', '计算机', '', '1', '100', '北京');
    INSERT INTO ss_company VALUES ('3', '阿里巴巴', null, '中国杭州市滨江区', 'bzd002', '马某', '110', '5000-10000人', '电子商务', '', '1', '100', '杭州');
    INSERT INTO ss_company VALUES ('4', '腾讯', null, '深圳市南山区', 'bzd003', '马某', '110', '5000-10000人', '游戏', '', '1', '100', '深圳');

    二、创建SpringBoot工程步骤:

    1、起步依赖:导入SpringBoot所需的pom.xml文件

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent><properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties><dependencies>
        <!-- **web的起步依赖springMVC -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency><!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--springboot支持的是jpa,mybatisplus自己做了启动器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency><!--spring-test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency><!--添加lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
    </dependencies>
    <!--maven插件Spring Boot应用提供了执行Maven操作的可能-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    2、配置application.yml文件

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/saas-export?characterEncoding=utf-8
        username: root
        password: root
    ​
    server:
      port: 8088

    3、配置启动类ApplicationTest

    package com.darren;
    ​
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    ​
    @SpringBootApplication
    @MapperScan("com.darren.dao")
    public class ApplicationTest {
        public static void main(String[] args) {
            SpringApplication.run(ApplicationTest.class,args);
        }
    }

    二、整合SSM

    备注:利用MyBatis逆向工程生成对应的pojo实体类、dao接口以及对应的映射文件

    1、创建pojo实体类

    package com.darren.pojo;
    ​
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.Data;
    ​
    import java.io.Serializable;
    import java.util.Date;
    @Data
    @TableName("ss_company")
    public class Company implements Serializable {
        /**
         * AUTO : AUTO(0, “数据库ID自增”),
         * INPUT : INPUT(1, “用户输入ID”),
         * ID_WORKER : ID_WORKER(2, “全局唯一ID”),默认值如果不设置会在用该策略
         * UUID : UUID(3, “全局唯一ID”),
         * NONE : NONE(4, “该类型为未设置主键类型”),
         * ID_WORKER_STR : ID_WORKER_STR(5, “字符串全局唯一ID”);
         */
    ​
        @TableId(type = IdType.UUID)
        private String id;
    ​
        private String name;
    ​
        private Date expirationDate;
    ​
        private String address;
    ​
        private String licenseId;
    ​
        private String representative;
    ​
        private String phone;
    ​
        private String companySize;
    ​
        private String industry;
    ​
        private String remarks;
    ​
        private Integer state;
    ​
        private Double balance;
    ​
        private String city;
    ​
        private static final long serialVersionUID = 1L;
    }

    2、创建Result类(建议)

    备注:Result类主要是在使用postman测试的时候,给出提示信息

    package com.darren.pojo;
    ​
    import java.io.Serializable;
    ​
    public class Result implements Serializable {
    ​
        private boolean success;   //状态判断
        private String message;    //返回的消息内容
    ​
    ​
        public Result(boolean success,String message) {
            this.message = message;
            this.success = success;
        }
    ​
        public String getMessage() {
            return message;
        }
    ​
        public void setMessage(String message) {
            this.message = message;
        }
    ​
        public boolean isSuccess() {
            return success;
        }
    ​
        public void setSuccess(boolean success) {
            this.success = success;
        }
    }

    3、创建Dao接口

    备注:由于com.baomidou.mybatisplus.core.mapper包已经封装了CRUD功能,所以这里的Dao接口只需继承对应的BaseMapper<Company>接口就可以了

    package com.darren.dao;
    ​
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.darren.pojo.Company;
    ​
    public interface CompanyDao extends BaseMapper<Company> {
    }

    4、创建Service接口

    package com.darren.service;
    ​
    import com.darren.pojo.Company;
    ​
    import java.util.List;
    ​
    public interface CompanyService {
        //查询所有
        List<Company> findAll();
    ​
        //单一查询
        Company findOne(String id);
    ​
        //更新
        void update(Company company);
    ​
        //添加
        void add(Company company);
    ​
        //删除
        void delete(String id);
    }

    5、创建ServiceImpl实现类

    package com.darren.service.impl;
    ​
    import com.darren.dao.CompanyDao;
    import com.darren.pojo.Company;
    import com.darren.service.CompanyService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    ​
    import java.util.List;
    @Service
    public class CompanyServiceImpl implements CompanyService {
        @Autowired
        private CompanyDao companyDao;
        @Override
        public List<Company> findAll() {
            return companyDao.selectList(null);
        }
    ​
        @Override
        public Company findOne(String id) {
            return companyDao.selectById(id);
        }
    ​
        @Override
        public void update(Company company) {
            companyDao.updateById(company);
        }
    ​
        @Override
        public void add(Company company) {
            companyDao.insert(company);
        }
    ​
        @Override
        public void delete(String id) {
            companyDao.deleteById(id);
        }
    }

    6、创建Controller类

    package com.darren.controller;
    ​
    import com.darren.pojo.Company;
    import com.darren.pojo.Result;
    import com.darren.service.CompanyService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    ​
    import java.util.List;
    ​
    @RestController
    @RequestMapping("/company")
    public class CompanyController {
        @Autowired
        private CompanyService companyService;
    ​
        /**
         * RESTful风格编程
         *      查询:get
         *      修改:put
         *      添加:post
         *      删除:delete
         * @return
         *///查询所有
        @GetMapping
        public List<Company> findAll(){
            return companyService.findAll();
        }
    ​
        //查询单一
        @GetMapping("/{id}")
        public Company findById(@PathVariable String id){
            return companyService.findOne(id);
        }
    ​
        //添加
        @PostMapping
        public Result add(@RequestBody Company company){
            try {
                companyService.add(company);
                return new Result(true,"新增成功");
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"新增失败");
            }
        }
    ​
        //修改
        @PutMapping
        public Result update(@RequestBody Company company){
            try {
                companyService.update(company);
                return new Result(true,"修改成功");
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"修改失败");
            }
        }
    ​
        //删除
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable String id){
            try {
                companyService.delete(id);
                return new Result(true,"删除成功");
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"删除失败");
            }
        }
    }

    三、测试整合结果

    启动ApplicationTest启动类,由于没有前端页面,所以这里使用postman工具进行测试

    1、测试 "查询" 结果:

    2、测试 "增加" 结果

    3、测试 "修改" 结果

    4、测试 "删除" 结果

     

  • 相关阅读:
    校园网认证原理
    使VS Code自带的Markdown支持mathjax
    图像处理实用资源
    第一周的python作业
    Python中关于turtle库的笔记
    软件测试入门篇
    如何设计一条好的测试用例打卡第二天
    根据用户登录浅谈软件测试打卡第一天
    Andorid源码系列:View的onTouchEvent()与performClick(),performLongClick()调用时机解析
    论Activity及启动模式,Fragment,Service的使用以及生命周期
  • 原文地址:https://www.cnblogs.com/cndarren/p/11674801.html
Copyright © 2011-2022 走看看