public interface ExchangeValueRepository extends JpaRepository<ExchangeValue, Long>
@Autowired private ExchangeValueRepository repository;
ExchangeValue findByFromAndTo(String from, String to);
ExchangeValue findByFrom (String from);
package com.lidihuo.microservices.currencyexchangeservice; import org.springframework.data.jpa.repository.JpaRepository; public interface ExchangeValueRepository extends JpaRepository<ExchangeValue, Long> { //creating query method ExchangeValue findByFromAndTo(String from, String to); }
ExchangeValue exchangeValue=repository.findByFromAndTo(from,to);
ExchangeValue exchangeValue=new ExchangeValue(1000L, from, to, BigDecimal.valueOf(65));
package com.lidihuo.microservices.currencyexchangeservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class CurrencyExchangeController { @Autowired private Environment environment; @Autowired private ExchangeValueRepository repository; @GetMapping("/currency-exchange/from/{from}/to/{to}") //where {from} and {to} are path variable public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to) //from map to USD and to map to INR { ExchangeValue exchangeValue = repository.findByFromAndTo(from, to); //setting the port exchangeValue.setPort(Integer.parseInt(environment.getProperty("local.server.port"))); return exchangeValue; } }
Hibernate: select exchangeva0_.id as id1_0_, exchangeva0_.conversion_multiple as conversi2_0_, exchangeva0_.currency_from as currency3_0_, exchangeva0_.port as port4_0_, exchangeva0_.currency_to as currency5_0_ from exchange_value exchangeva0_ where exchangeva0_.currency_from=? and exchangeva0_.currency_to=?