Snippets Collections
server.port=4001
spring.jackson.default-property-inclusion = NON_NULL
spring.jpa.defer-datasource-initialization= true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.hbm2ddl.auto=create
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.datasource.initialization-mode=never
spring.datasource.url=jdbc:h2:file:~/boots.db
package com.codecademy.plants.repositories;
import java.util.List; 
import org.springframework.data.repository.CrudRepository;
import com.codecademy.plants.entities.Plant;

public interface PlantRepository extends CrudRepository<Plant, Integer> {
  List<Plant> findByHasFruitTrue();
  List<Plant> findByHasFruitFalse();
  List<Plant> findByQuantityLessThan(Integer quantity);
  List<Plant> findByHasFruitTrueAndQuantityLessThan(Integer quantity);
  List<Plant> findByHasFruitFalseAndQuantityLessThan(Integer quantity);
}

//Controller
@GetMapping("/plants/search")
  public List<Plant> searchPlants(
    @RequestParam(name="hasFruit", required = false) Boolean hasFruit,
    @RequestParam(name="maxQuantity", required = false) Integer quantity
  ) {
    if (hasFruit != null && quantity != null && hasFruit) {
      return this.plantRepository.findByHasFruitTrueAndQuantityLessThan(quantity);
    }
    if (hasFruit != null && quantity != null && !hasFruit) {
      return this.plantRepository.findByHasFruitFalseAndQuantityLessThan(quantity);
    }
    if (hasFruit != null && hasFruit) {
      return this.plantRepository.findByHasFruitTrue();
    }
    if (hasFruit != null && !hasFruit) {
      return this.plantRepository.findByHasFruitFalse();
    }
    if (quantity != null) {
      return this.plantRepository.findByQuantityLessThan(quantity);
    }
    return new ArrayList<>();
  }
//Repository
List<Person> findByEyeColorAndAgeLessThan(String eyeColor, Integer age);
//Controller
@GetMapping("/people/search")
public List<Person> searchPeople(
  @RequestParam(name = "eyeColor", required = false) String eyeColor,
  @RequestParam(name = "maxAge", required = false) Integer maxAge 
) {
  if (eyeColor != null && maxAge != null) {
    return this.personRepository.findByEyeColorAndAgeLessThan(eyeColor, maxAge);
  } else if (eyeColor != null) {
    return this.personRepository.findByEyeColor(eyeColor);
  } else if (maxAge != null) {
    return this.personRepository.findByAgeLessThan(maxAge);
  } else {
    return new ArrayList<>();
  }
}
package com.codecademy.people.repositories;
import java.util.List; 
import org.springframework.data.repository.CrudRepository;
import com.codecademy.people.entities.Person;

//Repository folder
public interface PersonRepository extends CrudRepository<Person, Integer> {
  // this declaration is all we need!
  List<Person> findByEyeColor(String eyeColor);
  List<Person> findByAgeLessThan(Integer age); 
}

//Controller folder
	@GetMapping("/people/search")
	public List<Person> searchPeople(@RequestParam(name = "eyeColor", required = false) String eyeColor) {
  if (eyeColor != null) {
    return this.personRepository.findByEyeColor(eyeColor)
  } else {
    return new ArrayList<>();
  }
}

//Advanced Controller folder
@GetMapping("/people/search")
public List<Person> searchPeople(
  @RequestParam(name = "eyeColor", required = false) String eyeColor,
  @RequestParam(name = "maxAge", required = false) Integer maxAge 
) {
  if (eyeColor != null) {
    return this.personRepository.findByEyeColor(eyeColor)
  } else if (maxAge != null) {
    return this.personRepository.findByAgeLessThan(maxAge);
  } else {
    return new ArrayList<>();
  }
}

star

Thu Dec 22 2022 19:31:24 GMT+0000 (Coordinated Universal Time)

#java #springboot
star

Thu Dec 22 2022 19:04:27 GMT+0000 (Coordinated Universal Time)

#java #springboot
star

Thu Dec 22 2022 18:20:46 GMT+0000 (Coordinated Universal Time)

#java #springboot
star

Thu Dec 22 2022 18:13:55 GMT+0000 (Coordinated Universal Time)

#java #springboot

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension