{"id":637,"date":"2023-03-23T21:42:05","date_gmt":"2023-03-23T13:42:05","guid":{"rendered":""},"modified":"2023-03-23T21:42:05","modified_gmt":"2023-03-23T13:42:05","slug":"SpringCloud \u521b\u5efaJPA\u5b58\u50a8\u5e93","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/637.html","title":{"rendered":"SpringCloud \u521b\u5efaJPA\u5b58\u50a8\u5e93"},"content":{"rendered":"
\n
\r\npublic <\/span>interface ExchangeValueRepository extends <\/span>JpaRepository<ExchangeValue, Long>\r\n<\/pre>\n<\/code>\n <\/div>\n
\n \u6b65\u9aa42: <\/strong>\u6253\u5f00
\n CurrencyExchageController.java <\/strong>\u6587\u4ef6\u5e76\u81ea\u52a8\u8fde\u63a5
\n ExchageValueRepository <\/strong>\u3002\n <\/div>\n\n\r\n@Autowired\r\nprivate <\/span>ExchangeValueRepository repository;\r\n<\/pre>\n<\/code>\n <\/div>\n
\n \u6b65\u9aa43: <\/strong>\u5728
\n ExcahngeValueRepository.java <\/strong>\u6587\u4ef6\u4e2d\u521b\u5efa
\n \u67e5\u8be2\u65b9\u6cd5<\/strong>\u3002\n <\/div>\n\n\r\nExchangeValue findByFromAndTo(String from, String to);\r\n<\/pre>\n<\/code>\n <\/div>\n
\n \u5728\u4e0a\u9762\u7684\u8bed\u53e5\u4e2d\uff0c
\n ExchangeValue <\/strong>\u662f\u9884\u671f\u7684\u54cd\u5e94\u3002\u6211\u4eec\u5fc5\u987b\u627e\u5230
\n \u4e24\u4e2a<\/strong>\u5217:
\n \u4ece<\/strong>\u548c
\n \u5230<\/strong>\u3002\n <\/div>\n\n \u5982\u679c\u6211\u4eec\u60f3\u5728\u6839\u636e\u5355\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5217\u540d\u3002\u4f8b\u5982:\n <\/div>\n\n\r\nExchangeValue findByFrom (String from);\r\n<\/pre>\n<\/code>\n <\/div>\n
\n ExcahngeValueRepository.java <\/strong>\n <\/div>\n\n\r\npackage <\/span>com.lidihuo.microservices.currencyexchangeservice;\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\npublic <\/span>interface ExchangeValueRepository extends <\/span>JpaRepository<ExchangeValue, Long>\r\n{\r\n\/\/creating query method\r\nExchangeValue findByFromAndTo(String from, String to);\r\n}\r\n<\/pre>\n<\/code>\n <\/div>\n
\n \u6b65\u9aa44: <\/strong>\u5728
\n CurrencyExchangeController.java <\/strong>\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5:\n <\/div>\n\n\r\nExchangeValue exchangeValue=repository.findByFromAndTo<\/span>(from,to);\r\n<\/pre>\n<\/code>\n <\/div>\n
\n \u4ee3\u66ff\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5:\n <\/div>\n\n\r\nExchangeValue exchangeValue=new ExchangeValue(1000L, from, to, BigDecimal.valueOf<\/span>(65));\r\n<\/pre>\n<\/code>\n <\/div>\n
\n CurrencyExchangeController.java <\/strong>\n <\/div>\n\n\r\npackage <\/span>com.lidihuo.microservices.currencyexchangeservice;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.core.env.Environment;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n@SpringBootApplication\r\n@RestController \r\npublic <\/span>class <\/span>CurrencyExchangeController \r\n{\r\n@Autowired\r\nprivate <\/span>Environment environment;\r\n@Autowired\r\nprivate <\/span>ExchangeValueRepository repository;\r\n@GetMapping(\"\/currency-exchange\/from\/{from}\/to\/{to}\"<\/span>) \/\/where {from} and {to} are path variable\r\npublic <\/span>ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to) \/\/from map to USD and to map to INR\r\n{ \r\nExchangeValue exchangeValue = repository.findByFromAndTo<\/span>(from, to);\r\n\/\/setting the port\r\nexchangeValue.setPort<\/span>(Integer.parseInt<\/span>(environment.getProperty<\/span>(\"local.server.port\"<\/span>)));\r\nreturn <\/span>exchangeValue;\r\n}\r\n}\r\n<\/pre>\n<\/code>\n <\/div>\n
\n \u6b65\u9aa45: <\/strong>\u91cd\u65b0\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u4ee5\u83b7\u53d6\u66f4\u6539\u3002\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u7136\u540e\u8f93\u5165URI
\n http:\/\/localhost:8000\/currency-exchange\/from\/USD\/to\/INR <\/strong>\u3002\u5b83\u8fd4\u56de\u4ee5\u4e0b\u54cd\u5e94:\n <\/div>\n
<\/p>\n
\n \u6211\u4eec\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u5176\u4ed6\u65b9\u6cd5\u901a\u8fc7\u5c06URI\u4e2d\u7684\u8d27\u5e01
\n USD <\/strong>\u66f4\u6539\u4e3a
\n EUR <\/strong>\u8fdb\u884c\u8f6c\u6362\u3002\n <\/div>\n\n http:\/\/localhost:8000\/currency-exchange\/from\/EUR\/to\/INR <\/strong>\u3002\n <\/div>\n\n \u5b83\u8fd4\u56de\u4ee5\u4e0b\u54cd\u5e94:\n <\/div>\n
<\/p>\n
\n \u5728\u4e0a\u8ff0\u54cd\u5e94\u4e2d\uff0c\u6211\u4eec\u6b63\u5728\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u503c\u3002\n <\/div>\n\n \u5f53\u6211\u4eec\u5728URI(EUR\/to\/INR)\u4e2d\u4f20\u9012\u8d27\u5e01\u65f6\uff0c\u67e5\u8be2\u5c06\u89e6\u53d1\u5230\u6570\u636e\u5e93\u3002\u8981\u67e5\u770b\u89e6\u53d1\u4e86\u54ea\u4e2a\u67e5\u8be2\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u65e5\u5fd7\u4e2d\u770b\u5230\u8be5\u67e5\u8be2\u3002\n <\/div>\n\n\r\nHibernate: 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=?\r\n<\/pre>\n<\/code>\n <\/div>\n
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringCloud \u521b\u5efaJPA\u5b58\u50a8\u5e93zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[],"class_list":["post-637","post","type-post","status-publish","format-standard","hentry","category-bc_springcloud_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/637"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}