2. 스프링부트와 MySQL, mybatis 연동 테스트

Psj·2021년 10월 8일
0

sql

목록 보기
7/10


미리 t_user 테이블에 데이터 두개를 넣어놓았다.

UserRegisteration.java

package com.example.selfmade.userRegisteration;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserRegisteration {

	@Autowired
	UserRegisterationService userRegisterationService;

	@GetMapping("/test")
	@ResponseBody  // @ResponseBody는 리턴되는값을 페이지주소로 사용하는 상황이 아닐때 사용한다
	public List<UserRegisterationModel> getTest() {
		List<UserRegisterationModel> list = userRegisterationService.findUser();

		return list;
	}


}

UserRegisterationService.java

package com.example.selfmade.userRegisteration;

import java.util.List;


public interface UserRegisterationService {

	public List<UserRegisterationModel> findUser();
}

UserRegisterationServiceImp.java

package com.example.selfmade.userRegisteration;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserRegisterationServiceImp implements UserRegisterationService {

	@Autowired
	userRegisterationDAO userRegisterDAO;

	@Override
	public List<UserRegisterationModel> findUser() {

		List<UserRegisterationModel> userRegist = userRegisterDAO.getTestData();
		return userRegist;
	}

}

userRegisterationDAO.java

package com.example.selfmade.userRegisteration;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface userRegisterationDAO {

	List<UserRegisterationModel> getTestData();
}

src/main/resources/mappers/TestMapper.xml

<?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="com.example.selfmade.userRegisteration.userRegisterationDAO">
    <select id="getTestData" resultType="com.example.selfmade.userRegisteration.UserRegisterationModel">
        select
            user_code,
            user_name,
            user_id
        from
            t_user
    </select>
</mapper>

Postman을 이용해서 확인하니 정상적으로 데이터를 반환하는것을 확인할수 있다.

profile
Software Developer

0개의 댓글