将Spring Boot JDBCTemplate连接到SQL Server(MSSQL)

ton*_*ony 5 java spring spring-mvc spring-boot

我是Spring Boot的新手,我在尝试设置我的项目时遇到了麻烦,因此它可以与SQL Server进行通信 - 更具体地说,我的JDBCTemplate实例变量为null,并且由于某种原因没有'自动装配'我在application.properties文件中指定的数据源.这些是我到目前为止采取的步骤:

  1. 使用STS我使用Spring Start Project模板创建了一个新的Spring Boot项目.
  2. 我通过'Type'选择了Gradle,并勾选了JDBC.
  3. 然后,我按照以下教程创建了一个到SQL Server的抽象接口(DAO)(http://www.tutorialspoint.com/spring/spring_jdbc_example.htm).
  4. 如果您将教程页面向下滚动到MainApp.java位,我没有使用主方法的前4行 - 因为我没有beans.xml文件.这是我假设Spring Boot的@Autowired注释进来并为我创建我的bean的地方?
  5. 我从Microsoft下载了SQL Server jar文件,并通过右键单击我的项目 - >构建路径 - >配置构建路径 - >添加外部JAR来将其作为外部JAR添加到我的项目中.这样做删除了我所遇到的错误,表明我在application.properties文件中指定的driverClassName无法找到.

我将首先显示我的'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
Run Code Online (Sandbox Code Playgroud)

下面是我的'JDBCTemplate.java'类,其中包含我的CRUD方法:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

最后,这是我的'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");

    }

}
Run Code Online (Sandbox Code Playgroud)

我之前提到的问题是我的jdbcTemplateObject实例......

protected JdbcTemplate jdbcTemplateObject;
Run Code Online (Sandbox Code Playgroud)

为null,因此在以下行中抛出异常:

List <Branch> branches = jdbcTemplateObject.query(SQL, new BranchMapper());
Run Code Online (Sandbox Code Playgroud)

它没有自动初始化,任何人都可以指出我做错了什么?

非常感谢!

托尼

Pra*_*sad 7

你是对的,你需要在其中配置带有数据源的beans.xml.

在CustController类customer()方法中,您使用new运算符:

    BranchJDBCTemplate branchTemplate = new BranchJDBCTemplate();
Run Code Online (Sandbox Code Playgroud)

所以这个branchTemplate实例不是spring manged因此数据源不是自动装配的,因此导致jdbctemplate的null值.

而是使用annotatioan作为:

    @Repository("branchDao")
    public class BranchJDBCTemplate implements BranchDAO {
    ...
    }
Run Code Online (Sandbox Code Playgroud)

并在CustController中访问branchTemplate:

    @RestController
    public class CustController {
        @Autowired
        @Qualifier("branchDao")
        BranchJDBCTemplate branchTemplate;  
        ...
    }   
Run Code Online (Sandbox Code Playgroud)