ποΈ Faker ν μ€νΈ λ°μ΄ν°
ποΈ Faker ν
μ€νΈ λ°μ΄ν°
Faker λμ
κ·Όκ±°
- νμλ ν μ€νΈ λ°μ΄ν°λ λ§λ€μ΄μ§ κΈ°λ₯μ ν΅ν΄ μμ±ν΄μΌ νλ€κ³ μκ°νλ€.
- κΈ°μ‘΄
Sequenceμ μν₯μ μ£Όκ±°λ νΉμ κΈ°λ₯μ μ°μ λμ΄ μμ±ν΄μΌ νλ λ°μ΄ν°κ° μ‘΄μ¬νκΈ° λλ¬ΈμSELECT쿼리 μΈμλDBλ₯Ό ν΅ν λ°μ΄ν° μ‘°μμ μ§μν΄μλ€. - κ·Έλ°λ°, λ°λλ‘ μκ°νλ©΄ κΈ°μ‘΄ λ°μ΄ν°λ λ€λ₯Έ ν
μ΄λΈμ μν₯μ μ£Όμ§ μλ μ μμλ
DBμ‘°μμ ν΅ν΄ λ°μ΄ν°λ₯Ό μμ±ν΄λ 무방νλ€λ μκ°μ΄ λ€μλ€. - μ μ λ°μ΄ν°λ₯Ό μμ±νλ €λ, μ΄λ€ κ°μ μ£Όμ
ν΄μΌ ν μ§ μκ°νλ κ²μ λ²κ±°λ‘μμ λκ»΄ μ½κ² λλ―Έ λ°μ΄ν°λ₯Ό μμ±ν μ μλ
FakerλΌμ΄λΈλ¬λ¦¬λ₯Ό μ°Ύκ² λμλ€.
μμ‘΄μ± μ£Όμ
1
2
/* Faker */
implementation 'net.datafaker:datafaker:2.2.2'
λλ―Έ λ°μ΄ν° μμ±
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
val faker = Faker()
val url = "jdbc:log4jdbc:postgresql://localhost:5432/postgres"
val user = "postgres"
val password = "1234"
DriverManager.getConnection(url, user, password).use { conn ->
val sql = "INSERT INTO tbl_notice (id, title, content, created_at, updated_at, writer_email) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO NOTHING"
conn.prepareStatement(sql).use { stmt ->
for (i in 1..101) {
stmt.setInt(1, i)
stmt.setString(2, faker.dog().name())
stmt.setString(3, faker.dog().sound())
stmt.setTimestamp(4, faker.date().birthday())
stmt.setTimestamp(5, faker.date().birthday())
stmt.setString(6, "test1@test.com")
stmt.executeUpdate()
}
stmt.close()
}
conn.close()
}
}
KotlinμΈμ΄λ‘ μμ±λ μ½λμ΄λ€.- νλ‘μ νΈλ₯Ό μ€ννλ©΄ μ΄μ
main()λ©μλμ ν¨κ»INSERTλ¬Έμ΄ μ€νλλ€. - νλ‘μ νΈλ₯Ό λ€μ μ€ν μ λλ―Έ λ°μ΄ν°κ° μ΄λ―Έ μ‘΄μ¬ν κ²½μ°
IDκ° μ€λ³΅λλ λ¬Έμ λ₯Ό κ³ λ €ν΄μΌ νλ€.
101κ°μ λ°μ΄ν°κ° μ μμ±λμλ€.
This post is licensed under CC BY 4.0 by the author.
