Spring With MyBatis ORM


Welcome to mybatis tutorials with spring.I am also new in mybatis.
Mybatis is unlike to ORM technology.Here I introduce it with a example
At first we take the Spring configuration for our demo project.So we first write a deployment descriptor web.xml file


 Spring MVC Application

    
  mvc-dispatcher
  org.springframework.web.servlet.DispatcherServlet
        1
 

 
  mvc-dispatcher
  /
 

Its just like usual Spring DD configuration.Now we need to define a dispatcher servlet where I define different configuration for mybatis with spring.
 
 


    

    

    

    
        
        
    


    
        
        
        
        
    

    
    
        
    


    
    
        
        
        
    


    
    
        
    

We use annotation based spring configuration as usual my spring step by step tutorials.Different is here we define mapper persistance directory.Here we need to mapping objects element with db column name so we need a mapped xml file ***************************


    
        
        
        
        
        
        
    

    


    

    
        INSERT INTO user (username,password,standard,age,sex)
        VALUE (#{username},#{password},#{standard},#{age},#{sex})
    

    
        UPDATE user
        SET
        username = #{username}
        password = #{password},
        standard = #{standard},
        age = #{age},
        sex = #{sex}
        where id = #{id}
    

    
        DELETE FROM user
        WHERE id = #{id}
    


Here we can see that Userservice related with User class.So we need to declare it. Now we need a user class
package com.springapp.domain;

/**
 * Created with IntelliJ IDEA.
 * User: DELL
 * Date: 6/18/14
 * Time: 9:58 AM
 * To change this template use File | Settings | File Templates.
 */

import org.springframework.stereotype.Component;

import java.io.Serializable;

@Component("user")
public class User implements Serializable {

    private static final long serialVersionUID = 3647233284813657927L;

    private String id;
    private String username;
    private String password = null;
    private String standard = null;
    private String age;
    private String sex = null;

    @Override
    public String toString() {
        return "User [name=" + getPassword() + ", standard=" + getStandard() + ", age=" + getAge()
                + ", sex=" + getSex() + "]";
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStandard() {
        return standard;
    }

    public void setStandard(String standard) {
        this.standard = standard;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
Now we need a UserService interface
package com.springapp.persistence;

/**
 * Created with IntelliJ IDEA.
 * User: DELL
 * Date: 6/18/14
 * Time: 10:05 AM
 * To change this template use File | Settings | File Templates.
 */

import com.springapp.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserService {

    public void saveUser(User user);
    public void updateUser(User user);
    public void deleteUser(int id);
    public List getAllUser();
    public User getUserByUsername(String username);
}


Now we need a implementation class of this interface
package com.springapp.service;

import com.springapp.domain.User;
import com.springapp.persistence.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: DELL
 * Date: 6/23/14
 * Time: 11:32 AM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserServiceImpl implements UserService {
    @Override
    public void saveUser(User user) {
        saveUser(user);
    }

    @Override
    public void updateUser(User user) {
        updateUser(user);
    }

    @Override
    public void deleteUser(int id) {
        deleteUser(id);
    }

    @Override
    public List getAllUser() {
        return getAllUser();
    }

    @Override
    public User getUserByUsername(String username) {
        return getUserByUsername(username);
    }


}

Now we need to define view pages that are in my git project listed bellow. You can download project from SpringWithMybatis Thanks.I'll Provide advance level coding with mybatis and spring next tutorials. Happy coding...:P
Reactions

Post a Comment

0 Comments