springboot ํ๋ก์ ํธ์ mysql ๋๋น๋ฅผ ์ฐ๊ฒฐํด๋ณด์! ๐ ๐ ๐
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'
๋์ปค๋ก ๋ง๋ฆฌ์๋๋น ์ค์น ์ ๋ง์ฐฌ๊ฐ์ง๋ก 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
use memodb
CREATE TABLE MEMOS(
MEMO_ID INT PRIMARY KEY,
TITLE VARCHAR(200),
CONTENT VARCHAR(1000)
);
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
/๋ฐ์ดํฐ๋ฒ ์ด์ค๋ช
?์ต์
@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๋ ๋ฌด์์ธ๊ฐ?
SqlSessionFactory
SqlSessionTemplate
@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;
}
<?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>
๋~!