DEV Community

yesjin
yesjin

Posted on

VSCode - Spring Boot & Mysql & Mybatis, 2022-11-16

Image description

  1. build.gradle에 있는 dependencies에 mybatis를 추가(mysql 컨낵터가 없다면 이것도 추가)
//mysql-connector : 
runtimeOnly 'com.mysql:mysql-connector-j'
//mybatis : 
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'

Enter fullscreen mode Exit fullscreen mode
  • 바로 실행할 경우 에러발생
Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Enter fullscreen mode Exit fullscreen mode

Image description

  1. application.properties에 붙여넣기
spring.datasource.driver=com.mysql.cj.jdbc.Driver
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/데이터베이스이름?&serverTimezone=UTC&autoReconnect=true&allowMultiQueries=true&characterEncoding=UTF-8
spring.datasource.username=계정이름
spring.datasource.password=계정비번
spring.datasource.mapper-locations=classpath:/mapper/*.xml
Enter fullscreen mode Exit fullscreen mode

Image description

  1. 위와 같이 폴더와 파일 만들어주기

  2. 파일 내용 붙여넣기
    1) DataSourceConfig.java

import javax.sql.DataSource;

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

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

Enter fullscreen mode Exit fullscreen mode

2) MySQLConfig.java

import javax.sql.DataSource;

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

@Configuration
@MapperScan ("com.example.CLOOK.mapper")
public class MySQLConfig {


    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));

        Resource myBatisConfig = new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml");
        sessionFactory.setConfigLocation(myBatisConfig);

        return sessionFactory.getObject();
    }
}
Enter fullscreen mode Exit fullscreen mode

3) TestMapper.xml

<?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.example.demo.mapper.TestMapper">
</mapper>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)