Basics
Last updated
Was this helpful?
Last updated
Was this helpful?
Example code is available on .
This article shows how to work with MyBatis in Groovy application. We will fetch data from database and show them on UI.
Create Item
class that will represent a database entity. This class will be used by MyBatis as a data transfare object. MyBatis will automatically fetch values from database in to an instance of Item
class.
Create mapper interface for Item
class. Then this mapper will be referenced from XML mapping file.
To try out whether MyBatis is working, create Item
database table and insert there a new record. So, we can fetch it later in Vaadin UI.
Here is an example of how to get the mapper and fetch values from database. Get the bean from the context Grails.get(ItemMapper)
and call the method findById(1)
.
When grails run-app
is executed the application is started, a value Sample
is fetched from database and displayed in the web browser.
Create src/groovy/com/app/mappers/ItemMapper.xml
file and define SQL query that will be executed when findById
method is called.
ItemMapper
implementation is provided by .