Bibi's DevLog ๐ค๐
[Spring] Spring JdbcTemplate ๊ฐ์ด๋ (๊ณต์๋ฌธ์, Baeldung) ๋ณธ๋ฌธ
[Spring] Spring JdbcTemplate ๊ฐ์ด๋ (๊ณต์๋ฌธ์, Baeldung)
๋น๋น bibi 2021. 5. 23. 22:53Spring JdbcTemplate์ ๋ํ ๊ณต์๋ฌธ์ ๊ฐ์ด๋
(K๊ฐ ์๋ ค์ฃผ์ จ๋ค๐โโ๏ธ ๊ฐ์ฌํฉ๋๋ค!)
3.1 Choosing an Approach for JDBC Database Access์์ ๊ฐ๋จํ ์ค๋ช ์ ์ฝ๊ณ
3.3.1. UsingJdbcTemplate ๋ถ๋ถ๋ถํฐ ๋ณด๋ฉด ๋ ๋ฏ!
https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#jdbc
3.1 Choosing an Approach for JDBC Database Access (JDBC ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ํ ์ ๊ทผ๋ฐฉ์ ์ ํ)
JdbcTemplate
, SimpleJdbcInsert
, SimpleJdbcCall
, ๋ฑ๋ฑ์ ๋ฐฉ์๋ค์ด JDBC ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๊ทผ์ ์ฝ๊ฒ ํ๋ค.
์ ์ค ํ๋๋ฅผ ๊ณจ๋ผ ์ฐ๋๋ผ๋, ๋๋จธ์ง ๋ฐฉ์์ ์์ด ์ฌ์ฉํ ์๋ ์๋ค.
JdbcTemplate
: ๊ธฐ๋ณธ์ ์ด๋ฉฐ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ์คํ๋ง JDBC ์ ๊ทผ๋ฒ. ๊ฐ์ฅ ๋ฎ์ ์์ค์ ์ ๊ทผ.- ๋๋จธ์ง ๋ฐฉ์๋ค์ ๋ชจ๋ ์ด ๋ฐฉ์์ ํ์ฅ์ด๋ค.
NamedParameterJdbcTemplate
: JDBC์?
๋ฐฉ์ ๋์ ๋ช ๋ช ๋ ๋งค๊ฐ๋ณ์ ์ฌ์ฉํ ์ ์๋๋ก ํจ. SQL๋ฌธ์ ์ฌ๋ฌ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ ๋ ์ ์ฉํจ.SimpleJdbcInsert
,SimpleJdbcCall
: ํ์ํ ๊ตฌ์ฑ์ ๊ท๋ชจ๋ฅผ ์ ํํ๊ธฐ ์ํด ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ต์ ํํ๋ค.
3.3.1 Using JdbcTemplate
JdbcTemplate
๋ JDBC ์ฝ์ด ํจํค์ง์ ์ค์ฌ ํด๋์ค์ด๋ค.
- ์์์ ์์ฑ ๋ฐ ๋ฆด๋ฆฌ์ค๋ฅผ ๋ค๋ฃฌ๋ค.
- JDBC์ ์ค์ฌ ์ํฌํ๋ก์ฐ์ ๊ธฐ๋ณธ ์์ ์ ๋ค๋ฃฌ๋ค.
JdbcTemplate
ํด๋์ค๋..
- SQL์ฟผ๋ฆฌ๋ค์ ์คํํจ
- ๊ตฌ๋ฌธ๊ณผ ์ ์ฅ๋ ํ๋ก์์ ํธ์ถ์ ์์ ํจ
ResultSet
์ธ์คํด์ค์ ๋ํ ๋ฐ๋ณต์ ์คํํจ- ๋ฐํ๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ ์ถ์ถํจ
- JDBC ์์ธ๋ฅผ ์ก์ ๋ ์ผ๋ฐ์ ์ด๊ณ ์ ์ตํ๊ฒ ๋ณํํจ
โป DataSource
๋ ํญ์ ์คํ๋ง ์ปจํ
์ด๋์์ ๋น์ผ๋ก ๊ตฌ์ฑํด์ผ ํจ.
SELECT ์ฟผ๋ฆฌ
String์ ์ฐพ๋ ์ฟผ๋ฆฌ
- id๊ฐ 1212์ธ ๋ฐฐ์ฐ์ ์ฑ์ ๊ฐ์ ธ์ด
String lastName = this.jdbcTemplate.queryForObject( "select last_name from t_actor where id = ?", String.class, 1212L);
๋ฐ์ธ๋ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ์ฟผ๋ฆฌ
- ์ด๋ฆ์ด Joe์ธ ๋ฐฐ์ฐ์ ์๋ฅผ ์
int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject( "select count(*) from t_actor where first_name = ?", Integer.class, "Joe");
๊ด๊ณ์๋ ํ(row)์ ์๋ฅผ ์ธ๋ ์ฟผ๋ฆฌ
- ๋ฐฐ์ฐ์ ์๋ฅผ ์ ?
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_actor", Integer.class);
ํ๋์ ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์์ ๊ฐ์ ์ฑ์ฐ๋ ์ฟผ๋ฆฌ
- ์์ด๋๊ฐ 1212์ธ ๋ฐฐ์ฐ์ ์ฑ๊ณผ ์ด๋ฆ์ ์ฐพ์
Actor actor = jdbcTemplate.queryForObject( "select first_name, last_name from t_actor where id = ?", (resultSet, rowNum) -> { Actor newActor = new Actor(); newActor.setFirstName(resultSet.getString("first_name")); newActor.setLastName(resultSet.getString("last_name")); return newActor; }, 1212L);
์ฌ๋ฌ ๋๋ฉ์ธ ๊ฐ์ฒด(๋ฆฌ์คํธ)๋ฅผ ์ฐพ์์ ๊ฐ์ ์ฑ์ฐ๋ ์ฟผ๋ฆฌ
- ๋ฐฐ์ฐ๋ค์ ์ฑ๊ณผ ์ด๋ฆ์ ์ฐพ์
List<Actor> actors = this.jdbcTemplate.query( "select first_name, last_name from t_actor", (resultSet, rowNum) -> { Actor actor = new Actor(); actor.setFirstName(resultSet.getString("first_name")); actor.setLastName(resultSet.getString("last_name")); return actor; });
ํ๋์ ๋๋ฉ์ธ ๊ฐ์ฒด & ์ฌ๋ฌ ๋๋ฉ์ธ ๊ฐ์ฒด ๋ฅผ ์ฐพ์ ๋
RowMapper
์ฝ๋๊ฐ ์ค๋ณต๋๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ค๋ณต์ ์ ๊ฑฐํ ์ ์๋คprivate final RowMapper<Actor> actorRowMapper = (resultSet, rowNum) -> { Actor actor = new Actor(); actor.setFirstName(resultSet.getString("first_name")); actor.setLastName(resultSet.getString("last_name")); return actor; }; public List<Actor> findAllActors() { return this.jdbcTemplate.query( "select first_name, last_name from t_actor", actorRowMapper); }
INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ ๊ทธ๋ฆฌ๊ณ JdbcTemplate
update(...)
๋ฉ์๋๋ฅผ ํตํด INSERT, UPDATE, DELETE๋ฅผ ๋ชจ๋ ์ํํ ์ ์๋ค. ๋งค๊ฐ๋ณ์ ๊ฐ์ ๋ณดํต ๋ณ์ / ๊ฐ์ฒด ๋ฐฐ์ด๋ก ์ ๊ณต๋๋ค.
์ ์ํธ๋ฆฌ๋ฅผ ์ฝ์ ํ๋ ์ฟผ๋ฆฌ
- ์ฑ์ด Watling, ์ด๋ฆ์ด Leonor์ธ ๋ฐฐ์ฐ๋ฅผ ์ถ๊ฐํ๋ค
this.jdbcTemplate.update( "insert into t_actor (first_name, last_name) values (?, ?)", "Leonor", "Watling");
๊ธฐ์กด ์ํธ๋ฆฌ๋ฅผ ์์ ํ๋ ์ฟผ๋ฆฌ
- id๊ฐ 5276์ธ ๋ฐฐ์ฐ์ ์ด๋ฆ์ Banjo๋ก ์์ ํ๋ค
this.jdbcTemplate.update( "update t_actor set last_name = ? where id = ?", "Banjo", 5276L);
๊ธฐ์กด ์ํธ๋ฆฌ๋ฅผ ์ญ์ ํ๋ ์ฟผ๋ฆฌ
- id๊ฐ
actorId
์ธ ๋ฐฐ์ฐ๋ฅผ ์ญ์ ํ๋ค this.jdbcTemplate.update( "delete from t_actor where id = ?", Long.valueOf(actorId));
- id๊ฐ
JdbcTemplate
๋ชจ๋ฒ ์ฌ๋ก
์๋์ ๊ฐ์ด DataSource
๋ ์ค์ ํ์ผ์์ ์์ฑํ๊ณ (Spring Configuration), ๊ทธ ๋น์ DAOํด๋์ค์ ์์กด์ฑ ์ฃผ์
ํ๋ ๋ฐฉ์์ด ๊ถ์ฅ๋๋ค.
- DAO ํด๋์ค
public class JdbcCorporateEventDao implements CorporateEventDao { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } // JDBC-backed implementations of the methods on the CorporateEventDao follow... }
- ์ค์ ํ์ผ (XML)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="corporateEventDao" class="com.example.JdbcCorporateEventDao"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/> </beans>
๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์์กด์ฑ ์ฃผ์ ์ ํ๊ธฐ ์ํด์๋ ์ปดํฌ๋ํธ์ค์บ๊ณผ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ ์ ์๋ค.
- DAO ํด๋์ค
@Repository public class JdbcCorporateEventDao implements CorporateEventDao { private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } // JDBC-backed implementations of the methods on the CorporateEventDao follow... }
- ์ค์ ํ์ผ (XML)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- Scans within the base package of the application for @Component classes to configure as beans --> <context:component-scan base-package="org.springframework.docs.test" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/> </beans>
3.3.2 Using NamedParameterJdbcTemplate
NamedParameterJdbcTemplate
ํด๋์ค๋ JDBC ๊ตฌ๋ฌธ์ ๋ช
๋ช
๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ์ง์ํด ์ค๋ค.
๊ธฐ์กด JDBC๊ตฌ๋ฌธ๊ณผ ๋ค๋ฅธ ์ ์ ?
๋ง์ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ผ ์ ์ํ ๋งค๊ฐ๋ณ์๋ช
์ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
์ด ํด๋์ค๋ JdbcTemplate
๋ฅผ ํฌํจํ๋ค.
๋ช ๋ช ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด JDBC๊ตฌ๋ฌธ์ ํ๋ก๊ทธ๋๋ฐํ ๋ ์ด ํด๋์ค๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from T_ACTOR where first_name = :first_name";
SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}
๋๋ ์๋์ ๊ฐ์ด, Map
๊ธฐ๋ฐ์ผ๋ก ๋ช
๋ช
๋ ๋งค๊ฐ๋ณ์์ ๊ทธ์ ์์ํ๋ ๊ฐ์ ์ ๋ฌํ ์ ์๋ค.
// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from T_ACTOR where first_name = :first_name";
Map<String, String> namedParameters = Collections.singletonMap("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}
NamedParameterJdbcTemplate
์๋ SqlParameterSource
์ธํฐํ์ด์ค๊ฐ ์กด์ฌํ๋ค.
SqlParameterSource
:NamedParameterJdbcTemplate
์ ๋ช ๋ช ๋ ๋งค๊ฐ๋ณ์์ source์ด๋ค.MapSqlParameterSource
ํด๋์ค :java.util.Map
์ ์ด๋ํฐ(ํค๊ฐ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ด๊ณ , ๊ฐ์ด ๋งค๊ฐ๋ณ์ ๊ฐ)์ธ ๊ฐ๋จํ ๊ตฌํ์ฒด์ด๋ค.BeanPropertySqlParameterSource
ํด๋์ค : ์์์ ์๋ฐ๋น์ ๋ฉํํ๊ณ , ๋ฉํ๋ ์๋ฐ๋น์ ์์ฑ์ ๋ช ๋ช ๋ ๋งค๊ฐ๋ณ์์ ๊ฐ์ source๋ก ์ฌ์ฉํจ์์๋ ์๋์ ๊ฐ๋ค.
public class Actor { private Long id; private String firstName; private String lastName; public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public Long getId() { return this.id; } // setters omitted... }
// some JDBC-backed DAO class... private NamedParameterJdbcTemplate namedParameterJdbcTemplate; public void setDataSource(DataSource dataSource) { this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); } public int countOfActors(Actor exampleActor) { // notice how the named parameters match the properties of the above 'Actor' class String sql = "select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName"; SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor); return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class); }
3.3.4 Running Statements
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class ExecuteAStatement {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void doExecute() {
this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");
}
}
3.3.5 Running Queries
queryForObject()
: ํ๋์ ๊ฐ์ ๋ฆฌํดํ ๋ (๊ฐฏ์๋ฅผ ์ธ๊ฑฐ๋, ํ ํ์ ๊ฐ์ ๊ฒ์ )๋งค๊ฐ๋ณ์๋ก
int
๋String
์ ๋ฃ์ ์ ์๋คimport javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class RunAQuery { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public int getCount() { return this.jdbcTemplate.queryForObject("select count(*) from mytable", Integer.class); } public String getName() { return this.jdbcTemplate.queryForObject("select name from mytable", String.class); } }
queryForList()
: ์ฌ๋ฌ ํญ๋ชฉ์ ๋ฆฌ์คํธ๋ก ๋ฆฌํดํ ๋private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public List<Map<String, Object>> getList() { return this.jdbcTemplate.queryForList("select * from mytable"); }
๋ฆฌํด๋๋ ๋ฆฌ์คํธ์ ํํ๋
[{name=Bob, id=1}, {name=Mary, id=2}]
์ ๊ฐ๋ค.
3.3.5. Updating the Database
ํน์ PK์ ์ปฌ๋ผ์ ์์ ํ๋ ๊ฒฝ์ฐ์ ์์.
import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class ExecuteAnUpdate { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public void setName(int id, String name) { this.jdbcTemplate.update("update mytable set name = ? where id = ?", name, id); } }
Spring JDBC Template ํํ ๋ฆฌ์ผ (Baeldung) (์์ฑ ์ค)
- datasource ํจํค์ง : ๋ฐ์ดํฐ ์์ค์ ์ก์ธ์คํ๊ธฐ ์ํ ์ ํธ๋ฆฌํฐ ํด๋์ค.
2. ๊ตฌ์ฑ
๋ฐ์ดํฐ ์์ค์ ๊ฐ๋จํ ๊ตฌ์ฑ. (MySQL ์ฌ์ฉ)
๋ฐ์ดํฐ์์ค๋ฅผ ์์ฑํ๊ณ ์คํ๋ง ๋น์ผ๋ก ๋ง๋ค๊ธฐ.
@Configuration
@ComponentScan("com.baeldung.jdbc")
public class SpringJdbcConfig {
@Bean
public DataSource mysqlDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
dataSource.setUsername("guest_user");
dataSource.setPassword("guest_password");
return dataSource;
}
}
3. JdbcTemplate ๋ฐ ์ฟผ๋ฆฌ ์คํ
3.1. ๊ธฐ๋ณธ ์ฟผ๋ฆฌ
JdbcTemplate API๋ก ์๋์ ๊ฐ์ ๋๋ถ๋ถ์ ๊ธฐ๋ฅ์ ์ก์ธ์คํ ์ ์๋ค.
- Connection ์์ฑ ๋ฐ ์ข ๋ฃ
- ๊ตฌ๋ฌธ ์คํ ๋ฐ ์ ์ฅ๋ ์ ์ฐจ ํธ์ถ
ResultSet
์ ์ํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
๊ฐ๋จํ SELECT๋ฌธ
int result = jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
๊ฐ๋จํ INSERT๋ฌธ
public int addEmplyee(int id) { return jdbcTemplate.update( "INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA"); }
?
๋ฅผ ํตํด ๋งค๊ฐ๋ณ์๋ฅผ ์ ๊ณตํ๋ค.
3.2. ๋ช ๋ช ๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์ฟผ๋ฆฌ
์ด๋ฆ์ด ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด NamedParameterJdbcTemplate
๋ผ๋ ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- (?) ๋ด๋ถ์ ์ผ๋ก
?
๋ฅผ ์ด๋ฆ ์๋ ํ๋ผ๋ฏธํฐ๋ก ๋์ฒดํ๋ฉฐ, ์ฟผ๋ฆฌ ์คํ์ ์ํด ๊ฐ์ผ JdbcTemplate๋ก ์์ํ๋ค
๋ช
๋ช
๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ํ ๊ฐ์ ์ ๊ณตํ๊ธฐ ์ํด MapSqlParameterSource
๋ฅผ ์ฌ์ฉํ๋ค.
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", 1); return namedParameterJdbcTemplate.queryForObject( "SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id", namedParameters, String.class);
๋ช ๋ช ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฒฐ์ ์ง๊ธฐ ์ํด ์คํ๋ง ๋น์ผ๋ก๋ถํฐ ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค.
Employee employee = new Employee(); employee.setFirstName("James"); String SELECT_BY_ID = "SELECT COUNT(*) FROM EMPLOYEE WHERE FIRST_NAME = :firstName"; SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(employee); return namedParameterJdbcTemplate.queryForObject( SELECT_BY_ID, namedParameters, Integer.class);
BeanPropertySqlParameterSource
๋ฅผ ์ฌ์ฉํ ๊ตฌํ (์ ์ฒ๋ผ ๋ช ๋ช ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ํน์ ํ๋ ๋์ )
3.3. Java ๊ฐ์ฒด์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๋งตํํ๊ธฐ
RowMapper
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํจ์ผ๋ก์จ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์๋ฐ ๊ฐ์ฒด์ ๋งตํํ ์ ์๋ค.
์๋ฅผ ๋ค์ด ์ฟผ๋ฆฌ์ ์ํด ๋ฐํ๋๋ ๊ฐ ํ์ ๋ํด, ์คํ๋ง์ด row mapper๋ฅผ ์ฌ์ฉํด ์๋ฐ ๋น์ ์ฑ์ด๋ค.
public class EmployeeRowMapper implements RowMapper<Employee> { @Override public Employee mapRow(ResultSet rs, int rowNum) throws SQLException { Employee employee = new Employee(); employee.setId(rs.getInt("ID")); employee.setFirstName(rs.getString("FIRST_NAME")); employee.setLastName(rs.getString("LAST_NAME")); employee.setAddress(rs.getString("ADDRESS")); return employee; } }
๊ทธ ๋ค์, row mapper๋ฅผ ์ฟผ๋ฆฌ API์ ์ ๋ฌํ๊ณ ์์ ํ ์ฑ์์ง ์๋ฐ ๊ฐ์ฒด๋ฅผ ์ป์ ์ ์๋ค.
String query = "SELECT * FROM EMPLOYEE WHERE ID = ?"; Employee employee = jdbcTemplate.queryForObject( query, new Object[] { id }, new EmployeeRowMapper());
'๐ฅ BE ๋ฐฑ์๋ > Spring ์คํ๋ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] ๊ฐ์ URL์ ์ฌ์ฉํ๋ฉด์๋ ๋ค๋ฅธ ํ URL์ ์ฟผ๋ฆฌ์คํธ๋ง์ ๋ฐ๊ณ ์ถ์ ๋ ๋งตํํ๋ ๋ฒ (0) | 2021.05.28 |
---|---|
[Java Spring] DB์ ๋ ์ง ์ ์ฅํ๊ธฐ, JSON์ผ๋ก ๋ ์ง ๋ฐ๊ธฐ (0) | 2021.05.28 |
[Spring] @Transactional, ํธ๋์ญ์ (0) | 2021.04.30 |
[Spring] @Query (0) | 2021.04.30 |
[Spring] ResponseEntity, @RestController, @RequestBody, @ResponseBody (0) | 2021.04.30 |