Monthly Archives: May 2013

Mybatis Insert Using XML or Annotation & Return Primary ID

This is a short post on how to use MyBatis to insert an object using XML <insert> or annotation @Insert and return the value of the primary id.

mybatis

The complete code on GitHub is using Spring & H2 embedded database to provide ready to run example.

>mvn exec:java

Objectives:

  • How to use <insert..> & @Insert to insert an object?
  • How to get primary Id of the inserted object?

Continue reading

Spring @Profile

We can enable the environment-aware feature “Profile” in Spring XML configuration, also we can enable this feature using annotation @Profile in Spring @Configuration class “the Spring annotation configuration” which is equivalent to XML configuration <beans…>…..</beans>.

springsource

Objective:

  • How to use @Profile to load Spring configuration for specific environment?

Continue reading

Spring Profile XML Configuration

If you like to load environment specific spring configuration you can do this using profile. In other words, if you want to load a set of beans that have different settings on different environment (development, testing or production) you can use profile feature instead of pointing to a different spring xml configuration at each stage.

springsource

Objective:

  • How to use profile in spring xml configuration?

Continue reading

MyBatis + Spring + Junit (The Missing Part)

We have seen in Mybatis + Spring (XML | Annotation) Mapper how to write a simple mybatis-spring app and what is actually needed to get it up and running. Here I will extend that sample code to cover more stuff that you may face in a real “coding”. We will see how to make a single select that will fetch the one and the many of a “one-to-many” relationship, who to use mybatis config file with spring (if you need it), how to test mybatis-spring code using junit and things you need to watch out!

H2 embedded database has been used to make this example easy to run. But you still can use it with other databases e.g. “MySql, Oracle,….”

mybatis-spring-junit

Objectives:

  • How to use make a single select that will fetch all one to many records?
  • How to reference mybatis config file in spring xml config file?
  • How to test using junit?

Continue reading

Spring MVC + jQuery File Upload (multiple, drag&drop, progress…)

jQuery-File-Upload js library is “so far” the most elegant, smart, simple..etc js library I have used to upload files. Simply it is amazing. It supports async upload “Ajax”, multiple files upload, drag&drop, progress update and a lot more. Here we will see an example of uploading multiple files to Spring MVC app. Bootstrap has been used in this example to make the UI more attractive.

spring-mvc-jquery-file-upload

Objectives:

Continue reading

Create Your Own Maven archetype (Spring MVC)

Maven gives the ability to build a new project from a predefined archetype templates i.e. it generates for us the basic files so we can start coding right away. However, sometimes we need to have our customized version of these templates, fortunately Maven gives us the ability to create those templates which will be used to build our projects.

maven-logo

Objectives:

  • How to create a new Maven archetype?
  • Build Spring MVC archetype.

Continue reading