java

์Šคํ”„๋ง mysql ์—ฐ๊ฒฐํ•˜๊ธฐ

dev_summer 2021. 2. 27. 14:02

 

 

 

1. ์šฐ์„  ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์„œ ํ…Œ์ŠคํŠธ ์‹œ ์ฃผ์„์ฒ˜๋ฆฌํ–ˆ๋˜ pom์˜ mybatis์™€ mysql ์˜ ์ฃผ์„์„ ํ’€์–ด์ค€๋‹ค.

 

 

2. ์„ค์ •ํŒŒ์ผ์„ ์ˆ˜์ •

jsp์‹œ context์— ์ ์–ด์ฃผ์—ˆ๋˜ db์—ฐ๊ฒฐ์‹œ ํ•„์š”ํ–ˆ๋˜ ์ •๋ณด๋“ค์„ ๋™์ผํ•˜๊ฒŒ ์ ์–ด์ค€๋‹ค.

 

p84ํŽ˜์ด์ง€์˜ ๋ ˆ๊ฑฐ์‹œ ๋ชจ๋ธ์˜ root.xml, ์˜†์ชฝ์˜ ์ž๋ฐ”ํŒŒ์ผ์„ ymlํŒŒ์ผ๋กœ์„ค์ •ํ• ์ˆ˜์ž‡๋‹ค

๋‚ด๊ฐ€ ymlํŒŒ์ผ๋กœ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์Šคํ”„๋ง์ด xml,์ž๋ฐ”ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ์„ค์ •ํ•ด์ค€๋‹ค.

 

3. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์—ฐ๊ฒฐ ์„ค์ •ํ•˜๊ธฐ

 

p89

 

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = "com.cos.crud.repository")
public class DataAccessConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {

        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(
        		new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfig {

	@ConfigurationProperties(prefix="spring.datasource")
	public DataSource dataSource(){
    	return DataSourceBuilder.create().build();
	}
}

 

 

 

 

์ปจํŠธ๋กค+ํด๋ฆญํ•˜๋ฉด ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์•Œ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

๋Œ€ํ‘œ์‚ฌ์ง„ ์‚ญ์ œ

๊ทธ๋Œ€๋กœ ๋ณต๋ถ™!

 

 

 

 

*์Šคํ”„๋ง ์ปจํ…์ŠคํŠธ ์•ˆ์— ioc๊ฐœ๋…์„ ์ด์šฉํ•ด์„œ ์Šคํ”„๋ง์ด ์ปดํฌ๋„ŒํŠธ์Šค์บ”(๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์Šค์บ”) @__ ์–ด๋…ธํ…Œ์ด์…˜๋“ค์„ ์ฐพ๋Š”๋‹ค.

๋ณต๋ถ™ํ•œ๊ฒƒ๋“ค์ด @configuration ์ด ๋ฉ”๋ชจ๋ฆฌ์— ๋œฌ๋‹ค.

 

 

์ด์ œ ๋งŒ๋“  ํด๋”์•ˆ์— ์„ค์ • ํ…์ŠคํŠธ๋งŒ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

4. my sql database๋งŒ๋“ค๊ธฐ

 

root ์ ‘์†ํ•ด์„œ ๋งŒ๋“ค๊ธฐ

create user 'spring'@'%' identified by 'bitc5600';

create database spring;

grant all privileges on spring.* to 'spring'@'%';

 

ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

CREATE TABLE mem(
id int AUTO_INCREMENT PRIMARY KEY, 
username varchar(100) not null,
password varchar(100) not null,
email varchar(100),
createDate timestamp

)ENGINE=InnoDB DEFAULT CHARSET = utf8;

 

 

5. ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ

 

 

 

๋ชจ๋ธ์„ ๋งŒ๋“ค๊ฑด๋ฐ

domain: ๊ธฐ๋Šฅ์ด๋ผ๋Š” ์˜๋ฏธ

damainํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— model, dto๋“ฑ์˜ ๊ธฐ๋Šฅ๋“ค์„ ๋ชจ์•„๋†“์„์ˆ˜๋„ ์žˆ๋‹ค.

model=Value Object, VO๋ผ๋Š” ํŒจํ‚ค์ง€๋ผ๊ณ ๋„ ๋งŒ๋“ ๋‹ค.

VO๋Š” ๋ถˆ๋ณ€, ๋ณ€ํ•˜์ง€์•Š๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค. finaly , ์ˆ˜์ •๋˜์ง€์•Š๋Š” ๋ฐ์ดํ„ฐ๋งŒ์„ VO๋ผ ๋ถ€๋ฅธ๋‹ค.

setter์„ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค. getter์™€ ์ƒ์„ฑ์ž๋งŒ ๋งŒ๋“ ๋‹ค.

๋ณ€๊ฒฝํ• ์ˆ˜์ž‡๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด dto๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด model์„ ์ฝ์–ด dto์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

ํ˜‘์—…์„ ์œ„ํ•œ ์•ฝ์†!

 

 

 

 

 

 

6. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์‚ฌ์šฉ์„ ์œ„ํ•œ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

 

https://mybatis.org/mybatis-3/sqlmap-xml.html

 

mybatis – MyBatis 3 | Mapper XML Files

Mapper XML Files The true power of MyBatis is in the Mapped Statements. This is where the magic happens. For all of their power, the Mapper XML files are relatively simple. Certainly if you were to compare them to the equivalent JDBC code, you would immedi

mybatis.org

 

<?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.cos.springboot.repository.MemRepository">

  <select id="findAll" resultType="com.cos.sprringboot.model.Mem">
    select * from mem order by id desc
  </select>
  
  
  <select id="findById" resultType="com.cos.sprringboot.model.Mem">
    select * from mem where id=#{id} ์ •ํ™•ํ•˜๊ฒŒ ์ ์–ด์•ผ ํ•œ๋‹ค.
  </select>
 
  <insert id="save"> // ๊ฒŒํ„ฐ๊ฐ€ ํ˜ธ์ถœ ๋œ๋‹ค. 
  	insert into mem(username, password, email, createDate) values(#{username},#{password},#{email},now())
  </insert>
  
   <insert id="update"> 
  	update mem set password = #{password}, email = #{email} where id = #{id}
  </insert>
  
  <insert id="delete"> 
  	delete from mem where id= #{id}
  </insert>
  
</mapper>

 

 

 

* ๊ฐ„๋‹จํ•œ SQL๋ฌธ ์•„๋ž˜๋กœ๋„ ๊ฑธ ์ˆ˜ ์žˆ๋‹ค.

 

*์กฐ์ธํ•ด์„œ ๋ฐ›์„๋•Œ ๋งต ์“ฐ์ง€๋ง๊ณ  ์กฐ์ธํ•œ DTO์‚ฌ์šฉ ํ•˜์—ฌ ๋ฐ›๋Š” ๊ฒƒ์ด ์ข‹๋‹ค! ๊ทธ๋ ‡๊ฒŒ ๋ฐ›์Ÿˆ

 

package com.cos.springboot.repository;
import java.util.List;
import com.cos.springboot.model.Mem;

public interface MemRepository {
    @Select("SELECT * FROM mem")
	List<Mem> findAll();
}

 

 

7. mem์ž‘๋™์‹œํ‚ค๊ธฐ

 

package com.cos.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cos.springboot.dto.RequestMemJoinDto;
import com.cos.springboot.repository.MemRepository;

@Controller
public class MemController {
	
	@Autowired // ์ฃผ์ž…ํ•ด์ค€๋‹ค DI์˜ ๊ฐœ๋…, ํ˜„์žฌ @repository์˜์—ญ์— MemRepository ์ด ๋„์–ด์ ธ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•˜๋‹ค.
	private MemRepository memRepository;
	
	@PostMapping("/mem/api/join")
	// ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋Š” username, password, email , ํ•œ๊ฑด์€ getparam์œผ๋กœ ๋ฐ›์œผ๋ฉด ๋˜์ง€๋งŒ ์—ฌ๋Ÿฌ๊ฑด์ผ๋•Œ์—๋Š” DTO๋กœ ๋ฐ›๋Š”๋‹ค.
	// json์œผ๋กœ ๋˜์ง€๊ณ  json์œผ๋กœ ๋ฐ›๋Š”๋‹ค. requestbody, ๊ทธ๋Ÿฌ๊ณ  ํ˜•ํƒœ๋ฅผ ๋ณ€ํ˜•์‹œํ‚ค๋ฉด ๋œ๋‹ค.
	// DML์€ ์ธ์„œํŠธ๋งŒ ํ•˜๊ธฐ๋–„๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ’๋งŒ ๋ฐ›๊ธฐ ๋–„๋ฌธ์— ์ธ์„œํŠธ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ํŽ˜์ด์ง€์ด๋™๋งŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
	// api๊ฐ€ ๋ถ™์„๋•Œ์—๋Š” data๋งŒ ๋ฆฌํ„ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ํŽ˜์ด์ง€๋ฆฌํ„ด์ด ์•„๋‹Œ ์ธ์„œํŠธ๊ฐ€ ์ž˜๋˜์—ˆ๋‹ค๋Š” ๊ฒฐ๊ณผ๊ฐ’๋งŒ ๋ฆฌํ„ด
	public @ResponseBody int memApiJoin(@RequestBody RequestMemJoinDto requestMemJoinDto) {
		// memํ˜•ํƒœ๋กœ ๋ฐ›์„์ˆ˜๋„์žˆ๊ฒ ์ง€๋งŒ, DTO๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ›๋Š”๋‹ค. mem์€ ๋””๋น„์šฉ
		// ๋ฆฌํ€˜์ŠคํŠธ๋ฐ”๋””,๋ฒ„ํผ๋กœ ๋ฐ›๋Š”๋‹ค. ์›๋ž˜ ์Šคํ”„๋ง์€ ๋ชป๋ฐ›์ง€๋งŒ ์žญ์Šจ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ›์•„์ค€๋‹ค.
		
		int result = memRepository.save(requestMemJoinDto);
		
		if(result ==1) {
			return 200; //์›๋ž˜๋Š” ๋ทฐ๋ฆฌ์กธ๋ธŒ๊ฐ€ ๊ด€์—ฌํ•˜๊ธฐ๋•Œ๋ฌธ์— @@ResponseBody ๋กœ ๋ฐ์ดํ„ฐ๋กœ ์ธ์‹ํ•˜๊ฒŒ ํ•ด์•ผํ•จ.
		}else {
			return 500;
		}
	}
}

 

ํ˜„์žฌ์˜ ์—ํ”Œ๋ฆฌ์ผ€์ด์…˜์ปจํ…์ŠคํŠธ ์ƒํ™ฉ

@controller

HomeController

MemControler

@configulation

DataAccessConfig

DataSourceConfig

@repository (๋ ˆํŒŒ์ง€ํ† ๋ฆฌ ์˜์—ญ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋„์šฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ, ์˜ˆ์ „์—” ์‚ฌ์šฉํ•˜์˜€์œผ๋‚˜ ํ˜„์žฌ๋Š” dataacess๊ฐ€ ๋„์–ด์ฃผ๊ณ ์žˆ์Œ)

MemRepository

 

 

 

*intํ˜•์ด ์•„๋‹Œ String ํ˜•์œผ๋กœ ๋ฆฌํ„ด ๋ฐ›๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

 

 

 

 

 

 

์ถ”ํ›„ @valid ๋ฅผ ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

(1). ํšŒ์›๊ฐ€์ž…

 

- MemController

public @ResponseBody ResponseEntity<String> memApiJoin(@RequestBody RequestMemJoinDto requestMemJoinDto) {
		// memํ˜•ํƒœ๋กœ ๋ฐ›์„์ˆ˜๋„์žˆ๊ฒ ์ง€๋งŒ, DTO๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ›๋Š”๋‹ค. mem์€ ๋””๋น„์šฉ
		// ๋ฆฌํ€˜์ŠคํŠธ๋ฐ”๋””,๋ฒ„ํผ๋กœ ๋ฐ›๋Š”๋‹ค. ์›๋ž˜ ์Šคํ”„๋ง์€ ๋ชป๋ฐ›์ง€๋งŒ ์žญ์Šจ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ›์•„์ค€๋‹ค.

		int result = memRepository.save(requestMemJoinDto);

		if (result == 1) {
			return new ResponseEntity<String>("ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต", HttpStatus.CREATED); // ์›๋ž˜๋Š” ๋ทฐ๋ฆฌ์กธ๋ธŒ๊ฐ€ ๊ด€์—ฌํ•˜๊ธฐ๋•Œ๋ฌธ์— @ResponseBody ๋กœ ๋ฐ์ดํ„ฐ๋กœ
																				// ์ธ์‹ํ•˜๊ฒŒ ํ•ด์•ผํ•จ.
		} else {
			return new ResponseEntity<String>("ํšŒ์›๊ฐ€์ž… ์‹คํŒจ.", HttpStatus.BAD_REQUEST);
		}
	}

 

 

 

 

 

(2). ํšŒ์› ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€

- list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํšŒ์› ๋ฆฌ์ŠคํŠธ</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>username</th>
			<th>email</th>
			<th>createdate</th>
			<th>update</th>
		</tr>
		<c:forEach var="mem" items="${mems}">
			<tr>
				<th>${mem.username}</th>
				<th>${mem.email}</th>
				<th>${mem.createDate}</th>
				<td><button onclick="mem_update(${mem.id})">edit</button></td>
			</tr>


		</c:forEach>
	</table>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
	function mem_update(mem_id){
		location.href='/mem/'+mem_id;
	}
</script>
</body>
</html>

- MemController

	@GetMapping("/mem")
	public String mems(Model model) {
		List<Mem> mems = memRepository.findAll();

		model.addAttribute("mems", mems);

		return "mem/list";
	}

 

 

 

 

(3). ํšŒ์› ์ˆ˜์ • ํŽ˜์ด์ง€

 

- update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํšŒ์› ์ˆ˜์ • ํŽ˜์ด์ง€</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>id</th>
			<th>username</th>
			<th>password</th>
			<th>email</th>
			<th>createdate</th>

		</tr>
		<tr>
			<th><input id="id" type="text" value="${mem.id}"
				readonly="readonly"></th>
			<th><input type="text" value="${mem.username}"
				readonly="readonly"></th>
			<th><input id="password" type="password" value="${mem.password}"></th>
			<th><input id="email" type="text" value="${mem.email}"></th>
			<th><input type="text" value="${mem.createDate}"
				readonly="readonly"></th>
		</tr>

	</table>
	<button id="mem_update_proc">์ˆ˜์ •ํ•˜๊ธฐ</button>
	<script
		src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
	<script>
		$('#mem_update_proc').on('click', function() {
			let data={
					id: $('#id').val(),
					password: $('#password').val(),
					email: $('#email').val()
					};
		$.ajax({
			type: 'PUT',
			url : '/mem/api/update',
			data : JSON.stringify(data),
			contentType : 'application/json; charset=utf-8',
			dataType :'json'
			}).done(function(result){
				if(result==='ok'){
				alert('ํšŒ์›์ •๋ณด๊ฐ€ ์ˆ˜์ • ์™„๋ฃŒ');
				location.href='/mem';
				}else {
					alset('ํšŒ์›์ •๋ณด ์ˆ˜์ • ์‹คํŒจ')}
				}).fail(function(result){
				alert('์„œ๋ฒ„์˜ค๋ฅ˜');
				});
			});
	</script>
</body>
</html>

 

 

- MemController

	@PutMapping("/mem/api/update")
	public @ResponseBody ResponseEntity<?> updateProc(@RequestBody RequestMemUpdateDto requestMemUpdateDto) {
		int result = memRepository.update(requestMemUpdateDto);
		if(result==1) {
			return new ResponseEntity<RequestMemUpdateDto>(requestMemUpdateDto, HttpStatus.OK);	
		}else {
			return new ResponseEntity<RequestMemUpdateDto>(requestMemUpdateDto, HttpStatus.BAD_REQUEST);	
		}
	}

 

 

 

(4). ํšŒ์› ์‚ญ์ œ

- MemController

 

 

 

 

 

 

 

 

 

 

 

 

 

 

* ์ •๋ฆฌ

 

1. mysql์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” pom.xml์˜ mysql๊ณผ mybatis์„ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค.

2. configํŒŒ์ผ์„ ๋‘˜๋‹ค ๋ณต์‚ฌ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ํ•ด์•ผํ•œ๋‹ค. IOCํ•˜๊ธฐ์œ„ํ•จ,DataSourceConfig ์€ ์ˆ˜์ •ํ•  ํ•„์š” ์—†๋‹ค. DataAccessConfig์€ ์Šค์บ”ํ•  ํŽ˜์ด์ง€๋งŒ ํ™•์ธํ•˜์—ฌ ์ˆ˜์ •ํ•œ๋‹ค.

3. ymlํŒŒ์ผ์—์„œ DB์—ฐ๊ฒฐ์„ ์œ„ํ•œ datasource๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

4. model์„ ๋งŒ๋“ ๋‹ค. setter์€ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค. setterํ•  ์ผ์ด ์—†๋‹ค.

5. MemRepository๋ฅผ ๋งŒ๋“ ๋‹ค. ๋งคํผ๊ฐ€ ์Šค์บ”๋˜๋Š” ์œ„์น˜์— ์žˆ์–ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์— DataAccessConfig์˜ ์Šค์บ” ์œ„์น˜์— ์ •ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.

6. mem.xmlํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค. namespace๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ ๋ฐ”๋ผ๋ณผ์ˆ˜์žˆ๊ฒŒ ํ™•์ธํ•œ๋‹ค.

 

๋ฆฌ์ŠคํŠธ๋Š” ๊ฒŸ์ด๋ผ์„œ ajax ์•ˆ์ป๋‹ค.

์ธ์„œํŠธ, ์—…๋ฐ์ดํŠธ๋Š” ํฌ์ŠคํŠธ๋ฐฉ์‹์ด๋ผ์„œ ajax๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.