Giriş
Şu satırı dahil ederiz.
Parametre Alanı
Parametrelerde placeholder olarak "?" karakteri yani soru işareti kullanılır. Açıklaması şöyle. Bu sınıf yerine NamedParameterJdbcTemplate daha iyi olabilir.
Şu satırı dahil ederiz.
import org.springframework.jdbc.core.JdbcTemplate;Parametre Alanı
Parametrelerde placeholder olarak "?" karakteri yani soru işareti kullanılır. Açıklaması şöyle. Bu sınıf yerine NamedParameterJdbcTemplate daha iyi olabilir.
With JdbcTemplate, we generally do pass the parameter values with "?" (question mark). However, it is going to introduce the SQL injection problem. So, Spring provides another way to insert data by the named parameter. In that way, we use names instead of "?". So it is better to remember the data for the column. This can be done using NamedParameterJdbcTemplate.Benzer bir açıklama şöyle.
JdbcTemplate supports only positioned parameters (?). Replace JdbcTemplate with NamedParameterJdbcTemplate
Yani JdbcTemplate SQL cümlesi içindeki "?" boşluklarının parametrelerle doldurulmasını ister.
NamedParameterJdbcTemplate SQL cümlesi içindeki ":" ile başlayan parametrelerin belirtilen Map içindeki değerler ile doldurur.SpringBoot İçinde Kullanım
Bu sınıfı sadece Autowire etmek yeterli.
DAO İçinde Kullanım
JdbcTemplate sınıfı genellikle bir DAO içinde kullanılır. Bu sınıfı sadece Autowire etmek yeterli.
Örnek
Şöyle yaparız.
Örnek
Şöyle yaparız.
Şöyle yaparız.
Şöyle yaparız.
query metodu
query() metodu yazısına taşıdım.
Bu sınıfı sadece Autowire etmek yeterli.
DAO İçinde Kullanım
JdbcTemplate sınıfı genellikle bir DAO içinde kullanılır. Bu sınıfı sadece Autowire etmek yeterli.
Örnek
Şöyle yaparız.
public class UserDAOImpl implements UserDAO
{
  @Autowired
  private JdbcTemplate jdbcTemplate;
  ...
}
Tanımlama - DriverManagerDataSource
Örnek
Şöyle yaparız.
Şöyle yaparız.
Örnek
Şöyle yaparız.
Şöyle yaparız.
DataSource olarak test ortamında DriverManagerDataSource verilebilir.Örnek
Şöyle yaparız.
<bean  name="dataSource" id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="org.postgresql.Driver" />
  <property name="url" value="jdbc:postgresql://localhost:5432/testdbnew" /> 
  <property name="username" value="admin1" />
  <property name="password" value="admin1" />
</bean>    
<bean  id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean> 
Şöyle yaparız.
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
  <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/" />
    <property name="username" value="root" />
    <property name="password" value="admin" />
  </bean>
  ...
</beans>Örnek
Şöyle yaparız.
<bean id="abstractDataSource" abstract="true">
  <property name="driverClass" value="com.mysql.jdbc.Driver" />
  <property name="initialPoolSize" value="@initial.pool.size@" />
  <property name="minPoolSize" value="@min.pool.size@" />
  <property name="maxPoolSize" value="@max.pool.size@" /> 
</bean>
<bean id="masterDS" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  parent="abstractDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://@host@/" />
    <property name="user" value="@user@" />
    <property name="password" value="@pwd@" />
    <property name="dataSourceName" value="@dbName@" />
</bean>Şöyle yaparız.
<bean id="jdbcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
  ...
  <property name="minPoolSize" value="10" />
  <property name="maxPoolSize" value="60" />
  <property name="initialPoolSize" value="12" />
  <property name="maxConnectionAge" value="1800" />
  <property name="maxIdleTime" value="600" />
  <property name="maxIdleTimeExcessConnections" value="300" />
  <property name="idleConnectionTestPeriod" value="100" />
  <property name="acquireIncrement" value="5" />
  <property name="acquireRetryAttempts" value="30" />
  <property name="acquireRetryDelay" value="1000" />
  <property name="breakAfterAcquireFailure" value="false" />
  <property name="checkoutTimeout" value="10000" />
  <property name="testConnectionOnCheckout" value="false" />
  <property name="preferredTestQuery" value="SELECT 1;" />
  <property name="numHelperThreads" value="10" />
  <property name="maxStatements" value="1000" />
  <property name="maxStatementsPerConnection" value="25" />
  ...
</bean>Örnek
Şöyle yaparız.
DataSource dataSource = ...;
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); Şöyle yaparız.
@Bean
JdbcTemplate jdbcTemplate(){return new JdbcTemplate(datasource());}
@Bean
public DataSource datasource(){
  BasicDataSource dataSource=new BasicDataSource();
  dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  dataSource.setUrl("jdbc:mysql://localhost:3306/quizzes");
  dataSource.setUsername("root");
  dataSource.setPassword("dbpass");
  return dataSource;
}Şöyle yaparız.
List<Map<String, String>> map = ...;
String sql = " insert into  your_table " + "(  aa,bb  )"
                    + "values " + "(  ?,? )";
BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
  @Override
  public void setValues(PreparedStatement ps, int i)
  throws SQLException {
    Map<String, String> bean = map.get(i);
    ps.setString(1, bean.get("aa"));
    ps.setString(2, bean.get("bb")); 
    //..
    //..
    }
  @Override
  public int getBatchSize() {
    return map.size();
  }
};
jdbcTemplate.batchUpdate(sql, bpss);catch (Exception e) {
  if (e.getCause() instanceof BatchUpdateException) {
    BatchUpdateException be = (BatchUpdateException) e.getCause();
    int[] batchRes = be.getUpdateCounts();
    if (batchRes != null && batchRes.length > 0) {
      for (int index = 0; index < batchRes.length; index++) {
        if (batchRes[index] == Statement.EXECUTE_FAILED) {
          ...
        }
      }
    }
  }  
}execute metodu yazısına taşıdım
query() metodu yazısına taşıdım.
queryForInt metodu
queryForInt(), queryForLong(), queryForObject() gibi metodlar tek bir satır döndürürler. Eğer birden fazla veya boş satır gelirse, IncorrectResultSizeDataAccessException veya bundan türeyen EmptyResultDataAccessException exceptionları atılır.
queryForObject metodu
queryForInt(), queryForLong(), queryForObject() gibi metodlar tek bir satır döndürürler. Eğer birden fazla veya boş satır gelirse, IncorrectResultSizeDataAccessException veya bundan türeyen EmptyResultDataAccessException exceptionları atılır.
queryForObject metodu
queryForObject metodu yazısına taşıdım.
queryForList metodu
Birden fazla sonuç nesnesi dönülecekse kullanılır. List <Map <String,Object>> nesnesi döndürür. Şöyle yaparız
Şöyle yaparız.
update metodu  - sql + args
update metodu yazısına taşıdım
Birden fazla sonuç nesnesi dönülecekse kullanılır. List <Map <String,Object>> nesnesi döndürür. Şöyle yaparız
String queryString = "SELECT * FROM userInformation";
List<Map<String, Object>> listOfUsers = jdbcTemplate.queryForList(queryString);Şöyle yaparız.
String number = ...
List<Map<String, Object>> result =
  jdbcTemplate.queryForList(sql, new Object[]{number, number});queryForStream metodu
queryForStream metodu yazısın taşıdım
setMaxRows metodu - int
JdbcTemplate sınıfının setMaxRows(int maxRows) metod kullanılırsa SQL içindeki LIMIT/TOP seçenekleri  gibi çalışır.
update metodu yazısına taşıdım
update metodu  - sql + PreparedStatementCreator
update metodu yazısına taşıdım
 
Hiç yorum yok:
Yorum Gönder