<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
DB 연결 정보가 들었는 properties 폴더생성
마이바티스 설정이 있는 ServletAppContext
//Spring MVC 관련된 설정을 하는 클래스
@Configuration
//Controller 어노테이션이 셋팅되어 있는 클래스를 Controller로 등록한다.
@EnableWebMvc
//스캔할 패키지를 지정한다.
@ComponentScan("com.demo.controller")
@ComponentScan("com.demo.beans")
@PropertySource("/WEB-INF/properties/db.properties")
public class ServletAppContext implements WebMvcConfigurer {
@Value("${db.classname}")
private String db_classname;
@Value("${db.url}")
private String db_url;
@Value("${db.username}")
private String db_username;
@Value("${db.password}")
private String db_password;
@Autowired
private MenuService menuService;
// Controller의 메서드가 반환하는 jsp의 이름 앞뒤에 경로와 확장자를 붙혀주도록 설정한다.
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
WebMvcConfigurer.super.configureViewResolvers(registry);
registry.jsp("/WEB-INF/views/", ".jsp");
}
// 정적 파일의 경로를 매핑한다.
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
WebMvcConfigurer.super.addResourceHandlers(registry);
registry.addResourceHandler("/**").addResourceLocations("/resources/");
}
// 데이터베이스 접속 정보 관리
@Bean
public BasicDataSource dataSource() {
BasicDataSource source = new BasicDataSource();
source.setDriverClassName(db_classname);
source.setUrl(db_url);
source.setUsername(db_username);
source.setPassword(db_password);
return source;
}
// 쿼리문과 접속 관리하는 객체
@Bean
public SqlSessionFactory factory(BasicDataSource source) throws Exception{
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(source);
SqlSessionFactory factory = factoryBean.getObject();
return factory;
}