Christmas Pikachu MyBatis 사용법 [gradle 사용]
개발일지/스프링

MyBatis 사용법 [gradle 사용]

ZI_CO 2024. 3. 27.

 

 

1. 의존성 때문에 build.gradle에 추가

plugins {
	id 'java'
	id 'war'
	id 'org.springframework.boot' version '3.2.4'
	id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.koreait'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3'
	implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
	implementation 'jakarta.servlet:jakarta.servlet-api'
	implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api'
	implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl'
	runtimeOnly 'com.mysql:mysql-connector-j'
}

tasks.named('test') {
	useJUnitPlatform()
}
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3'

 

 

 

 

 

 

 

2. 경로를 설정해준다.

 

 

 

package com.koreait.day080.biz.member;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class MemberDAO {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static final String SELECTALL="SELECT MID,NAME FROM MEMBER";
	private static final String SELECTONE="SELECT MID,NAME FROM MEMBER WHERE MID=? AND PASSWORD=?";

	private static final String INSERT="INSERT INTO MEMBER (MID,PASSWORD,NAME,ROLE) VALUES(?,?,?,?)";
	private static final String UPDATE="";
	private static final String DELETE="";

	public List<MemberDTO> selectAll(MemberDTO mDTO) {
		return (List<MemberDTO>)jdbcTemplate.query(SELECTALL, new MemberRowMapper02());
	}
	public MemberDTO selectOne(MemberDTO mDTO) {
		Object[] args= { mDTO.getMid(), mDTO.getPassword() };
		try {
			return jdbcTemplate.queryForObject(SELECTONE, args, new MemberRowMapper02());
		} catch(Exception e) {
			return null;
		}
	}

	public boolean insert(MemberDTO mDTO) {
		int result=jdbcTemplate.update(INSERT, mDTO.getMid(), mDTO.getPassword(), mDTO.getName(), mDTO.getRole());
		if(result<=0) {
			return false;
		}
		return true;
	}
	public boolean update(MemberDTO mDTO) {
		int result=jdbcTemplate.update(UPDATE, mDTO.getMid(), mDTO.getPassword(), mDTO.getName(), mDTO.getRole());
		if(result<=0) {
			return false;
		}
		return true;
	}
	public boolean delete(MemberDTO mDTO) {
		int result=jdbcTemplate.update(DELETE, mDTO.getMid(), mDTO.getPassword(), mDTO.getName(), mDTO.getRole());
		if(result<=0) {
			return false;
		}
		return true;
	}
}

class MemberRowMapper implements RowMapper<MemberDTO> {

	@Override
	public MemberDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
		MemberDTO data=new MemberDTO();
		data.setMid(rs.getString("MID"));
		data.setPassword(rs.getString("PASSWORD"));
		data.setName(rs.getString("NAME"));
		data.setRole(rs.getString("ROLE"));
		return data;
	}

}
class MemberRowMapper02 implements RowMapper<MemberDTO> {

	@Override
	public MemberDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
		MemberDTO data=new MemberDTO();
		data.setMid(rs.getString("MID"));
		data.setName(rs.getString("NAME"));
		return data;
	}

}

 

 

 

 

 

 

 

 

package com.koreait.day080.biz.member;

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

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface IMemberDAO {

	public List<MemberDTO> selectAll();
	public MemberDTO selectOne(Map<String,String> map); // 낮은 결합도를 유지하게해줌

}

 

 

 

 

 

 

<?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.koreait.day080.biz.member.IMemberDAO">
	<select id="selectAll" resultType="com.koreait.day080.biz.member.MemberDTO">
		SELECT MID,NAME FROM MEMBER
	</select>
	<select id="selectOne" resultType="com.koreait.day080.biz.member.MemberDTO">
		SELECT MID,NAME FROM MEMBER WHERE MID=#{mid} AND PASSWORD=#{password}
	</select>
</mapper>

 

 

 

 

 

package com.koreait.day080.biz.member;

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

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

@Service
public class MemberServiceImpl implements MemberService {

	@Autowired
	private IMemberDAO mDAO;

	@Override
	public List<MemberDTO> selectAll(MemberDTO mDTO) {
		//return mDAO.selectAll(mDTO);
		return mDAO.selectAll();
	}

	@Override
	public MemberDTO selectOne(MemberDTO mDTO) {
		//return mDAO.selectOne(mDTO);
		Map<String,String> map=new HashMap<String,String>();
		map.put("mid", mDTO.getMid());
		map.put("password", mDTO.getPassword());
		return mDAO.selectOne(map);
	}

	@Override
	public boolean insert(MemberDTO mDTO) {
		//mDAO.insert(mDTO); // 성공
		//return mDAO.insert(mDTO); // 실패
		return false;
	}

	@Override
	public boolean update(MemberDTO mDTO) {
		//return mDAO.update(mDTO);
		return false;
	}

	@Override
	public boolean delete(MemberDTO mDTO) {
		//return mDAO.delete(mDTO);
		return false;
	}

}

댓글