Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

First steps with MyBatis

1. Introduction

MyBatis (formerly iBatis) is a persistence framework that supports SQL.
This framework simplifies programming versus the use of JDBC. MyBatis can be
configured with XML or annotations.

In this case we are going to see a brief example of the Configuration through
annotations, since in this portal we can find this tutorial
of the MyBatis configuration through XML.

2. Environment

The tutorial is written using the following environment:

  • Hardware: MacBook Pro 15 ‘Laptop (2.2 Ghz Intel Core i7, 8GB DDR3).
  • Operating System: Mac OS High Sierra 10.13.2
  • Development environment: Eclipse Oxygen
  • Apache Maven 3.5.2
  • Spring 5.0.2.RELEASE
  • MyBatis 3.2.4
  • MySql 5.1.34

3. Configuration

First, we will add the necessary dependencies in the pom.xml to
start with our example.

pom.xml

Once the dependencies have been introduced in the pom.xml file, we will continue
with the configuration of MyBatis through annotations.

We create a class, which in this example we will call AppConfig.java. In this
class we will configure both MyBatis, and the connection to the database.
First this class will have the @Configuration annotation, this annotation
indicates that this class declares one or more @Bean methods
and can be processed by the Spring container. The @Bean annotation indicates that
this method produces a bean.

Next, to indicate where is the interface where MyBatis is going to
perform the mapping with the SQL statements, we use the @MapperScan annotation.
In this case instead of Mapper (the most common name in the configuration with XML,
we used the name of Repository).

AppConfig.java

As we can see, in the first method named with the name dataSource ()
the connection with the database is established, in this example it is decided to use
MySql.

In the second method named transactionManager (), it uses the connection obtained
from the DataSource to manage the transaction.

The third method sqlSessionFactory () is responsible for creating the session with MyBatis,
as we are using Spring we use SqlSessionFactoryBean to create the session.

4. Mapping Interface

As we mentioned before, in this interface is where
the SQL statements are going to be mapped thanks to MyBatis, this is where we can appreciate the simplification
of code with respect to JDBC.

4.1. Definition of the data model

First of all we define the Footballer class, which will be the data model or
POJO (Plain Old Java Object) to be entered in the database.

Footballer.java

4.2. Definition of the mapping interface

Now we proceed to the definition of our Mapping Interface, in this example
we have only defined an insert operation and a
search operation in the database.

FootballerRepository.java

As mentioned previously, the word Mapper is more used when
making the configuration with XML, so in our case we have decided to use
the word Repository.

It is striking the notation # {id} for example, when performing the insertion of an
element in the database. With JDBC this parameter would be identified with?
in the PreparedStatement.

But in this case with this notation, MyBatis is instructed to create a
PreparedStatement parameter , so that it is much simpler, since it requires less
code and less effort when instantiating the objects.

Finally, we will comment on the @Repository annotation, with this annotation it is indicated
that the class, in this case the interface, is a component as it is done with @Service, @Controller and the parent of all @Component, indicating to Spring that is a bean to be included in its context to be able to be instantiable in other components.

5. Spring context

To finish with our example we are going to raise the context of Spring, for
that we are going to use the AnnotationConfigApplicationContext class, which will
identify all the annotations mentioned above.

App.java

Once the context of Spring has been raised with the configuration defined in the
AppConfig class , the Mapper is instantiated where the SQL statements are mapped with
MyBatis (FootballerRepository.java) and
the database is inserted and searched to finish with the example.

One aspect that I would like to highlight for future applications, when creating a
controller or service layer, to inject the mapping interface, use
the @Autowired annotation in the definition of the attribute that corresponds to the
mapping interface .

@Autowired

FootballerRepository footballerRepository;

6. Conclusions

As we have seen, the MyBatis framework allows us to perform
persistence operations in a simple way, since it allows us to simplify
our code with respect to JDBC.

The post First steps with MyBatis appeared first on Target Veb.



This post first appeared on Targetveb, please read the originial post: here

Share the post

First steps with MyBatis

×

Subscribe to Targetveb

Get updates delivered right to your inbox!

Thank you for your subscription

×