Monday, September 18, 2017

Spring JDBC Example (XML and Annotation)

pom.xml


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringDemo1</groupId>
<artifactId>SpringDemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<!-- repository for oracle -->
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>

<dependencies>
<!-- oracle ojdbc dependency -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>

</dependencies>
</project>

Employee.java


/**
 * 
 */
package com.bebo.model;

/**
 * @author anthakur
 *
 */
public class Employee {
private int id;
private String name;
private int salary;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}


}

EmployeeService.java


/**
 * 
 */
package com.bebo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.bebo.dao.EmployeeDao;
import com.bebo.model.Employee;

/**
 * @author anthakur
 *
 */
@Service
public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;

public void inserEmployee(Employee employee){
employeeDao.insertEmployee(employee);
}

public String getEmployeeName(int id) {
return employeeDao.getEmployeeName(id);
   }
}


EmployeeDao.java


/**
 * 
 */
package com.bebo.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bebo.model.Employee;

/**
 * @author anthakur
 *
 */
@Repository
public class EmployeeDao {

@Autowired
private JdbcTemplate jdbcTemplate;

public void insertEmployee(Employee employee) {
jdbcTemplate.update("insert into employee(id,name,salary) values(?,?,?)", employee.getId(), employee.getName(),
employee.getSalary());
System.out.println("Successfully.....");
}

  public String getEmployeeName(int id) {
       String sql = "select name from employee where id = ?";
       String name = jdbcTemplate.queryForObject(sql,new Object[]{id},String.class);
       return name;
   }
  
}


AppConfig.java


/**
 * 
 */
package com.bebo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.bebo.dao.EmployeeDao;

/**
 * @author anthakur
 *
 */
@Configuration
@ComponentScan("com.bebo.*")
public class AppConfig {

@Bean
DriverManagerDataSource dataSource(){
DriverManagerDataSource dataSource =new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
dataSource.setUsername("ems");
dataSource.setPassword("ems");
return dataSource;
}
@Bean
JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(dataSource());
}
}

applicationContext.xml


<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 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.bebo.*"></context:component-scan>

<!-- <bean id="employee" class="com.bebo.Employee"> <property name="name" 
value="Anil Thakur"></property> <constructor-arg value="xyz" type="String"></constructor-arg> 
</bean> -->

<!-- <bean id="employeeDaoA" class="com.bebo.dao.EmployeeDao">
<property name="name" value="Anil Thakur from employeeDaoA"></property>
</bean>


<bean id="employeeDaoB" class="com.bebo.dao.EmployeeDao">
<property name="name" value="Anil Thakur from employeeDaoB"></property>
</bean> -->

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="ems" />
<property name="password" value="ems" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

MainClass.java


/**
 * 
 */
package com.bebo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import com.bebo.config.AppConfig;
import com.bebo.model.Employee;
import com.bebo.service.EmployeeService;

/**
 * @author anthakur
 *
 */
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {

/*
* Resource resource =new ClassPathResource("applicationContext.xml");
* BeanFactory beanFactory =new XmlBeanFactory(resource);
* Employee employee =(Employee) beanFactory.getBean("employee");
*/

/*ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");*/

AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

EmployeeService employeeService = (EmployeeService) applicationContext.getBean("employeeService");
Employee employee = new Employee();
employee.setId(2);
employee.setName("abc");
employee.setSalary(10000);
employeeService.inserEmployee(employee);
/* String name=employeeService.getEmployeeName(1);
System.out.println("Name="+name);*/

}

}


No comments:

Post a Comment