IDEA创建springboot项目(接口+数据库)

1.new project -》spring Initializr-》jdk版本,点击next

IDEA创建springboot项目(接口+数据库)_第1张图片

2.设置项目属性,点击next

IDEA创建springboot项目(接口+数据库)_第2张图片

3.选择项目需要的依赖(由于项目是springboot+mybatis的Java后台项目依赖如下),点击next

IDEA创建springboot项目(接口+数据库)_第3张图片

IDEA创建springboot项目(接口+数据库)_第4张图片

4.选择项目代码存放路径

IDEA创建springboot项目(接口+数据库)_第5张图片

5.删除无用的工程目录,目录结构如下

IDEA创建springboot项目(接口+数据库)_第6张图片

6.创建对应文件和业务代码

IDEA创建springboot项目(接口+数据库)_第7张图片

6.1设置配置文件(application.yml)

spring:
  profiles:
    active: dev

6.2配置启动端口、数据库信息、mybatis映射、日志(application-dev.yml)

server:
  port: 8082

spring:
  datasource:
    username: root
    password: XX
    url: jdbc:mysql://localhost:3306/project_manage?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.anran.example.model

logging:
  file: /usr/log/example.log
  level:
    root: INFO
    com.anran.projectmanage.mapper: DEBUG

6.3数据库sql

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` varchar(800) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

6.4 下面是业务代码

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.anran
    example
    0.0.1-SNAPSHOT
    example
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


ExampleApplication.java (服务启动引导类)

package com.anran.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class ExampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(ExampleApplication.class, args);
	}

}

TestMapper.xml




    

TestModel.java

package com.anran.example.model;

/**
 * 测试类
 */
public class TestModel {
    /**
     * id
     */
    int id;

    /**
     * 内容
     */
    String content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

TestMapper.java

package com.anran.example.mapper;


import com.anran.example.model.TestModel;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TestMapper {

    TestModel get(int id);
}

TestService.java

package com.anran.example.service;

import com.anran.example.mapper.TestMapper;
import com.anran.example.model.TestModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {
    @Autowired
    private TestMapper testMapper;

    public TestModel get(int id) {
        return testMapper.get(id);
    }
}

TestController.java

package com.anran.example.controller;

import com.anran.example.model.TestModel;
import com.anran.example.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testController")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    TestService testService;
    @RequestMapping("/get.action")
    public TestModel get(@RequestParam("id") int id) {
        TestModel testModel = testService.get(id);
        return testModel;
    }

    @RequestMapping("/test.action")
    public ResponseEntity test() {
        return new ResponseEntity("hello", HttpStatus.OK);
    }
}

7.启动服务

IDEA创建springboot项目(接口+数据库)_第8张图片

IDEA创建springboot项目(接口+数据库)_第9张图片

8.测试接口

http://localhost:8082/testController/get.action?id=1

IDEA创建springboot项目(接口+数据库)_第10张图片

9.IDEA打包springboot服务

IDEA创建springboot项目(接口+数据库)_第11张图片

10.windows启动服务

java -jar example-0.0.1-SNAPSHOT.jar

IDEA创建springboot项目(接口+数据库)_第12张图片

IDEA创建springboot项目(接口+数据库)_第13张图片

11.查询接口

IDEA创建springboot项目(接口+数据库)_第14张图片

你可能感兴趣的:(创建springboot,mysql,+,mybatis)