当前位置: 首页 > 工具软件 > SSMP > 使用案例 >

SpringBoot整合ssmp

范高刚
2023-12-01

SpringBoot整合ssmp
ssmp指:Spring+SpringMVC+Mybatis-plus

pom文件添加指定依赖项
Mybatis-plus
lombok

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
    </parent>

    <groupId>com.java</groupId>
    <artifactId>BuyFlightTicketSys-ssmp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Domain层

在类名上添加lombok的注解@Data,自动添加getter/setter
@TableField表示值就是数据库的字段名

@Data
public class Account {
    @TableField("firstName")
    private String firstName;
    @TableField("lastName")
    private String lastName;
    private String email;
    private String password;
 }

Dao层

添加@Mapper注解,并继承MyBatis-Plus的BaseMapper<数据模型类名>, 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
(可以继续写入自定义代码,不影响原始操作)

@Mapper
public interface AccountDao extends BaseMapper<Account> {
	@Select("select EXISTS(select * from tbl_account where email=#{email} )")
    Boolean checkExistAccount(String email);
}

Service层

按照一个接口一个实现类搭配操作
接口继承IService<数据模型类名>

AccountService接口


public interface AccountService extends IService<Account> {

    /**
     * Check database whether existing account
     * @param email
     * @return
     */
    boolean checkExistAccount(String email);
}

AccountServiceImpl实现类继承ServiceImpl<Mapper接口, 数据模型类名>,实现接口


@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {

    @Autowired
    private AccountDao accountDao;
    
    @Override
    public boolean checkExistAccount(String email) {
        boolean existAccount = accountDao.checkExistAccount(email);
        return existAccount;
    }
}

Controller层

按照Rest风格开发
在类名上添加这两个注解,
@RestController
@RequestMapping(“/accounts”)
(注意:地址必须在最后带s,如accounts)

select: @GetMapping()
update:@PutMapping()
delete: @DeleteMapping()
insert: @PostMapping()

@RestController
@RequestMapping("/accounts")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping()
    public R login(@RequestBody Account account) {
        return new R(accountService.loginByEmail(account.getEmail(), account.getPassword()));
    }

    @GetMapping("{email}/{op}")
    public R checkExistAccount(@PathVariable String email, @PathVariable int op) {
        return new R(accountService.checkExistAccount(email));
    }

    @GetMapping("{email}")
    public R findFirstnameByEmail(@PathVariable String email) {
        String firstname = accountService.findFirstnameByEmail(email);
        return new R(true, firstname);
    }

    @PostMapping
    public R save(@RequestBody Account account){
        boolean flag = accountService.save(account);
        return new R(flag, flag ? "注册成功^_^" : "注册失败-_-!");
    }

}

在Controller包下创建一个Result类,用来封装各种JSON 格式类型的返回结果,包括请求成功和请求失败的结果,以及响应的数据和消息。

package com.java.controller.utils;

import lombok.Data;

/**
 * @author Kyle
 * @date 2023/4/22 19:36
 */
@Data
public class Result {
    private Boolean flag;
    private Object data;
    private String msg;

    public Result() {

    }

    public Result(Boolean flag) {
        this.flag = flag;
    }

    public Result(Boolean flag, Object data) {
        this.flag = flag;
        this.data = data;
    }

    public Result(String msg, Boolean flag) {
        this.flag = flag;
        this.msg = msg;
    }

    public Result(String msg) {
        this.msg = msg;
    }
}

属性配置

在application.yml文件中对项目的端口等信息进行配置

server:
  port: 80

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/project_db?serverTimezone=UTC
    username: root
    password: root

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
      id-type: auto
 类似资料: