^ Specifics of DAO
- There is no "one DAO, one DTO" restriction
- CRUD and Custom/Raw SQL can be mixed
- DAO classes can be declared directly in "sdm.xml" or in external XML files using "ref"
<sdm>
<dto-class name="Order" ref="get_orders.sql" />
<dto-class name="OrderPosition" ref="get_order_positions.sql" />
<dto-class name="Product" ref="get_products.sql" />
<dao-class name="OrderDao">
<crud dto="Order" table="orders" />
<query-dto-list method="get_orders(date1, date2)" ref="get_orders.sql" dto="Order" />
<exec-dml method="update_order_state(order_id)" ref="update_order_state.sql" />
<crud dto="OrderPosition" table="order_positions" />
<query-dto-list method="get_order_positions(order_id)" ref="get_order_positions.sql" dto="OrderPosition" />
</dao-class>
<dao-class name="ProductDao" ref="ProductsDao.xml"/>
</sdm>^ DAO, CRUD
To generate DAO CRUD methods, use the tag 'crud'. Here it is with every attribute and child element spelled out:
<crud dto="Order" table="orders" fetch-generated="true" >
<create method="createOrder" />
<read-all method="readOrderList" />
<read method="readOrder" />
<update method="updateOrder" />
<delete method="deleteOrder" />
</crud>- When 'fetch-generated' is 'true' (the default), the code that fetches auto-incremented values after INSERT is generated as well.
- A CRUD method is not generated if a PK is required but cannot be detected.
- The CRUD-UPDATE method is not generated if all columns of the data table belong to the PK.
-
The attribute <crud table=..." is optional.
E.g., this one
<crud dto="gorm-Project"/>
is the same as this one
<crud dto="gorm-Project" table="*"/>
and means that "projects" is taken from <dto-class...
<dto-class name="gorm-Project" ref="projects"/>
This is how to generate a complete set of CRUD methods:
<crud dto="Order"/>
This is how to enable only "create" and "delete":
<crud dto="Order">
<create method="createOrder" />
<delete method="deleteOrder" />
</crud>This is how to enable only "create" and "delete" using the default method names:
<crud dto="Order">
<create/>
<delete/>
</crud>^ DAO, Custom SQL
XML tags:
| query | The generated method returns a single scalar value | For SQL statements returning a 1-column ResultSet, such as SELECT. Only the first record is fetched. |
| query-list | The generated method returns a list of scalar values | For SQL statements returning a 1-column ResultSet, such as SELECT. All resulting records are fetched. |
| query-dto | The generated method returns a single DTO | For SQL statements returning a 1..n column ResultSet, such as SELECT. Only the first record is fetched. |
| query-dto-list | The generated method returns a list of DTOs | For SQL statements returning a 1..n column ResultSet. All resulting records are fetched. |
| exec-dml | The generated method returns the number of rows affected. | For DML statements such as INSERT, UPDATE, or DELETE; or for SQL statements that return nothing, such as DDL statements. |
Examples:
<query-dto-list method="get_tests_by_rating(rating)" dto="Test"
ref="select * from get_tests_by_rating(?)"/> |
The attribute 'method' specifies the name of the generated method and its parameters, if there are any. The SQL statement is provided in 'ref'. |
<query-dto-list method="get_all_categories" dto="Category"
ref="get_all_categories.sql"/> |
The SQL statement is located in an external file. |
<exec-dml method="get_rs(p_rating, on_test:Test)"
ref="begin SP_GET_RS_TEST(?, ?); end;"/> |
Calling a stored procedure. See sp-udf.html for details. |
<query-dto-list method="get_tests_by_rating(double rating)" dto="Test"
ref="select * from get_tests_by_rating(?)"/> |
The code generator tries to detect parameter types from JDBC metadata, but sometimes you have to specify them explicitly. |
<query method="get_sound(Long t_id)" return-type="byte []"
ref="get_sound.sql"/> |
For 'query' and 'query-list', you can define/redefine the types of return values. |
<query method="get_orders_count(d1, d2)" external-sql="true"
ref="order/get_orders_count.sql"/> |
The attribute 'external-sql' is used when you build the SQL statement dynamically, in your own code. |
<query-dto-list method="get_customers(company, region)" dto="Customer"
ref="customers(company_name, region)"/> |
SQL-shortcut for 'select * from customers where company_name=? and region=?': Possible SQL-shortcuts: // table -> field1, field2, ... // table(param1, param2, ...) // table(param1, param2, ...) -> field1, field2, ... NOTE. For SQL-shortcuts, column and parameter info is taken directly from the data table. Use them as a workaround when the same info obtained from an SQL statement is incorrect or incomplete. |