1. MySQL, MayBatis 의존성 추가
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2. application.properties 작성하기
src.main.resources경로에 있는 application.properties에 작성
spring.datasource.username=(mySQL 아이디)
spring.datasource.password=(mySQL 비밀번호)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/(db이름)?serverTimezone=UTC&allowPublicKeyRetrieval=true
3. src.main.java경로에 config 패키지 생성 및 파일 추가
MyBatisConfig.java
package com.example.demo.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@MapperScan(basePackages = "(dao 패키지 경로)")
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory (DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setTypeAliasesPackage("(dto 패키지 경로)");
return sqlSessionFactory.getObject();
}
@Bean
public SqlSessionTemplate sqlSession (SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4. dao 패키지 생성 후
Mapper.java 인터페이스, mapper.xml 파일 생성
TestMapper.java
package kr.co.vuetest.dao;
import java.util.List;
import org.springframework.stereotype.Component;
import kr.co.vuetest.dto.TestDTO;
@Component
public interface TestMapper {
public void insertTest(TestDTO testDTO) throws Exception;
}
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="kr.co.vuetest.dao.TestMapper">
<insert id="insertTest" parameterType="kr.co.vuetest.dto.TestDTO">
INSERT INTO
test
VALUES
(#{id}, #{name}, #{gender})
</insert>
</mapper>
'spring-vue-project' 카테고리의 다른 글
4. 스프링 부트 lombok 적용 (0) | 2021.11.07 |
---|---|
2. Vue-SpringBoot axios 연동 (0) | 2021.11.05 |