I am very new to Spring Boot and I had problems trying to configure my project so that it could communicate with SQL Server - more precisely, my JDBCTemplate instance variable is null and for some reason is not being "autwired" with the data source specified in application.properties file. These are the steps that I have done so far:
- Using STS, I created a new Spring boot project using the Spring Start Project template.
- I chose Gradle for Type and tagged JDBC.
- Then I completed the following Abstract Interface (DAO) tutorial for SQL Server ( http://www.tutorialspoint.com/spring/spring_jdbc_example.htm ).
- If you scroll down the tutorial page to the MainApp.java bit, the first 4 lines of the main method, which I did not use, because I do not have a beans.xml file. Here I assume that Spring Boot @Autowired annotation comes in and creates my beans for me?
- I downloaded the SQL Server jar file from Microsoft and added it to my project as an external JAR by right-clicking on my project → Build path → Configure build path → Add external JAR .. ". This removed the error that indicated that the directory_name_name specified in application.properties was not found.
I will start by displaying the contents of my file "application.properties":
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testdb
spring.datasource.username=sa
spring.datasource.password=myPassword
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerConnection
spring.datasource.initialize=true
Below is my JDBCTemplate.java class which contains my CRUD methods:
package demo;
import java.util.List;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class BranchJDBCTemplate implements BranchDAO {
private DataSource dataSource;
@Autowired
protected JdbcTemplate jdbcTemplateObject;
@Autowired
@Override
public void setDataSource(DataSource ds) {
this.dataSource = ds;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
@Override
public void create(String name) {
String SQL = "insert into branches (name) values (?)";
jdbcTemplateObject.update(SQL, name);
System.out.println("Created Record Name = " + name);
return;
}
@Override
public Branch getBranch(Integer id) {
String SQL = "select * from branches where id = ?";
Branch student = jdbcTemplateObject.queryForObject(SQL,
new Object[]{id}, new BranchMapper());
return student;
}
@Override
public List<Branch> listBranches() {
String SQL = "select * from branches";
List <Branch> branches = jdbcTemplateObject.query(SQL, new BranchMapper());
return branches;
}
@Override
public void delete(Integer id) {
String SQL = "delete from branches where id = ?";
jdbcTemplateObject.update(SQL, id);
System.out.println("Deleted Record with ID = " + id );
return;
}
@Override
public void update(Integer id, String name) {
String SQL = "update Student set name = ? where id = ?";
jdbcTemplateObject.update(SQL, id);
System.out.println("Updated Record with ID = " + id );
return;
}
}
, CustController.java, , JDBCTemplate :
package demo;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustController {
@RequestMapping("/customer")
public Cust customer(@RequestParam(value="name", required=false, defaultValue="World") String name) {
BranchJDBCTemplate branchTemplate = new BranchJDBCTemplate();
List<Branch> branchesList = branchTemplate.listBranches();
for (Branch branch : branchesList) {
System.out.print("ID : " + branch.getId());
}
return new Cust(12, "Test", "Test");
}
}
, , , , jdbcTemplateObject...
protected JdbcTemplate jdbcTemplateObject;
null , , :
List <Branch> branches = jdbcTemplateObject.query(SQL, new BranchMapper());
, - , ?
!