Steps to develop spring JDBC application by using NamedParameterJdbcTemplate
1) create new maven project.
2) add required dependencies in POM.xml file
Spring JDBC dependency, oracle/sql driver etc.!
3) create beans configured Java/xml file.
4) write business logic inside src/main/java
Spring JDBC provides multiple templates to interact with Databases i.e., JdbcTemplate, NamedParameterJdbcTemplate, SimpleJdbcInsert, SimpleJdbcCall
based on requirement we can use any above templates.
Java model class
package com.smarttechguides.bank; | |
public class HdfcBank { | |
private int accountNumber; | |
private String customerName; | |
private int depositeAmount; | |
private double withdrawAmount; | |
private double totalAmount; | |
public int getAccountNumber() { | |
return accountNumber; | |
} | |
public void setAccountNumber(int accountNumber) { | |
this.accountNumber = accountNumber; | |
} | |
public String getCustomerName() { | |
return customerName; | |
} | |
public void setCustomerName(String customerName) { | |
this.customerName = customerName; | |
} | |
public int getDepositeAmount() { | |
return depositeAmount; | |
} | |
public void setDepositeAmount(int depositeAmount) { | |
this.depositeAmount = depositeAmount; | |
} | |
public double getWithdrawAmount() { | |
return withdrawAmount; | |
} | |
public void setWithdrawAmount(double withdrawAmount) { | |
this.withdrawAmount = withdrawAmount; | |
} | |
public double getTotalAmount() { | |
return totalAmount; | |
} | |
public void setTotalAmount(double totalAmount) { | |
this.totalAmount = totalAmount; | |
} | |
@Override | |
public String toString() { | |
return "HdfcBank [accountNumber=" + accountNumber + ", customerName=" + customerName + ", depositeAmount=" | |
+ depositeAmount + ", withdrawAmount=" + withdrawAmount + ", totalAmount=" + totalAmount + "]"; | |
} | |
} |
Spring Dao Class
package com.smarttechguides.trnximpl; | |
import com.smarttechguides.bank.HdfcBank; | |
public interface BankTransactions { | |
public int insertUser(HdfcBank hdfcUser); | |
} | |
Dao Class Implementation
package com.smarttechguides.trnximpl; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | |
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | |
import org.springframework.stereotype.Component; | |
import com.smarttechguides.bank.HdfcBank; | |
@Component | |
public class BankTrxnImplements implements BankTransactions { | |
@Autowired | |
NamedParameterJdbcTemplate namedJdbcTemplate; | |
public int insertUser(HdfcBank hdfcUser) { | |
MapSqlParameterSource map = new MapSqlParameterSource(); | |
map.addValue("acNumber", hdfcUser.getAccountNumber()); | |
map.addValue("customer", hdfcUser.getCustomerName()); | |
map.addValue("withdraw", hdfcUser.getWithdrawAmount()); | |
String query = "insert into bank(accnumber,customer,withdraw) values(:acNumber,:customer,:withdraw)"; | |
int update = namedJdbcTemplate.update(query, map); | |
return update; | |
} | |
} |
Java based configuration file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.smarttechguides.configurations;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
@ComponentScan(basePackages = "com.smarttechguides.trnximpl")
public class DatabaseConfig {
@Bean(name = "dataSource")
public DataSource dataSource() {
DriverManagerDataSource dds = new DriverManagerDataSource();
dds.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dds.setUsername("system");
dds.setPassword("manager");
return dds;
}
@Bean(name = "template")
public NamedParameterJdbcTemplate jdbcTemplate() {
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource());
return template;
}
}
package com.smarttechguides.configurations; | |
import javax.sql.DataSource; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | |
import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
@Configuration | |
@ComponentScan(basePackages = "com.smarttechguides.trnximpl") | |
public class DatabaseConfig { | |
@Bean(name = "dataSource") | |
public DataSource dataSource() { | |
DriverManagerDataSource dds = new DriverManagerDataSource(); | |
dds.setUrl("jdbc:oracle:thin:@localhost:1521:orcl"); | |
dds.setUsername("system"); | |
dds.setPassword("manager"); | |
return dds; | |
} | |
@Bean(name = "template") | |
public NamedParameterJdbcTemplate jdbcTemplate() { | |
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource()); | |
return template; | |
} | |
} |
Java Main class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.smarttechguides;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.smarttechguides.bank.HdfcBank;
import com.smarttechguides.configurations.DatabaseConfig;
import com.smarttechguides.trnximpl.BankTrxnImplements;
public class BankApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(DatabaseConfig.class);
BankTrxnImplements bean = context.getBean(BankTrxnImplements.class);
HdfcBank hdfcUser = new HdfcBank();
hdfcUser.setAccountNumber(101);
hdfcUser.setCustomerName("ravan");
hdfcUser.setDepositeAmount(150);
bean.insertUser(hdfcUser);
}
}
package com.smarttechguides; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import com.smarttechguides.bank.HdfcBank; | |
import com.smarttechguides.configurations.DatabaseConfig; | |
import com.smarttechguides.trnximpl.BankTrxnImplements; | |
public class BankApplication { | |
public static void main(String[] args) { | |
ApplicationContext context = new AnnotationConfigApplicationContext(DatabaseConfig.class); | |
BankTrxnImplements bean = context.getBean(BankTrxnImplements.class); | |
HdfcBank hdfcUser = new HdfcBank(); | |
hdfcUser.setAccountNumber(101); | |
hdfcUser.setCustomerName("ravan"); | |
hdfcUser.setDepositeAmount(150); | |
bean.insertUser(hdfcUser); | |
} | |
} |
pom.xml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smarttechguides</groupId>
<artifactId>02-BankApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.smarttechguides</groupId> | |
<artifactId>02-BankApplication</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jdbc</artifactId> | |
<version>5.3.13</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>5.3.14</version> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle.database.jdbc</groupId> | |
<artifactId>ojdbc6</artifactId> | |
<version>11.2.0.4</version> | |
</dependency> | |
</dependencies> | |
</project> |
Project Folder Structure
Note: spring beans we can configure in 3 ways
1) xml based configuration file
Configure <beans> inside beans.xml file
2) annotation-based configuration
Configure <context:annotation-config/> inside beans.xml file and use annotations inside Java class.
3) Java based configuration
Configurations happen in Java file without xml file.
With the help of annotations like @configuration, @component, @service, @controller. etc.!
No comments:
Post a Comment