TIL/MyBatis

[MyBatis] crud-javaconfig

yndev 2022. 3. 15. 20:59

앞서 해준 crud는 xmlconfig를 이용한 것이었다.

javaconfig를 이용해서 crud를 똑같이 해준다

 

MenuDAO, mybatis-config.xml이 없어지고 menu-mapper.xml 대신 MenuMapper.java파일이 대체된다.

 

Application, MenuController, PrintResult 코드 동일

 

Template

- mybatis-config.xml에서 해준 설정을 Template에 해준다.

package com.greedy.section02.javaconfig;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

public class Template {

	private static String DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static String URL = "jdbc:log4jdbc:oracle:thin:@localhost:1521:xe";
	private static String USER = "C##REVIEW";
	private static String PASSWORD = "REVIEW";
	
	private static SqlSessionFactory sqlSessionFactory;
	
	public static SqlSession getSqlSession() {
		
		if(sqlSessionFactory == null) {
			Environment environment = 
					new Environment("dev"
							  , new JdbcTransactionFactory()
							  , new PooledDataSource(DRIVER, URL, USER, PASSWORD));
			Configuration configuration = new Configuration(environment);
			
			configuration.addMapper(MenuMapper.class);
			
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
			}
		
		return sqlSessionFactory.openSession(false);
	}
	
}

 

MenuService

-MenuDAO대신 MenuMapper 선언해서 메소드 생성함

package com.greedy.section02.javaconfig;

import static com.greedy.section02.javaconfig.Template.getSqlSession;

import java.util.List;

import org.apache.ibatis.session.SqlSession;


public class MenuService {

	private MenuMapper menuMapper;
	
	public List<MenuDTO> selectAllMenu() {
		
		SqlSession sqlSession = getSqlSession();
		
		menuMapper = sqlSession.getMapper(MenuMapper.class);
		List<MenuDTO> menuList = menuMapper.selectAllMenu();
		
		sqlSession.close();
		
		return menuList;
	}

	public MenuDTO selectMenuByCode(int code) {
		
		SqlSession sqlSession = getSqlSession();
		
		menuMapper = sqlSession.getMapper(MenuMapper.class);
		MenuDTO menu = menuMapper.selectMenuByCode(code);
		
		sqlSession.close();
		
		return menu;
	}

	public boolean registMenu(MenuDTO menu) {
		
		SqlSession sqlSession = getSqlSession();
		
		menuMapper = sqlSession.getMapper(MenuMapper.class);
		int result = menuMapper.insertMenu(menu);
		
		if(result > 0) {
			sqlSession.commit();
		} else {
			sqlSession.rollback();
		}
		
		sqlSession.close();

		return result > 0 ? true : false;
	}

	public boolean modifyMenu(MenuDTO menu) {
		
		SqlSession sqlSession = getSqlSession();
		
		menuMapper = sqlSession.getMapper(MenuMapper.class);
		int result = menuMapper.updateMenu(menu);
		
		if(result > 0) {
			sqlSession.commit();
		} else {
			sqlSession.rollback();
		}
		
		sqlSession.close();

		return result > 0 ? true : false;
		
	}

	public boolean deleteMenu(int code) {
		
		SqlSession sqlSession = getSqlSession();
		
		menuMapper = sqlSession.getMapper(MenuMapper.class);
		int result = menuMapper.deleteMenu(code);
		
		if(result > 0) {
			sqlSession.commit();
		} else {
			sqlSession.rollback();
		}
		
		sqlSession.close();

		return result > 0 ? true : false;
	}

}

 

MenuMapper

package com.greedy.section02.javaconfig;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface MenuMapper {

	@Results(id="menuResultMap", value= {
			@Result(id = true, property = "code", column = "MENU_CODE"),
			@Result(property = "name", column = "MENU_NAME"),
			@Result(property = "price", column = "MENU_PRICE"),
			@Result(property = "categoryCode", column = "CATEGORY_CODE"),
			@Result(property = "orderableStatus", column = "ORDERABLE_STATUS"),
			
	})
	@Select("SELECT \n" +
			"		MENU_CODE\n" +
			"	  , MENU_NAME\n" +
			"	  , MENU_PRICE\n" +
			"	  , CATEGORY_CODE\n" +
			"	  , ORDERABLE_STATUS\n" +
			"  FROM TBL_MENU\n" +
			" WHERE ORDERABLE_STATUS = 'Y'\n" +
			" ORDER BY MENU_CODE")
	List<MenuDTO> selectAllMenu();

	@Select("SELECT \n" +
			"		MENU_CODE\n" +
			"	  , MENU_NAME\n" +
			"	  , MENU_PRICE\n" +
			"	  , CATEGORY_CODE\n" +
			"	  , ORDERABLE_STATUS\n" +
			"  FROM TBL_MENU\n" +
			" WHERE ORDERABLE_STATUS = 'Y'\n" +
			"   AND MENU_CODE = #{ code }")
	
	@ResultMap("menuResultMap")
	MenuDTO selectMenuByCode(int code);

	@Insert("INSERT \n" +
			"  INTO TBL_MENU\n" +
			"(\n" +
			"  MENU_CODE\n" +
			", MENU_NAME\n" +
			", MENU_PRICE\n" +
			", CATEGORY_CODE\n" +
			", ORDERABLE_STATUS\n" +
			")\n" +
			"VALUES\n" +
			"(\n" +
			"  SEQ_MENU_CODE.NEXTVAL\n" +
			", #{ name }\n" +
			", #{ price }\n" +
			", #{ categoryCode }\n" +
			", 'Y'\n" +
			")")
	int insertMenu(MenuDTO menu);

	@Update("UPDATE \n" +
			"		TBL_MENU\n" +
			"   SET MENU_NAME = #{ name }\n" +
			"     , MENU_PRICE = #{ price }\n" +
			"     , CATEGORY_CODE = #{ categoryCode }\n" +
			" WHERE MENU_CODE = #{ code }\n")
	int updateMenu(MenuDTO menu);

	@Delete("DELETE \n" +
			"  FROM TBL_MENU\n" +
			" WHERE MENU_CODE = #{ code }\n")
	int deleteMenu(int code);
	

	
}