๐Ÿ’ Springboot mybatis, mysql ์„ค์ • ๐Ÿ’

bo-yoonยท2021๋…„ 5์›” 16์ผ
2

springboot settings

๋ชฉ๋ก ๋ณด๊ธฐ
1/2

springboot ํ”„๋กœ์ ํŠธ์— mysql ๋””๋น„๋ฅผ ์—ฐ๊ฒฐํ•ด๋ณด์ž! ๐Ÿ’ ๐Ÿ’ ๐Ÿ’

๐Ÿ’ 0. ์˜์กด์„ฑ

	implementation 'mysql:mysql-connector-java'
        implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
   	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'


๐Ÿ’ 1. ๋””๋น„ ์ƒ์„ฑ

๋„์ปค๋กœ ๋งˆ๋ฆฌ์•„๋””๋น„ ์„ค์น˜ ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ mysql ๋กœ ํ•ด์ฃผ์—ˆ๋‹ค.


docker run -p 3309:3306 --name mysql_boot -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_DATABASE=memodb -e MYSQL_USER=borab -e MYSQL_PASSWORD=pass -d mysql --character-set-server=utf8 --collation-server=utf8_unicode_ci

docker exec -it mysql_boot bash
mysql -u borab -p
pass

๐Ÿ’ 2. ํ…Œ์ด๋ธ” ์ƒ์„ฑ

use memodb


CREATE TABLE MEMOS(
    MEMO_ID INT  PRIMARY KEY,
    TITLE VARCHAR(200),
    CONTENT VARCHAR(1000)

);

๐Ÿ’ 3. application.yml ์ถ”๊ฐ€

  • ์Šคํ”„๋ง ๋ถ€ํŠธ

spring:
  datasource:
    platform: mysql
    jdbc-url: jdbc:mysql://localhost:3309/hobbydb?characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 1234
    driver-class-name: com.mysql.cj.jdbc.Driver

  • mysql ๋ฒ„์ „ 8 ๋ถ€ํ„ฐ๋Š” com.mysql.cj.jdbc.Driver ๋ฅผ ์‚ฌ์šฉํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์ด์ „์—๋Š” com.mysql.jdbc.Driver

  • jdbc:์‚ฌ์šฉ๋””๋น„์ข…๋ฅ˜://์„œ๋ฒ„ ip:์„œ๋ฒ„ port/๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…?์˜ต์…˜


๐Ÿ’ 4. /mappers/*.xml ์ถ”๊ฐ€

  • /resources/mappers ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ 
    ๊ทธ์— ๋งž์ถฐ์„œ xml ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

๐Ÿ’ 5. DataSourceConfig ๋งŒ๋“ค๊ธฐ


@Slf4j
@Configuration
@PropertySource("classpath:/application.yml")
public class DatasourceConfig {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariConfig hikariConfig() {
    	// application.yml ์— ์žˆ๋Š” spring.datasource ๋ฅผ ์ฝ์–ด HikariConfig์— ๊ด€๋ จ๋œ ์„ค์ •์„ ํ•ด์ค€๋‹ค.
        
        return new HikariConfig();
    }

    @Bean
    public DataSource dataSource() {
    	// hikariConfig๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ datasource ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
        DataSource dataSource = new HikariDataSource(hikariConfig());
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:/mappers/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}


hikariCP๋Š” ๋ฌด์—‡์ธ๊ฐ€?

  • springboot 2 ๋ถ€ํ„ฐ ์ง€์ •๋œ default db connection pool

SqlSessionFactory

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—ฐ๋™ํ•ด์ฃผ๋Š” ๊ฐ์ฒด

SqlSessionTemplate

  • Mybatis ์ฟผ๋ฆฌ๋ฌธ์„ ์‹คํ–‰ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๐Ÿ’ 6. ๋กœ์ง ์ž‘์„ฑ

  • mapper ์ธํ„ฐํŽ˜์ด์Šค์™€ service ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • ์—ฌ๊ธฐ์— ์‚ฌ์šฉํ•  model๋„ ์ž‘์„ฑํ•ด์ค€๋‹ค.

@Repository
@Mapper
public interface ApiMapper {

   boolean insertMemo(Memo memo);

   
}

@Service
public class MemoService {


    private final ApiMapper apiMapper;

    public boolean insertMemo(Memo memo) {
        return  this.apiMapper.insertMemo(memo);
    }
    
}
@Data
@ApiModel(description = "๋ฉ”๋ชจ ๊ฐ์ฒด")
public class Memo {

    @ApiModelProperty(notes = "๋ฉ”๋ชจ ์•„์ด๋””", example = "1592827184", position = 1)
    long id;

    @ApiModelProperty(notes = "๋ฉ”๋ชจ ์ œ๋ชฉ", example = "script1", position = 2)
    String title;

    @ApiModelProperty(notes = "๋ฉ”๋ชจ ๋‚ด์šฉ", example = "hihi, my name id borabee", position = 3)
    String content;
}

๐Ÿ’ 7. 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.memo.app.api.mapper.ApiMapper">

    <insert id="insertMemo" parameterType="com.memo.app.api.model.Memo">
        /* MemoController. */
        INSERT INTO MEMOS (MEMO_ID, TITLE , CONTENT) VALUES (#{id}, #{title}, #{content})
    </insert>
    
</mapper>

๐Ÿ’ 8. ์‹คํ–‰

๋~!

profile
๊ฐœ๋ฐœ ๋กœ๊ทธ ๐ŸŽ ๐ŸŽ ๐ŸŽ

0๊ฐœ์˜ ๋Œ“๊ธ€