^ Specifics of DAO
- There is no restriction like "one DAO, one DTO"
- 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_orders.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'. This is how it looks with completely defined attributes and internal elements:
<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' (default), the code responsible for fetching of auto-incremented values after INSERT is generated.
- CRUD method is not generated if PK is required, but cannot be detected.
- CRUD-UPDATE method is not generated if all columns of data table belong to PK.
-
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 using "projects" 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 default names of methods:
<crud dto="Order"> <create/> <delete/> </crud>
^ DAO, Custom SQL
XML tags:
query | Generated method returns single scalar value | For SQL statements returning 1-column ResultSet, such as SELECT. Only the first record is fetched. |
query-list | Generated method returns a list of scalar values | For SQL statements returning 1-column ResultSet, such as SELECT. All resulting records are fetched. |
query-dto | Generated method returns single DTO | For SQL statements returning 1..n columns ResultSet, such as SELECT. Only the first record is fetched. |
query-dto-list | Generated method returns a list of DTO | For SQL statements returning 1..n columns ResultSet. All resulting records are fetched. |
exec-dml | Generated method returns row count affected. | For DML statements, such as INSERT, UPDATE or DELETE; or an SQL statements that return nothing, such as a DDL statements. |
Examples:
<query-dto-list method="get_tests_by_rating(rating)" dto="Test" ref="select * from get_tests_by_rating(?)"/> |
Attribute 'method' specifies the name of generated method and the list of method params, if needed. SQL statement is provided in 'ref'. |
<query-dto-list method="get_all_categories" dto="Category" ref="get_all_categories.sql"/> |
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. Read sp-udf.html for details. |
<query-dto-list method="get_tests_by_rating(double rating)" dto="Test" ref="select * from get_tests_by_rating(?)"/> |
Code generator tries to detect param types using JDBC metadata, but sometime, you may need to specify param types 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"/> |
Attribute 'external-sql' is used if you need to build SQL statements dynamically (in external 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. Column/param info for SQL-shortcuts is obtained directly from data table. It may be used as workaround if such info obtained from SQL is incorrect/incomplete. |