Skip to main content

Spring 4 and mybatis 3 for data tier

Personally I think Hibernate has its complications and doesn't fit an enterprise SOA applications, It will need a lot of customization that will take the essence of why Hibernate is for.

So I prefer to use another framework for SQL databases and it's Mybatis.

Mybatis in short is an SQL mapping framework that will handle the conversion from SQL to Java and the other way around.

it relies on the concept of SQL maps, which are "originally" xml files that define the mapping between our DAO methods and the queries we execute and describe the results and how they are mapped.

example on a simple map :
https://gist.github.com/c0a1cc76b8c8646e05c6.git


now let's see how to use this query from our java code.

first we have to place our xml file in the classpath, in my case I use gradle as build system and the src/main/resources folder is the best choice for me.

now in some package I should have an interface that will call this map:

[https://gist.github.com/af0ceb439fe560f697b0.git]

note that the name of the interface is the same as the xml map and the name of the id of the select query is the same as the method name.

now what remained is to tell our spring configuration about these files :

[https://gist.github.com/636718cca702f7e1297f.git]

now we can inject the mapper in our dao and use it directly :

[https://gist.github.com/c6070206271c23af0a1c.git]


Now that you know how to use Mybatis and how to write the sql queries there are few things to know:
1- There is a project called Mybatis generator, it reads the database and automatically generate for you the basic CRUD queries and it will cover 70-80% of what you need, so you don't have to manually write every standard query every time.
check for the project here: http://mybatis.github.io/generator/
2- it's easy to customize and optimize the query in Mybatis unlike hibernate and what's good about it, is that it does one thing and it does it right.

3- you can avoid using xml files at all by writing your queries as annotations over the mapper interface methods, example :

package org.mybatis.example;
public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}

this is a quick way but it has its limitations and mess.

References:

1-http://mybatis.github.io/mybatis-3/getting-started.html
2-http://mybatis.github.io/generator/

Comments

  1. Nice article. Very useful. One thing I would to add, that instead of creating bean of mapper, we can use @MapperScan in java configuration class. Find the link

    ReplyDelete

Post a Comment

Popular posts from this blog

Android RecyclerView - Adding Empty View

So RecyclerView was introduced to replace List view and it's optimized to reuse existing views and so it's faster and more efficient as stated in the documentation:

https://developer.android.com/training/material/lists-cards.html

While using it, I faced the issue of missing a useful functionality that is implemented in ListView.
that feature is setting an empty view in case there was no records.

In ListView it was as simple as this

View emptyView = findViewById(R.id.mylist_empty_view);
ListView  myList = ....
myList.setEmptyView(emptyView);

but this method doesn't exist for recycler view so we need a work around until android team fixes this.


and here are the screen shots of both list view and recycler view fix

List view :

Recycler view :



here is how I fixed it:



here is the content of empty_view, it can be anything.



Enjoy.

Creating your own OAuth2 server and clients using spring security - part 1

In this series of posts, I'll try to put together a simple working example on how to create your own OAuth2 server.

if you want to know more on OAuth2 and when to use it as authentication and authorization protocol then you can search about it on google and i'll put some URLs later.

Now I assume you are familiar with java web applications using Spring and maven.

to get started we need to create the server side with all dependencies required and i'll list them here, i'll use maven 2 to ease downloading dependencies for us.


Steps:

1- Create new maven project with arch type webapp:



2- Add the required depenedencies for spring, spring security, spring-oauth2, hibernate & other libraries (required for this tutorial only you can use other libraries if you like)

https://gist.github.com/anonymous/d33a31ddc3ba84375cf3

3- I used hibernate to automate the creation of the schema required by spring OAuth2 to manage tokens (it's required to have schema created in db if you a…

Android - Multiple themes for one application

Sometimes you want to have multiple themes for your app
one strong example is having the ability to switch between dark and light themes because during night, a white bright screen can really be annoying for users eyes

Android will do most of the work for you but it may be required to change icons between themes to fit colors
In this blog I'll show a simple app with both dark and light themes and how to change icons without having to do that from code and keep things clean and centralized.
first of all let's create our activity, it will look something like this :


In /rest/values/styles.xml, we inherit Theme.AppCompat
 <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat">
        <!--
            Theme customizations available in newer API levels can go in
            res/values…