본문 바로가기

Programming/Spring

Spring boot - DB / Mybatis / Mapper

반응형

 

 

1. Controller: UserProfileController.java

package com.example.myba.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.myba.dto.ItemDto;
import com.example.myba.dto.ResponseDto;
import com.example.myba.service.QuickService;

import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class UserProfileController {
	
	@Autowired
	private QuickService quickService; //자동으로 값을 넣은 객체를 생성해줌. DI
	
	@GetMapping("/dummy")
	public String dummy() {
//		log.info("dummy");
		return "{}";
	}
	
	@GetMapping("/dummy2")
	public String dummy2() {
		return "dummy2";
	}
	
	@GetMapping("/member")
	public String getMember(@RequestParam("empNo") String empNo) {
		return "OK";
	}

	@GetMapping("/company/{id}")
	public String getCompany(@PathVariable("id") String id) {
		return "OK";	
	}
	
	@PostMapping("/item")
	public ResponseDto registerItem(@RequestBody ItemDto item) {
		
		boolean b = quickService.registerItem(item);
		
		if(b==true) {
			ResponseDto responseDto = new ResponseDto();
			responseDto.setMessage("ok");
			
			return responseDto;
		}
		
		ResponseDto responseDto = new ResponseDto();
		responseDto.setMessage("fail");
		
		return responseDto;
	}
	
	@PostMapping("/user")
	public String registerUser(@RequestBody String user) {		
		return "OK";
	}
	
	@GetMapping("/item")
	public ItemDto getItem(@RequestParam("id") String id) {
		ItemDto res = quickService.getItemById(id);
		return res;
	}
	
}

 

2. dto

- ItemDto.java

package com.example.myba.dto;

public class ItemDto {
	
	private String id;
	
	private String name;

	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}

 

 

- ResponseDto.java

package com.example.myba.dto;

public class ResponseDto {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}

 

3. mapper: QuickMapper.java

package com.example.myba.mapper;

import java.util.HashMap;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface QuickMapper {
	HashMap<String, Object> findById(HashMap<String, Object> paramMap);
}

 

 

 

4. mapper.xml: QuickMapper.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.myba.mapper.QuickMapper">

	<select id="findById" parameterType="hashmap" resultType="hashmap">
		SELECT id, name FROM item WHERE id = #{id}
	</select>
	
</mapper>

 

 

5. service: QuickService.java

package com.example.myba.service;

import java.util.HashMap;

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

import com.example.myba.dto.ItemDto;
import com.example.myba.mapper.QuickMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class QuickService {
	
	@Autowired QuickMapper quickMapper;
	
	public boolean registerItem(ItemDto itemDto) {
		// TODO:
		
		return true;
	}
	
	public ItemDto getItemById(String id) {
		
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("id", id);
		
		HashMap<String, Object> res = quickMapper.findById(paramMap);
		
		
		ItemDto itemDto = new ItemDto();
		itemDto.setId((String)res.get("ID"));;
		itemDto.setName((String)res.get("NAME"));
		
		return itemDto;
	}
}

 

6. application.properties

spring.application.name=myba

#spring.datasource.url=jdbc:mysql://localhost:3306/personal_info
#spring.datasource.username=asdf
#spring.datasource.password=1234
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:~/test
spring.datasource.username=asdf
spring.datasource.password=1234

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@12.34.56.78:1526:abcde
spring.datasource.username=asdf
spring.datasource.password=1234


mybatis.type-aliases-package=com.example.myba.mapper 
# Mybatis mapper 위치 설정
mybatis.mapper-locations= mybatis-mapper/*.xml

 

 

7. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>myba</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>myba</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>3.0.3</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter-test</artifactId>
			<version>3.0.3</version>
			<scope>test</scope>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
		<dependency>
		    <groupId>org.springdoc</groupId>
		    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
		    <version>2.1.0</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
		    <groupId>org.projectlombok</groupId>
		    <artifactId>lombok</artifactId>
		    <version>1.18.30</version>
		    <scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

 

 

 

반응형