A bunch of Maven Archetypes for Spring based Projects

Maven is a good project management tool that greatly reduces the amount of time we spend creating Java projects with a proper structure. With so many predefined Maven archetypes, it is even easier to create projects by simply selecting the archetype based on the technologies we need and the type (jar/war/ear) of project we want to create.

However, sometimes those predefined archetypes’ structures may not suit our needs well, or we may need some more additions to the pre-configured dependencies/frameworks, etc.

MyBatis Tutorial : Part4 – Spring Integration

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

MyBatis-Spring is a sub-project of MyBatis that provides Spring integration support, which drastically simplifies MyBatis usage. For those who are familiar with Spring’s Dependency Injection process, using MyBatis-Spring is very simple.

First, let us see the process of using MyBatis without Spring.

  1. Create SqlSessionFactory using SqlSessionFactoryBuilder by passing mybatis-config.xml, which contains DataSource properties, a list of Mapper XMLs, TypeAliases, etc.
  2. Create an SqlSession object from SqlSessionFactory.
  3. Get a Mapper instance from SqlSession and execute queries.
  4. Commit or roll back the transaction using the SqlSession object.

With MyBatis-Spring, most of the above steps can be configured in the Spring ApplicationContext, and SqlSession or Mapper instances can be injected into Spring Beans. Then we can use Spring’s TransactionManagement features without writing transaction commit/rollback code all over the place.

MyBatis Tutorial: Part 3 – Mapping Relationships

In this post, let us see how to use MyBatis ResultMap configuration to map relationships.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

To illustrate, we are considering the following sample domain model:

There will be Users and each User may have a Blog and each Blog can contain zero or more posts.

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

In this post, I will explain how to perform CRUD operations using MyBatis’s annotation support without needing to configure queries in XML mapper files.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

Step#1: Create a table BLOG and a java domain Object Blog.

CREATE TABLE  blog 
(
      blog_id int(10) unsigned NOT NULL auto_increment,
      blog_name varchar(45) NOT NULL,
      created_on datetime NOT NULL,
      PRIMARY KEY  (blog_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
package com.sivalabs.mybatisdemo.domain;

import java.util.Date;

public class Blog 
{
     private Integer blogId;
     private String blogName;
     private Date createdOn;
     
     //Setters and getters
     
     @Override
     public String toString() {
      return "Blog [blogId=" + blogId + ", blogName=" + blogName
        + ", createdOn=" + createdOn + "]";
     }
}

Step#2: Create UserMapper.java interface with SQL queries in Annotations.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis is an SQL Mapper tool which greatly simplifies database programming when compared to using JDBC directly.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

Step1: Create a Maven project and configure MyBatis dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.sivalabs</groupId>
 <artifactId>mybatis-demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>mybatis-demo</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
     <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.10</version>
   <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.1.1</version>
  </dependency>
  <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.21</version>
     <scope>runtime</scope>
  </dependency>
 </dependencies>
</project>

Step#2: Create the table USER and a Java domain Object User as follows:

Keep The Code Clean: WatchDog & SpotTheBug Approach

Before discussing the WatchDog & SpotTheBug Approach, let me give a brief context on the need for this.

Three months back, I was asked to write core infrastructure code for our new application, which uses all the latest and greatest technologies. I have written the infrastructure code and implemented two use cases to demonstrate which logic should go into which layer, and the code looks good (at least to me :-)). Then I moved on to my main project, and I was hearing that the project that I designed (from now onwards, I will refer to this as ProjectA) is going well.

When to use RequestDispatcher.forward() and response.sendRedirect()?

Many people know how RequestDispatcher.forward() and response.sendRedirect() work.

RequestDispatcher.forward() is generally called server-side redirection and is used to forward to a resource within the same application. That resource could be a JSP or another servlet.

response.sendRedirect() is generally called client-side redirection, as it issues a new request from the browser. This method is used to redirect to another resource within the same application, to a resource in some other application running in the same web container, or to any other resource running in some other web container.

Are frameworks making developers dumb?

Last week, I had to conduct interviews to hire senior Java developers with around 5 years of experience. But after the interview process was over, I felt like frameworks make developers’ lives easier but, at the same time, make them dumber.

Everyone puts almost all the new frameworks on their resume, claiming they have “Strong, working experience on Spring, Hibernate, Web Services, etc.”.

Here is how the interviews went.

Me: You have used Spring in your latest project. What are the advantages of using Spring? Interviewee: We can configure beans in XML, and it will take care of instantiating and giving them to us.

10 things to become an outstanding Java developer

If you are a Java developer and passionate about technology, you can follow the tips below, which will make you an outstanding Java developer.

1. Have a strong foundation and understanding of OO Principles

For a Java developer, having a strong understanding of Object-Oriented Programming is a must. Without a strong foundation in OOPS, one can’t realize the beauty of an object-oriented programming language like Java. If you don’t have a good idea of what OOPS is, even though you are using an OOP language, you may still be coding in a procedural way. Just studying the definitions of OO principles won’t help much. You should know how to apply those OO principles when designing a solution in an OO way. So, one should have strong knowledge of object modeling, inheritance, polymorphism, and design patterns.