Friday, November 2, 2018

Spring Boot and MyBatis

Integrating Spring Boot and Mybatis is quite easy. We will use our previous post as our base project. First, we need to add the following dependencies to our maven pom.xml as shown below. Note that we use PostgreSQL as our database.


We will use the following POJO without any annotation in it as shown below.



Next, create a package where we will put all of our Data Access Object files. Inside the package, create a Data Access Object interface and xml files as shown below.


The ICustomerDao.java contains methods required to interact with Customer data. In this interface we have an insert, an update, and a delete Customer method. Also, we have a method to get count of total Customer. We also have a method to get a Customer by its id. Note that we have the id annotated with @Param("id"). The id inside param refers to id that is used in xml file later. We also have a method to get a limited number of Customer (get Customer start from row offset for next limit number of Customer). Note that we also have them annotated with @Param. The interface itself is annotated with @Mapper, meaning it is a MyBatis mapper class.


Next we will see ICustomerDao.xml. Tag <mapper> defines the interface we created above.


Tag <resultmap> defines what object and its alias we want to return from a query. Here we have a Customer object inside package id.co.rhs.models and we give it an alias Customer. For each property in the class we map it to its database column. And since column ADDRESS_ID has relation one to one with class Address, we use <association> tag. The attribute select defines which statement in IAddressDao to get the address object. Other attributes are self explanatory.


Insert, update, and delete statements are very straightforward. The parameters are put inside #{}. Note that in the interface we pass object Customer and in the xml we just need to get its properties inside curly brackets.

Methods to get Customer are shown below. Here we set the resultMap as Customer even though the query returns more than one resultset. If we want to return a result other than our resultMap, we can use resultType. Here we return resultType integer for getAllCustomersCount method.


What about primary key? We can create a native PostgreSQL function to generate random string as shown below. Then in the customer table we set the primary key default value to be the generated string value.



Next we have to make a database configuration in application.properties as shown below. Here we have to define the driver class name, database url, database username and password. This application.properties file is saved in src/main/resources folder. Note that here we provide plain password. Next we will see how to encrypt this password.



To encrypt the password we have to create a method to encrypt and decrypt it. Below are our methods to do that.


Next we have to create a configuration class as shown below. Here we annotate the class with @Configuration and @EnableAutoConfiguration. @Configuration allows us to define a bean, in our case datasource bean for database connection. @EnableAutoConfiguration makes spring auto configuration enabled. We also have four variables annotated with @Value to get its value from application.properties file. A method named datasource() is annotated with @Bean and return a datasource object. We do this since we want to decrypt our encrypted password in application.properties file.


For testing, we will modify our App.java. Here we add @MapperScan annotation to make our dao package be scanned. We also have a variable ICustomerDao which we annotate with @Autowired. This makes Spring injection happens in that variable. We also make the App class implements Spring Boot's CommandLineRunner and implements its run() method. This method will be executed when Spring application is run. In our case it will get a customer by its id and print it on screen.


Our project structure now looks like this.


Below is the result running our code above.


0 comments:

 

©2009 Stay the Same | by TNB