Showing posts with label MyBatis. Show all posts
Showing posts with label MyBatis. Show all posts

Saturday, February 27, 2016

Set global properties for your MyBatis mappers using Spring-MyBatis

If you need to define some global properties in MyBatis, but using spring-mybatis

In your spring config file:



In application.properties:

fooTableName=FOO_T

In your spring config file: (note sorry about the odd xml declaration, this blogger pre xml doesn't let me define the xml in one line using "/"?)


 
  
 
  
   ${fooTableName}
  
 



Now in your mapper:




Thanks to post at http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-parameter/8422082#8422082

Wednesday, June 10, 2015

https://github.com/rickcr/mybatis-generic-crud

Many times your application consists of doing common CRUD type operations (Create, Read, Update, Delete.)

I'm a big fan of MyBatis and a few solutions have been proposed to handle some of the basic CRUD in a generic fashion. This is just one 'partially generic' way to handle these common things. I say 'partially' since I've seen other approaches, such as having a single mapper and you pass in the table name and a list of columns etc. That approach is decent as well, but I've found that many times you start out generic but you'll often have to tweak a few things such that your CRUD becomes a bit more complicated. Following the approach below you have the flexibility to override whatever you want. The drawback (minor) is that, yes, you still need to create a place-holder mapper interface and you still need to create your mapper xml file and the corresponding CRUD sql in it.

The benefit is that if you follow the convention of naming the mapper methods to match the GenericCrudMapper, you typically are only writing the SQL, and the Java side becomes simple.

Note, I also created a GenericCrudService class. If you follow any of my other tutorials you'll realize that I do prefer to have my Mappers used within a service class. This is just my preference and not required. I've found in the 'real world' that I often need to do more stuff around my basic CRUD operations so that using the Mapper within a Service class gives me an extra layer to shield the client - typically the UI - from having to handle the other needed business logic around the CRUD operations.

Friday, April 10, 2015

MyBatis-Spring-ZK Maven Multimodule Project

I know have a sample project that uses MyBatis, Spring, and ZK, broken out into separate Maven modules. It's a more realistic setup for how you'd typically organize your 'real life' projects, especially in a corporate environment.   Domain module (pojos), Services module (MyBatis), Web project (ZK)

Blogger Syntax Highliter