Package com.sivalabs.springmvcapp.repositories

Source Code of com.sivalabs.springmvcapp.repositories.UserRepository

/**
*
*/
package com.sivalabs.springmvcapp.repositories;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sivalabs.springmvcapp.entities.User;

/**
* @author Siva
*
*/
@Repository
public class UserRepository {

  @Autowired
  private JdbcTemplate jdbcTemplate;
 
  public User findUserById(Integer userId) {
    User user = null;
    user = jdbcTemplate.queryForObject("select * from user where user_id=?", new Object[]{userId}, new RowMapper<User>(){

      @Override
      public User mapRow(ResultSet rs, int rowNum) throws SQLException {
        User user = new User();
        user.setUserId(rs.getInt("user_id"));
        user.setUserName(rs.getString("username"));
        return user;
      }});
    return user;
  }

  public List<User> findAllUsers() {
    return jdbcTemplate.query("select * from user", new RowMapper<User>(){

      @Override
      public User mapRow(ResultSet rs, int rowNum) throws SQLException {
        User user = new User();
        user.setUserId(rs.getInt("user_id"));
        user.setUserName(rs.getString("username"));
        return user;
      }}
    );
  }
}
TOP

Related Classes of com.sivalabs.springmvcapp.repositories.UserRepository

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.