K
K
KaizerSX2020-01-28 16:37:55
IntelliJ IDEA
KaizerSX, 2020-01-28 16:37:55

How to set application.properties settings in intellig idea community edition?

Good afternoon!

Connected via Maven Spring Boot, JPA. Created the necessary classes, entities and controller. Without connecting the database, Spring Boot works fine, but when I try to connect data from the database to the project, I don’t understand how to make IDEA accept settings from application.properties. The fact is that the settings glow as gray, supposedly not used. Esteemed that in Community Edition it is impossible to register settings through application.properties. Can anyone suggest alternative ways? And it’s a shame, Spring Boot works, but I can’t work with the database.

spring.h2.console.enabled=true

spring.datasource.url=jdbc:h2:./SalesPointscds
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=pass

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect


CONTROLLER:

package RestExample.MainPack.Controller;

import RestExample.MainPack.model.SALESPOINTDO;
import RestExample.MainPack.repos.SalesPointRepos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

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

@RestController
public class GreetingController {

    @Autowired
    SalesPointRepos salesPointRepos;


    @GetMapping("/greeting")
    @ResponseBody
    public String greeting() {


        Iterable<SALESPOINTDO> allSP=salesPointRepos.findAll();

        StringBuilder sb= new StringBuilder();

        allSP.forEach(sp->sb.append(sp+"<br>"));


        return  sb.toString();
    }
}


ENTITY:

package RestExample.MainPack.model;

import javax.persistence.*;

@Entity
@Table(name = "SALESPOINTDO")
public class SALESPOINTDO {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Integer ID;

 @Column(name = "NAME")
 private String name;

 @Column(name = "CITY")
 private String city;

 @Column(name = "ADDRESS")
 private String address;

    public SALESPOINTDO() {
    }

    public Integer getID() {
        return ID;
    }

    public void setID(Integer ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "City: "+getCity()+" "+"Address: "+getAddress();
    }
}


REPOSITORY:


package RestExample.MainPack.repos;

import RestExample.MainPack.model.SALESPOINTDO;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface SalesPointRepos extends CrudRepository<SALESPOINTDO,Integer> {





}


STARTSPRINGBOOT:

package RestExample.MainPack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication
public class StartRest {

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



    }
}


Screenshot from database:
5e3281a312ba7609794864.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2020-01-28
kuzmin @sergueik

KaizerSX file in
src\main\resources\application.properties?
opens like

Properties properties = new Properties();
    InputStream input = null;
    try {
      input = <имя вашего класса>.class.getClassLoader()
          .getResourceAsStream("application.properties");
      properties .load(input);

      
          System.err.println(properties .getProperty("spring.h2.console.enabled"));

what does it print?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question