본문 바로가기
SpringBoot/(책)스프링부트 시작하기

[springboot] 3-3. springboot mybatis typeAliases 설정하기

by 평범한kiki 2023. 4. 23.

매번 아래처럼 쓰기는 길고 나중에 고칠때 힘들다

<select id="selectBoardList" resultType="board.board.dto.BoardDto">

이렇게 쓸수 있게 typeAliases 지정하자

<select id="selectBoardDetail" parameterType="int" resultType="BoardDto">

 

1. /resources/mybatis/mybatis-config.xml

- typeAliases 추가하자

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		  <setting name="cacheEnabled" value="true"/>
		  <setting name="lazyLoadingEnabled" value="true"/>
		  <setting name="useGeneratedKeys" value="true"/>
		  <setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
	<typeAliases>
		<typeAlias type="board.board.dto.BoardDto" alias="BoardDto"/>	
	</typeAliases>	
</configuration>

 

2. 설정하자

/board/src/main/java/board/configuration/DatabaseConfiguration.java

-sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:/mybatis/mybatis-config.xml"));  

@Bean 
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    //앞에서 만든 데이터 소스 설정
    sqlSessionFactoryBean.setDataSource(dataSource);
    //mybatis.configuration 설정
    //sqlSessionFactoryBean.setConfiguration(mybatisConfig());
    //xml로 설정시
    sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:/mybatis/mybatis-config.xml")); 
    //mybatis 매퍼(Mapper)파일 위치 설정
    sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:/mapper/**/sql-*.xml")); 
    return sqlSessionFactoryBean.getObject();
}