将maven项目变为springboot项目并且搭建一个简单的接口

导读:本篇文章讲解 将maven项目变为springboot项目并且搭建一个简单的接口,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

将maven项目变为springboot项目并且搭建一个简单的接口

1:新建maven项目

2:增加pom.xml文件并且写好application.yml文件

 <dependencies>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>



    </dependencies>

application.yml:

server:
  port: 8087

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/book?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

# SpringBoot整合Mybatis配置
mybatis:
  # 定义别名映射包:实现对象映射
  type-aliases-package: com.ftzlover.java.entity
  # 加载映射文件,一个接口对应一个映射文件
  mapper-locations: classpath:/mappers/*.xml
  # 开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

3:新建页面并编写配置类

Application:

package com.ftzlover.Demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-08-30 6:50
 */
@SpringBootApplication
@MapperScan("com.ftzlover.Demo.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

4:创建mapper service controller entity层以及mappers.yml配置文件

在这里插入图片描述

5:配置好mappers命名空间开始编写实体类

package com.ftzlover.Demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-08-30 6:57
 */
@Data                    //lombok插件
@AllArgsConstructor       //有参构造
@NoArgsConstructor              //无参构造
@Accessors(chain = true)          //链式语法
public class Book {
    private Integer id;
    private String name;
    private String author;
    private String publish;
    private Integer pages;
    private Float price;
    private Integer bookcaseid;
    private Integer abled;

}

6:编写mapper层

package com.ftzlover.Demo.mapper;

import com.ftzlover.Demo.entity.Book;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-08-30 6:52
 */
public interface BookMapper {

    @Select("select * from book")

    List<Book> findbook();
}

mapper.yml:

<?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.ftzlover.Demo.mapper.BookMapper">

</mapper>

7:编写service层

package com.ftzlover.Demo.service;

import com.ftzlover.Demo.entity.Book;

import java.util.List;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-08-30 6:57
 */
public interface BookService {
    List<Book> findbook();
}

8:serviceImpl层

package com.ftzlover.Demo.service.impl;

import com.ftzlover.Demo.entity.Book;
import com.ftzlover.Demo.mapper.BookMapper;
import com.ftzlover.Demo.service.BookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-08-30 7:02
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;


    @Override
    public List<Book> findbook() {
        return bookMapper.findbook();
    }
}

9:编写controller层:

package com.ftzlover.Demo.controller;

import com.ftzlover.Demo.entity.Book;
import com.ftzlover.Demo.service.BookService;
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 ${范涛之}
 * @Description
 * @create 2021-08-30 6:57
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;


    @GetMapping("/find")
    public List<Book> find(){
        return bookService.findbook();
    }


}

10:启动测试

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81110.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!