Objektové relační mapování (ORM) je základní rámec pro zjednodušení interakce mezi databázemi a aplikacemi. Je široce používán s relačními databázemi a objektově orientovaným programováním (OOP). Usnadňuje život vývojáře tím, že eliminuje potřebu psaní složitých SQL, správu databázových operací, ověřování dat, správu transakcí a mnoho dalšího. Toto zjednodušení je zvláště důležité při vývoji podnikových aplikací, kde organizace hledají rychlý vývoj s konzistentními rámci.
V duchu povídání o tom, jak je život vývojářům usnadněn pomocí ORM, podobně Oracle Autonomous Database usnadňuje vývojářům život tím, že poskytuje nejlepší konvergovanou datovou platformu, která může hostovat všechny typy dat, takže vývojář nemusí obětovat. flexibilitu nebo zabřednutí do provozních požadavků nebo složitých integrací. Pro vývoj podnikových aplikací, kdy potřebujete vysokou bezpečnost, extrémní izolaci a řízení přístupu a jednotnou platformu pro konsolidaci, si můžete přečíst o tom, jak může autonomní databáze ve vyhrazené infrastruktuře (ADB-D) splnit tyto požadavky: Proč je autonomní databáze vyhrazena dokonalý domov.
ORM pro autonomní databázi
V tomto příspěvku na blogu prozkoumáme, jak můžeme použít některé oblíbené nástroje ORM s Autonomní databáze Dedicated k vytváření podnikových aplikací.
Trh má mnoho nástrojů ORM pro různé vývojové platformy, ale my chceme mluvit o dvou populárních nástrojích ORM pro dvě vývojové platformy: SQLAlchemy v Pythonu a Hibernate v Javě. Zde je tedy stručný popis architektury a kódu.
SQLAlchemy v Pythonu
Příklad architektury je nasazen v podnikovém vývojovém prostředí na Autonomous Database Dedicated. Aplikace Python a Java se připojují k samostatným databázím. S touto architekturou můžete nasadit mnoho databází, které se připojují k jejich vlastním aplikacím, které vyhovují různým liniím podniků, skupinám uživatelů atd., což vám poskytne skutečně konvergovanou datovou platformu.
SQLAlchemy je sada nástrojů Python SQL a objektový relační mapovač. V následujícím kódu připojíme vyhrazenou autonomní databázi pomocí SQLAlchemy a vybereme data z tabulky EMPLOYEE, vložíme data a aktualizujeme data, to vše pomocí nástroje.
<span style=“color: #4b69c6;“>from</span><span style=“color: #333333;“> sqlalchemy</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>engine </span><span style=“color: #4b69c6;“>import</span><span style=“color: #333333;“> create_engine</span>
<span style=“color: #4b69c6;“>from</span><span style=“color: #333333;“> sqlalchemy </span><span style=“color: #4b69c6;“>import</span><span style=“color: #333333;“> Column</span><span style=“color: #777777;“>,</span><span style=“color: #333333;“> String</span><span style=“color: #777777;“>,</span><span style=“color: #333333;“> Integer</span>
<span style=“color: #4b69c6;“>from</span><span style=“color: #333333;“> sqlalchemy</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>ext</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>declarative </span><span style=“color: #4b69c6;“>import</span><span style=“color: #333333;“> declarative_base</span>
<span style=“color: #4b69c6;“>from</span><span style=“color: #333333;“> sqlalchemy</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>orm </span><span style=“color: #4b69c6;“>import</span><span style=“color: #333333;“> sessionmaker</span>
<span style=“color: #aaaaaa;font-style: italic;“># use this connection scheme for simplicity for the reader</span>
<span style=“color: #aaaaaa;font-style: italic;“># create Autonomous Database Engine</span>
<span style=“color: #7a3e9d;“>engine</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> create_engine</span><span style=“color: #777777;“>(</span>
<span style=“color: #7a3e9d;“>f</span><span style=“color: #448c27;“>’oracle+oracledb://:@'</span><span style=“color: #777777;“>,</span>
<span style=“color: #7a3e9d;“>thick_mode</span><span style=“color: #777777;“>=</span><span style=“color: #9c5d27;“>False</span><span style=“color: #777777;“>,</span>
<span style=“color: #7a3e9d;“>connect_args</span><span style=“color: #777777;“>={</span>
<span style=“color: #777777;“>“</span><span style=“color: #448c27;“>user</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>:</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>“</span><span style=“color: #448c27;“>SCHEMA_NAME</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>,</span>
<span style=“color: #777777;“>“</span><span style=“color: #448c27;“>password</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>:</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>“</span><span style=“color: #448c27;“>PASSWORD</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>,</span>
<span style=“color: #777777;“>“</span><span style=“color: #448c27;“>host</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>:</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>“</span><span style=“color: #448c27;“>ADB-D HOST NAME</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>,</span>
<span style=“color: #777777;“>“</span><span style=“color: #448c27;“>port</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>:</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>“</span><span style=“color: #448c27;“>1521</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>,</span>
<span style=“color: #777777;“>“</span><span style=“color: #448c27;“>service_name</span><span style=“color: #777777;“>“</span><span style=“color: #777777;“>:</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>“</span><span style=“color: #448c27;“>ADB-D SERVICE NAME</span><span style=“color: #777777;“>“</span>
<span style=“color: #777777;“>})</span>
<span style=“color: #aaaaaa;font-style: italic;“># Create a base class for your models</span>
<span style=“color: #7a3e9d;“>Base</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> declarative_base</span><span style=“color: #777777;“>()</span>
<span style=“color: #aaaaaa;font-style: italic;“># Creating an ORM for Employee Table</span>
<span style=“color: #7a3e9d;“>class</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;font-weight: bold;“>EmployeeDetails</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;font-weight: bold;“>Base</span><span style=“color: #777777;“>):</span>
<span style=“color: #7a3e9d;“>__tablename__</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>'</span><span style=“color: #448c27;“>EMPLOYEE_DETAILS</span><span style=“color: #777777;“>'</span><span style=“color: #333333;“> </span>
<span style=“color: #7a3e9d;“>employeeId</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> Column</span><span style=“color: #777777;“>(</span><span style=“color: #333333;“>Integer</span><span style=“color: #777777;“>,</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>primary_key</span><span style=“color: #777777;“>=</span><span style=“color: #9c5d27;“>True</span><span style=“color: #777777;“>)</span>
<span style=“color: #7a3e9d;“>firstName</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> Column</span><span style=“color: #777777;“>(</span><span style=“color: #333333;“>String</span><span style=“color: #777777;“>(</span><span style=“color: #9c5d27;“>100</span><span style=“color: #777777;“>))</span>
<span style=“color: #7a3e9d;“>lastName</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> Column</span><span style=“color: #777777;“>(</span><span style=“color: #333333;“>String</span><span style=“color: #777777;“>(</span><span style=“color: #9c5d27;“>100</span><span style=“color: #777777;“>))</span>
<span style=“color: #7a3e9d;“>employeeDesignation</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> Column</span><span style=“color: #777777;“>(</span><span style=“color: #333333;“>String</span><span style=“color: #777777;“>(</span><span style=“color: #9c5d27;“>100</span><span style=“color: #777777;“>))</span>
<span style=“color: #7a3e9d;“>Session</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> sessionmaker</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>bind</span><span style=“color: #777777;“>=</span><span style=“color: #7a3e9d;“>engine</span><span style=“color: #777777;“>)</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>Session</span><span style=“color: #777777;“>()</span>
<span style=“color: #aaaaaa;font-style: italic;“># If the table doesn’t exist, this will create the EMPLOYEE Table</span>
<span style=“color: #7a3e9d;“>Base</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>metadata</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>create_all</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>engine</span><span style=“color: #777777;“>)</span>
<span style=“color: #aaaaaa;font-style: italic;“># Query to create the record</span>
<span style=“color: #7a3e9d;“>new_record</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;font-weight: bold;“>EmployeeDetails</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>employeeId</span><span style=“color: #777777;“>=</span><span style=“color: #9c5d27;“>1</span><span style=“color: #777777;“>,</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>firstName</span><span style=“color: #777777;“>=</span><span style=“color: #777777;“>'</span><span style=“color: #448c27;“>Brad</span><span style=“color: #777777;“>'</span><span style=“color: #777777;“>,</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>lastName</span><span style=“color: #777777;“>=</span><span style=“color: #777777;“>'</span><span style=“color: #448c27;“>Pitt</span><span style=“color: #777777;“>'</span><span style=“color: #777777;“>,</span>
<span style=“color: #7a3e9d;“>employeeDesignation</span><span style=“color: #777777;“>=</span><span style=“color: #777777;“>'</span><span style=“color: #448c27;“>Senior Artist</span><span style=“color: #777777;“>'</span><span style=“color: #777777;“>)</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>add</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>new_record</span><span style=“color: #777777;“>)</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>commit</span><span style=“color: #777777;“>()</span>
<span style=“color: #aaaaaa;font-style: italic;“># Query to update the record</span>
<span style=“color: #7a3e9d;“>record_to_update</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>query</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;font-weight: bold;“>EmployeeDetails</span><span style=“color: #777777;“>).</span><span style=“color: #333333;“>filter_by</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>employeeId</span><span style=“color: #777777;“>=</span><span style=“color: #9c5d27;“>1</span><span style=“color: #777777;“>).</span><span style=“color: #333333;“>first</span><span style=“color: #777777;“>()</span>
<span style=“color: #4b69c6;“>if</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>record_to_update</span><span style=“color: #777777;“>:</span>
<span style=“color: #7a3e9d;“>record_to_update</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>employeeDesignation </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>'</span><span style=“color: #448c27;“>Manager</span><span style=“color: #777777;“>'</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>commit</span><span style=“color: #777777;“>()</span>
<span style=“color: #aaaaaa;font-style: italic;“># Query to delete the record</span>
<span style=“color: #7a3e9d;“>record_to_delete</span><span style=“color: #333333;“> </span><span style=“color: #777777;“>=</span><span style=“color: #333333;“> </span><span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>query</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;font-weight: bold;“>EmployeeDetails</span><span style=“color: #777777;“>).</span><span style=“color: #333333;“>filter_by</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>employeeId</span><span style=“color: #777777;“>=</span><span style=“color: #9c5d27;“>1</span><span style=“color: #777777;“>).</span><span style=“color: #333333;“>first</span><span style=“color: #777777;“>()</span>
<span style=“color: #7a3e9d;“>record_to_delete</span><span style=“color: #777777;“>:\</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>delete</span><span style=“color: #777777;“>(</span><span style=“color: #7a3e9d;“>record_to_delete</span><span style=“color: #777777;“>)</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>commit</span><span style=“color: #777777;“>()</span>
<span style=“color: #aaaaaa;font-style: italic;“># Close the session</span>
<span style=“color: #7a3e9d;“>session</span><span style=“color: #777777;“>.</span><span style=“color: #333333;“>close</span><span style=“color: #777777;“>()</span>
Hibernace v Javě
Nyní si povíme něco o Hibernate, která se hojně využívá při vývoji podnikových aplikací v Javě. V tomto kódu propojujeme vyhrazenou autonomní databázi s Hibernate pomocí Spring JPA k vkládání dat do tabulky CUSTOMER pomocí API a vybíráme data ze stejné tabulky pomocí jiného API.
<span style="color: #aaaaaa;font-style: italic;">/**</span>
<span style="color: #aaaaaa;font-style: italic;">* Application to connect to Oracle Autonomous Database using SpringJPA and Hibernate </span>
<span style="color: #aaaaaa;font-style: italic;">*/</span><span style="color: #333333;"> </span>
<span style="color: #aaaaaa;font-style: italic;">/** </span>
<span style="color: #aaaaaa;font-style: italic;">* application.properties file: used to setup Database properties like URL, Username, Password, etc</span>
<span style="color: #aaaaaa;font-style: italic;">*/</span><span style="color: #333333;"> </span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">main</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">banner</span><span style="color: #777777;">-</span><span style="color: #333333;">mode</span><span style="color: #777777;">=</span><span style="color: #333333;">off</span>
<span style="color: #aaaaaa;font-style: italic;">// Oracle Autonomous Database settings</span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">datasource</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">url</span><span style="color: #777777;">=</span><span style="color: #333333;">jdbc</span><span style="color: #4b69c6;">:</span><span style="color: #333333;">oracle</span><span style="color: #4b69c6;">:</span><span style="color: #333333;">thin</span><span style="color: #4b69c6;">:</span><span style="color: #777777;">@</span><span style="color: #7a3e9d;">javaapp_medium</span><span style="color: #4b69c6;">?</span><span style="color: #333333;">TNS_ADMIN</span><span style="color: #777777;">=<</span><span style="color: #333333;">WALLET_PATH</span><span style="color: #777777;">></span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">datasource</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">username</span><span style="color: #777777;">=<</span><span style="color: #333333;">USERNAME</span><span style="color: #777777;">></span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">datasource</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">password</span><span style="color: #777777;">=<</span><span style="color: #333333;">PASSWORD</span><span style="color: #777777;">></span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">datasource</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">driver</span><span style="color: #777777;">-</span><span style="color: #333333;">class</span><span style="color: #777777;">-</span><span style="color: #333333;">name</span><span style="color: #777777;">=</span><span style="color: #7a3e9d;">oracle</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">jdbc</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">driver</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">OracleDriver</span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">jpa</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">hibernate</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">ddl</span><span style="color: #777777;">-</span><span style="color: #333333;">auto</span><span style="color: #777777;">=</span><span style="color: #333333;">update</span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">jpa</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">show</span><span style="color: #777777;">-</span><span style="color: #333333;">sql</span><span style="color: #777777;">=</span><span style="color: #9c5d27;">true</span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">jpa</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">properties</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">hibernate</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">format_sql</span><span style="color: #777777;">=</span><span style="color: #9c5d27;">true</span>
<span style="color: #7a3e9d;">spring</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">jpa</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">properties</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">hibernate</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">dialect</span><span style="color: #777777;">=</span><span style="color: #7a3e9d;">org</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">hibernate</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">dialect</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">Oracle12cDialect</span>
<span style="color: #aaaaaa;font-style: italic;">//logging</span>
<span style="color: #7a3e9d;">logging</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">pattern</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">console</span><span style="color: #777777;">=%</span><span style="color: #333333;">d</span><span style="color: #777777;">{</span><span style="color: #333333;">yyyy</span><span style="color: #777777;">-</span><span style="color: #333333;">MM</span><span style="color: #777777;">-</span><span style="color: #333333;">dd HH</span><span style="color: #4b69c6;">:</span><span style="color: #333333;">mm</span><span style="color: #4b69c6;">:</span><span style="color: #333333;">ss</span><span style="color: #777777;">}</span><span style="color: #333333;"> </span><span style="color: #777777;">%-</span><span style="color: #333333;">5level </span><span style="color: #777777;">%</span><span style="color: #333333;">logger</span><span style="color: #777777;">{</span><span style="color: #9c5d27;">36</span><span style="color: #777777;">}</span><span style="color: #333333;"> </span><span style="color: #777777;">-</span><span style="color: #333333;"> </span><span style="color: #777777;">%</span><span style="color: #333333;">msg</span><span style="color: #777777;">%</span><span style="color: #333333;">n</span>
<span style="color: #7a3e9d;">logging</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">level</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">org</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">hibernate</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">SQL</span><span style="color: #777777;">=</span><span style="color: #333333;">debug</span>
<span style="color: #7a3e9d;">logging</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">level</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">org</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">hibernate</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">type</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">descriptor</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">sql</span><span style="color: #777777;">=</span><span style="color: #333333;">trace</span>
<span style="color: #7a3e9d;">logging</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">level</span><span style="color: #777777;">.=</span><span style="color: #333333;">error</span>
<span style="color: #aaaaaa;font-style: italic;">/** </span>
<span style="color: #aaaaaa;font-style: italic;">* DemoApplication.java file: consists of main class</span>
<span style="color: #aaaaaa;font-style: italic;">*/</span><span style="color: #333333;"> </span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">SpringBootApplication</span>
<span style="color: #4b69c6;">public</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">class</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;font-weight: bold;">DemoApplication</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #4b69c6;">public</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">static</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">void</span><span style="color: #333333;"> </span><span style="color: #aa3731;font-weight: bold;">main</span><span style="color: #777777;">(</span><span style="color: #7a3e9d;">String</span><span style="color: #777777;">[]</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">args</span><span style="color: #777777;">)</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #7a3e9d;"> SpringApplication</span><span style="color: #777777;">.</span><span style="color: #aa3731;font-weight: bold;">run</span><span style="color: #777777;">(</span><span style="color: #7a3e9d;">DemoApplication</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">class</span><span style="color: #777777;">,</span><span style="color: #333333;"> args</span><span style="color: #777777;">);</span>
<span style="color: #777777;"> }</span>
<span style="color: #777777;">}</span>
<span style="color: #aaaaaa;font-style: italic;">/** </span>
<span style="color: #aaaaaa;font-style: italic;">* CustomerService.java file: consists of services like fetch and save that'll get from or insert/update the database</span>
<span style="color: #aaaaaa;font-style: italic;">*/</span><span style="color: #333333;"> </span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">Service</span>
<span style="color: #4b69c6;">public</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">class</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;font-weight: bold;">CustomerService{</span>
<span style="color: #333333;"> @Autowired</span>
<span style="color: #4b69c6;"> private</span><span style="color: #333333;"> CustomerRepository customerRepository;</span>
<span style="color: #4b69c6;"> public</span><span style="color: #333333;"> Long save(CustomerDto customerDto) </span><span style="color: #777777;">{</span>
<span style="color: #7a3e9d;"> Customer</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customer</span><span style="color: #333333;"> </span><span style="color: #777777;">=</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">new</span><span style="color: #333333;"> </span><span style="color: #aa3731;font-weight: bold;">Customer</span><span style="color: #777777;">();</span>
<span style="color: #333333;"> customer.</span><span style="color: #aa3731;font-weight: bold;">setName</span><span style="color: #777777;">(</span><span style="color: #7a3e9d;">customerDto</span><span style="color: #333333;">.</span><span style="color: #7a3e9d;">getName</span><span style="color: #777777;">());</span>
<span style="color: #333333;"> customer.</span><span style="color: #aa3731;font-weight: bold;">setAddress</span><span style="color: #777777;">(</span><span style="color: #7a3e9d;">customerDto</span><span style="color: #333333;">.</span><span style="color: #7a3e9d;">getAddress</span><span style="color: #777777;">());</span>
<span style="color: #7a3e9d;"> Customer</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">saved</span><span style="color: #333333;"> </span><span style="color: #777777;">=</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerRepository</span><span style="color: #777777;">.</span><span style="color: #aa3731;font-weight: bold;">save</span><span style="color: #777777;">(</span><span style="color: #333333;">customer</span><span style="color: #777777;">);</span>
<span style="color: #333333;"> return saved.</span><span style="color: #aa3731;font-weight: bold;">getId</span><span style="color: #777777;">();</span>
<span style="color: #777777;"> }</span>
<span style="color: #4b69c6;"> public</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">Optional</span><span style="color: #777777;"><</span><span style="color: #333333;">Customer</span><span style="color: #777777;">></span><span style="color: #333333;"> </span><span style="color: #aa3731;font-weight: bold;">fetch</span><span style="color: #777777;">(</span><span style="color: #7a3e9d;">Long</span><span style="color: #333333;"> id</span><span style="color: #777777;">)</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #7a3e9d;"> Optional</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Customer</span><span style="color: #777777;">></span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">optionalCustomer</span><span style="color: #333333;"> </span><span style="color: #777777;">=</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerRepository</span><span style="color: #777777;">.</span><span style="color: #aa3731;font-weight: bold;">findById</span><span style="color: #777777;">(</span><span style="color: #333333;">id</span><span style="color: #777777;">);</span>
<span style="color: #4b69c6;"> return</span><span style="color: #333333;"> optionalCustomer</span><span style="color: #777777;">;</span>
<span style="color: #777777;"> }</span>
<span style="color: #333333;">}</span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">Repository</span>
<span style="color: #4b69c6;">public</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">interface</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;font-weight: bold;">CustomerRepository</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">extends</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;font-weight: bold;">JpaRepository</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Customer</span><span style="color: #777777;">,</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">Long</span><span style="color: #777777;">>{</span>
<span style="color: #777777;">}</span>
<span style="color: #aaaaaa;font-style: italic;">/** </span>
<span style="color: #aaaaaa;font-style: italic;">* CustomerController.java file: consists of REST API path to get from or insert/update the database that leverages the services from CustomerService.java </span>
<span style="color: #aaaaaa;font-style: italic;">*/</span><span style="color: #333333;"> </span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">RestController</span><span style="color: #777777;">()</span>
<span style="color: #4b69c6;">public</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">class</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;font-weight: bold;">CustomerController</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #777777;"> @</span><span style="color: #7a3e9d;">Autowired</span>
<span style="color: #4b69c6;"> private</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">CustomerService</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerService</span><span style="color: #777777;">;</span>
<span style="color: #777777;"> @</span><span style="color: #7a3e9d;">PostMapping</span><span style="color: #777777;">(</span><span style="color: #9c5d27;">path</span><span style="color: #777777;">=</span><span style="color: #777777;">"</span><span style="color: #448c27;">/customer</span><span style="color: #777777;">"</span><span style="color: #777777;">)</span>
<span style="color: #7a3e9d;"> ResponseEntity</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Long</span><span style="color: #777777;">></span><span style="color: #333333;"> </span><span style="color: #aa3731;font-weight: bold;">create</span><span style="color: #777777;">(@</span><span style="color: #7a3e9d;">RequestBody</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">CustomerDto</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerDto</span><span style="color: #777777;">)</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #7a3e9d;">Long</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerId</span><span style="color: #333333;"> </span><span style="color: #777777;">=</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerService</span><span style="color: #777777;">.</span><span style="color: #aa3731;font-weight: bold;">save</span><span style="color: #777777;">(</span><span style="color: #333333;">customerDto</span><span style="color: #777777;">);</span>
<span style="color: #4b69c6;">return</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">new</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">ResponseEntity</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Long</span><span style="color: #777777;">>(</span><span style="color: #333333;">customerId</span><span style="color: #777777;">,</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">HttpStatus</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">CREATED</span><span style="color: #777777;">);</span>
<span style="color: #777777;">}</span>
<span style="color: #777777;"> @</span><span style="color: #7a3e9d;">GetMapping</span><span style="color: #777777;">(</span><span style="color: #9c5d27;">path</span><span style="color: #777777;">=</span><span style="color: #777777;">"</span><span style="color: #448c27;">/customer/{id}</span><span style="color: #777777;">"</span><span style="color: #777777;">)</span>
<span style="color: #7a3e9d;"> ResponseEntity</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Optional</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Customer</span><span style="color: #777777;">>></span><span style="color: #333333;"> </span><span style="color: #aa3731;font-weight: bold;">fetch</span><span style="color: #777777;">(@</span><span style="color: #7a3e9d;">PathVariable</span><span style="color: #777777;">(</span><span style="color: #777777;">"</span><span style="color: #448c27;">id</span><span style="color: #777777;">"</span><span style="color: #777777;">)</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">Long</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">id</span><span style="color: #777777;">)</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #7a3e9d;">Optional</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Customer</span><span style="color: #777777;">></span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">optCustomer</span><span style="color: #333333;"> </span><span style="color: #777777;">=</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">customerService</span><span style="color: #777777;">.</span><span style="color: #aa3731;font-weight: bold;">fetch</span><span style="color: #777777;">(</span><span style="color: #333333;">id</span><span style="color: #777777;">);</span>
<span style="color: #4b69c6;"> return</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">new</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">ResponseEntity</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Optional</span><span style="color: #777777;"><</span><span style="color: #7a3e9d;">Customer</span><span style="color: #777777;">>>(</span><span style="color: #333333;">optCustomer</span><span style="color: #777777;">,</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">HttpStatus</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">OK</span><span style="color: #777777;">);</span>
<span style="color: #777777;"> }</span>
<span style="color: #777777;">}</span>
<span style="color: #aaaaaa;font-style: italic;">/** </span>
<span style="color: #aaaaaa;font-style: italic;">* Customer.java file: consists of constructor class required to match the database metadata</span>
<span style="color: #aaaaaa;font-style: italic;">*/</span><span style="color: #333333;"> </span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">Entity</span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">Data</span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">NoArgsConstructor</span>
<span style="color: #777777;">@</span><span style="color: #7a3e9d;">AllArgsConstructor</span>
<span style="color: #4b69c6;">public</span><span style="color: #333333;"> </span><span style="color: #4b69c6;">class</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;font-weight: bold;">Customer</span><span style="color: #333333;"> </span><span style="color: #777777;">{</span>
<span style="color: #777777;"> @</span><span style="color: #7a3e9d;">Id</span>
<span style="color: #777777;"> @</span><span style="color: #7a3e9d;">GeneratedValue</span><span style="color: #777777;">(</span><span style="color: #9c5d27;">strategy</span><span style="color: #333333;"> </span><span style="color: #777777;">=</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">GenerationType</span><span style="color: #777777;">.</span><span style="color: #7a3e9d;">SEQUENCE</span><span style="color: #777777;">)</span>
<span style="color: #4b69c6;"> private</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">Long</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">id</span><span style="color: #777777;">;</span>
<span style="color: #4b69c6;"> private</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">String</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">name</span><span style="color: #777777;">;</span>
<span style="color: #4b69c6;"> private</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">String</span><span style="color: #333333;"> </span><span style="color: #7a3e9d;">address</span><span style="color: #777777;">;</span>
<span style="color: #777777;">}</span>
Zdroj: Oracle