IDEA搭建Springboot开发环境

环境创建

  • Create New Project
    在这里插入图片描述

  • Spring Initializer
    在这里插入图片描述

  • Project Metadata
    在这里插入图片描述

  • Dependencies
    在这里插入图片描述

  • name and location
    在这里插入图片描述

  • 目录结构
    在这里插入图片描述

  • maven配置
    在这里插入图片描述

web示例

我这里有一个mysql数据库,里面有一个user表,以查询此表为例
首先在项目里依次创建controller、service、dao、entity
在这里插入图片描述
controller包里创建HelloController

package com.example.test2.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/index")
    public String sayHello() {
        return "hello you!";
    }
}

修改application.propertiesapplication.yml
编辑application.yml

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://10.180.222.50:3306/dbtest
    username: root
    password: db123
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true

运行项目
在这里插入图片描述
启动后,浏览器上测试HelloController类是否运行。
在这里插入图片描述

进一步的完善
创建UserEntity

 package com.example.test2.entity;

import lombok.Data;

import javax.persistence.*;

@Entity
@Table(name = "user")
@Data
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "password")
    private String password;
}
 

创建UserDao

package com.example.test2.dao;

import com.example.test2.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface UserDao extends JpaRepository<UserEntity,String>,JpaSpecificationExecutor<UserEntity>{

    UserEntity getByName(String name);
}

创建UserService

package com.example.test2.service;

import com.example.test2.dao.UserDao;
import com.example.test2.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public UserEntity getUserEntityByName(String name) {
        return userDao.getByName(name);
    }

    public List<UserEntity> getAllUser() {
        return userDao.findAll();
    }
}

转向对UserService类单项测试
在这里插入图片描述
选择Create New Tests,勾选待测试方法
在这里插入图片描述
补充测试方法

package com.example.test2.service;

import com.example.test2.entity.UserEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    void getUserEntityByName() {
        UserEntity entity = userService.getUserEntityByName("user01");
        System.out.println("name:"+entity.getName()+"***password:"+entity.getPassword());
    }

    @Test
    void getAllUser() {
        List<UserEntity> userEntityList = userService.getAllUser();
        for (UserEntity entity : userEntityList) {
            System.out.println("name:"+entity.getName()+"***password:"+entity.getPassword());
        }
    }

}

之后对方法进行Junit测试的时候会出现空指针异常

java.lang.NullPointerException
   at com.example.test2.service.UserServiceTest.getUserEntityByName(UserServiceTest.java:17)
   ......

1、先将测试主类Test2ApplicationTests的访问权限设置为public
在这里插入图片描述

@SpringBootTest
public class Test2ApplicationTests {

	@Test
	void contextLoads() {
	}

}

2、UserServiceTest类继承测试主类

class UserServiceTest extends Test2ApplicationTests

再次单元测试即可通过

Hibernate: select userentity0_.id as id1_0_, userentity0_.name as name2_0_, userentity0_.password as password3_0_ from user userentity0_ where userentity0_.name=?
name:user01***password:YWRtaW4xMjM=

进一步的完善
创建UserController

package com.example.test2.controller;

import com.example.test2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getusers")
    public Object getUserInfo() {
        return userService.getAllUser();
    }
}
 

再次运行项目,浏览器访问IP:8080/getusers
在这里插入图片描述

打包部署

先执行maven clean,再执行maven package
在这里插入图片描述
target目录下会生成jar
在这里插入图片描述
将jar包上传至某个服务器上,这里以Linux虚拟为例

[root@worker opt]# ll
total 38748
-rw-r--r--. 1 root root 39677552 May 18 15:46 test2-0.0.1-SNAPSHOT.jar
[root@worker opt]# 

启动服务

[root@worker opt]# java -jar test2-0.0.1-SNAPSHOT.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)
 ......
 2020-05-18 15:41:50.411  INFO 29966 --- [           main] com.example.test2.Test2Application       : Started Test2Application in 6.108 seconds (JVM running for 6.6)

浏览器访问虚拟机ip:8080/getusers
在这里插入图片描述
至此开发环境搭建完成,经测试一切都OK。

遇到的问题

如果在写代码的实体类时,使用了lombok插件,但是却找不到实体对应的Get/Set方法时,需要在File>settings>plugins>搜索lombok>找到对应的插件,选择安装,然后重启IDEA即可。

  • 4
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值