0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

STSでデータベースからJSON形式のデータを取得する方法

0
Posted at

STSでデータベースからJSON形式のデータを取得する方法

テーブル:user_friend
カラム
Email(varchar)
FriendEmail(varchar)

コントローラークラス

TestController.jacva
package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.service.TestService;

@RestController
public class TestController {
	@Autowired
	TestService service;
	
	//GET フレンド情報全件取得
	//"http://localhost:8080/{email}/test"で呼び出されるメソッド
	@RequestMapping(value = "{email}/test", method = RequestMethod.GET)
	List<Map<String,Object>> findGet(@PathVariable String email) {
		//Serviceクラスのfindメソッドを呼び出す
		return service.run(email);
	}
}

サービスクラス

TestService.java
package com.example.service;

import java.util.List;
import java.util.Map;

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

import com.example.repository.TestRepository;

@Service
public class TestService {
	@Autowired
	TestRepository repository;
	
	//RestControllerクラスから呼び出されるメソッド
	public List<Map<String,Object>> run(String email) {
		//Repositoryクラスのfindメソッドを呼び出す
		return repository.run(email);
	}
}

リポジトリクラス

TestRepository.java
package com.example.repository;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

@Repository
public class TestRepository {
	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;
	
	//Serviceクラスから呼び出されるメソッド
	public List<Map<String,Object>> run(String email) {

		List<Map<String,Object>> list = null;
		
		//SQL文
		final String sql = "SELECT * FROM user_friend WHERE Email = :email";
		
		//SQLで送るパラメーターをセット
		SqlParameterSource param = new MapSqlParameterSource().addValue("email", email);
		
		try {
			//SQLを実行して結果を受け取る
			list = jdbcTemplate.queryForList(sql, param);
		} catch(EmptyResultDataAccessException e) {
			e.printStackTrace();
			return null;
		}
		
		//Logを出力
		Log log = LogFactory.getLog(TestRepository.class);
        log.info(list);
		
		return list;
	}
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?