본문 바로가기
Java

자바 Rest api3

by NaHyungMin 2020. 7. 16.

2020/07/16 - [Java] - 자바 Rest api2

2020/07/06 - [Java] - 자바 Rest api

 

 

import com.superpay.commonlib.util.JsonConvert;
import com.superpay.commonlib.util.Singleton;
import com.package.commonlib.util.response.ResponseMessage;
import com.package.restapi.account.dto.UserDto;
import com.package.restapi.account.service.ILoginService;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

@RestController
@RequestMapping(value = "/login", produces = "application/json; charset=UTF-8")
@Api(value="login-controller", description = "설명되나?")
public class LoginController {
    private final ILoginService loginService;

    @Autowired
    public LoginController(ILoginService loginService) {
        this.loginService = loginService;
    }

    @GetMapping(value = "/test")
    @ApiOperation(value="hello, World API", notes="hello, World를 반환하는 API, Ajax 통신 확인용.")
    public String helloworldString() throws Exception { /*@RequestBody ResultVO result*/
        UserDto userDto = loginService.login(246, null);

        ResponseMessage message = new ResponseMessage();
        message.add(userDto);

        return Singleton.<JsonConvert>Instance(JsonConvert.class).convert(message);
    }

Controller

@Api, @ApiOperation swagger 사용 어노테이션

 

import com.package.commonlib.wrapper.DtoWrapper;
import com.package.config.SqlName;
import com.package.restapi.account.dto.UserDto;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class LoginDao {
    private final SqlSession sqlSession;

    @Autowired
    public LoginDao(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    public UserDto getLoginUser(Map<String, Object> map){
        UserDto user = new UserDto();

        try{
            HashMap<String, Object> hashMap = sqlSession.selectOne(SqlName.getQuery(SqlName.accountSelect, "getLogin"), map);
            DtoWrapper.BindData(hashMap, user);
        }
        catch (Exception ex){
            ex.printStackTrace();
        }

        return user;
    }
}

 

Dao

 

import com.google.gson.annotations.Expose;
import com.package.commonlib.interfaces.IColumn;
import com.package.commonlib.interfaces.IDtoSelector;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Setter
@Getter
public class UserDto implements IDtoSelector {
	/**
     * column : MBR_ID
     * 회원_아이디
     */
    @IColumn(Name = "MBR_ID")
    @Expose
    private int mbrId;

    /**
     * column : EMAIL
     * 이메일
     */
    @IColumn(Name = "EMAIL")
    @Expose
    private String email;
}

Dto

@IColumn 커스텀 어노테이션

@Expose gson에서 사용한 json을 포함할건지 확인하는 어노테이션

 

public interface ILoginService {
    UserDto login(int mbrId, String email) throws Exception;
}

service 인터페이스

 

import com.package.restapi.account.dao.LoginDao;
import com.package.restapi.account.dto.UserDto;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements ILoginService {

    private final LoginDao loginDao;

    public LoginServiceImpl(LoginDao loginDao) {
        this.loginDao = loginDao;
    }

    @Override
    public UserDto login(int mbrId, String email) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("mbrId", mbrId);
        map.put("email", email);

        UserDto user = loginDao.getLoginUser(map);

        return user;
    }
}

 

service 구현

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="database.query.account.select">
    <select id="getLogin" resultType="hashMap" parameterType="hashmap">
        SELECT
            MBR_ID ,EMAIL
        FROM MEMBER
        <trim prefix="WHERE" prefixOverrides="AND |OR ">
            <if test="mbrId != null and mbrId != ''">
                AND MBR_ID = #{mbrId}
            </if>
            <if test="email != null and email != ''">
                AND EMAIL = #{email}
            </if>
        </trim>
     </select>
</mapper>

 

검색 마이바티스

'Java' 카테고리의 다른 글

자바 톰캣 + 아파치 설정  (0) 2020.07.23
자바 스프링 기본 구조  (0) 2020.07.16
자바 Rest api2  (0) 2020.07.16
자바 Rest api  (0) 2020.07.06
자바 싱글턴 테스트  (0) 2020.07.06