【问题标题】:how can I connect to my database with spring and test it?如何使用 spring 连接到我的数据库并进行测试?
【发布时间】:2015-12-01 19:51:42
【问题描述】:

我正在尝试为我的 spring 项目创建一个连接,这很简单。

我想在我的 application.properties 上创建配置字段。我尝试使用 spring.datasource 并没有收到任何错误,但我仍然没有收到任何信息,只是空指针......我认为连接没有正确建立。

这是我的 pom 的一部分,具有以下依赖项:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>

还有我的属性:

server.port=80
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.username=user
jdbc.password=password

我没有收到任何错误,但是当我查询数据库时,我总是得到一个空指针,这让我觉得工作不正常,知道如何配置这个吗?

谢谢

编辑:

我的配置类

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

    }

}

控制器

@Autowired
private MyService myService;


@RequestMapping("/")
public String index() {
    User myUser = myService.findUserById(1L);
    System.out.println(myUser);
    return myUser.getFirstName();
}

我的服务实现

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    UserRepository userRepository;


    public User findUserById(Long id){
        return userRepository.findOne(id);
    };
}

我的实体

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

还有我的仓库

public interface UserRepository extends CrudRepository<User, Long> {

    //User findOne(Long id); this is a default method
}

错误:

我总是得到空指针,即使数据存在......如果我将数据库密码更改为错误的密码,我不会收到任何错误......就像它会没问题一样。

【问题讨论】:

  • 提交spring xml或者spring注解sn -p
  • 配置类?
  • 如何注入 bean?
  • 哪个豆子?调用数据库?还是配置的bean?
  • 如何在应用程序中使用 JPA bean?您能否提供链配置 [ok 提供] -> 应用程序 [ok 提供] -> 服务并将 JPA bean 注入服务 -> 使用 JPA 连接器的方法?您可以发布堆栈跟踪异常吗?

标签: java spring spring-mvc jdbc


【解决方案1】:

1) 请从 pom 中删除以下依赖,因为它可能让 SpringBoot 使用 h2 而不是 mysql,这导致即使输入错误的 mysql 密码也看不到任何错误。

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

2) 在application.properties中,请使用:

spring.datasource.url=jdbc:mysql://localhost:3306/database
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

这些属性名可以从SpringBoot文档中找到:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html:

【讨论】:

    【解决方案2】:

    正确的数据源url是:

    jdbc:mysql://HostName:Port(3306 is the default)/DBName
    

    【讨论】:

    • 它不再抛出错误,但我仍然得到 null ,即使我将用户更改为错误的用户,似乎我没有收到其他类型的警告,知道吗?
    【解决方案3】:

    您的存储库中缺少@Repository 注释。添加它,看看它是否适合你。

    如果没有,
    1. 分享您获得的完整堆栈跟踪。
    2. 另外,你能到达控制器端点吗?

    【讨论】:

    • 需要测试的几件事: - 在您的服务类中设置一个断点,看看它是否到达那里。然后清除并检查控制台是否实际触发了查询。如果是这样,该查询是什么? - 检查用户表中是否有一个 id = 1 的条目。 - 在控制器中,如果返回的用户对象为空,那么你应该得到一个空指针异常。
    【解决方案4】:

    我注意到您的实体没有实现 Serializable 接口。

    如果这不是问题,请查看 github 存储库中的 spring boot 示例以供参考。

    https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa/

    【讨论】:

      猜你喜欢
      • 2017-07-11
      • 2016-06-12
      • 1970-01-01
      • 2016-01-21
      • 2015-02-19
      • 2011-05-20
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      相关资源
      最近更新 更多