Spring Ders16 - Database ( Veritabanı ) - RowMapper
RowMapper arayüzü (interface), ResultSetExtractor arayüzünde olduğu gibi JdbcTemplate sınıfının query metoduyla veritabanından verileri çekmeye yarayan bir arayüzdür.
query metodunun imzası (signatur) aşağıdaki gibidir.
Şimdi örnek programımızı yapalım.
Main.java
package test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BilgiIslem bilgici = (BilgiIslem) context.getBean("bilgiislem"); List <Personel> list = bilgici.getPersoneller(); System.out.println("ID\tAD\tMAAS\n"); for(Personel p:list){ System.out.println(p.getId()+"\t"+p.getAd()+"\t"+p.getMaas()); } } }
Personel.java
package test; public class Personel { private int id; private int maas; private String ad; public Personel() { } public Personel(int id, String ad, int maas) { this.id = id; this.ad = ad; this.maas = maas; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getMaas() { return maas; } public void setMaas(int maas) { this.maas = maas; } public String getAd() { return ad; } public void setAd(String ad) { this.ad = ad; } }
BilgiIslem.java
package test; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; public class BilgiIslem { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List<Personel> getPersoneller() { return jdbcTemplate.query("select * from bilgiislem", new RowMapper<Personel>() { @Override public Personel mapRow(ResultSet rs, int satirNumarasi) throws SQLException { Personel p = new Personel(); p.setId(rs.getInt(1)); p.setAd(rs.getString(2)); p.setMaas(rs.getInt(3)); return p; } }); } }
applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="****" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <bean id="bilgiislem" class="test.BilgiIslem"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>
test.sql
create table bilgiislem( id int, ad varchar(255), maas int );
Yorumlar
Yorum Gönder