Java 如何使用 spring 连接到我的数据库并对其进行测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32418646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I connect to my database with spring and test it?
提问by jpganz18
I am trying to create a connection for my spring project, soemthing very simple.
我正在尝试为我的 spring 项目创建一个连接,这很简单。
I want to create the configuration fields on my application.properties. I tried with the spring.datasource
and I dont get any error, but still, I dont get any info, just null pointers... I think the connections have not been made properly.
我想在我的 application.properties 上创建配置字段。我试过,spring.datasource
我没有得到任何错误,但仍然没有得到任何信息,只是空指针......我认为连接没有正确建立。
Here is part of my pom, with this dependencies:
这是我的 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>
And my properties:
还有我的属性:
server.port=80
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.username=user
jdbc.password=password
I dont get any error, but when I query the database, I always get a null pointer, which makes me think is not working properly, any idea how to configure this?
我没有收到任何错误,但是当我查询数据库时,我总是得到一个空指针,这让我觉得工作不正常,知道如何配置吗?
Thanks
谢谢
EDIT:
编辑:
My configuration class
我的配置类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
The controller
控制器
@Autowired
private MyService myService;
@RequestMapping("/")
public String index() {
User myUser = myService.findUserById(1L);
System.out.println(myUser);
return myUser.getFirstName();
}
My Service Implementation
我的服务实施
@Service
public class MyServiceImpl implements MyService {
@Autowired
UserRepository userRepository;
public User findUserById(Long id){
return userRepository.findOne(id);
};
}
My Entity
我的实体
@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;
}
}
And my repository
还有我的仓库
public interface UserRepository extends CrudRepository<User, Long> {
//User findOne(Long id); this is a default method
}
THE ERROR:
错误:
I always get null pointer, even if data exist... and if I change the database password for a wrong one, I dont get any error.. like if it would be ok.
我总是得到空指针,即使数据存在......如果我更改了错误的数据库密码,我不会收到任何错误......就像它会没事一样。
采纳答案by JM Yang
1) Please remove below dependency from pom, because of it might let SpringBoot use h2 instead of mysql, which cause you don't see any error even with wrong mysql password.
1) 请从 pom 中删除以下依赖项,因为它可能让 SpringBoot 使用 h2 而不是 mysql,这导致即使 mysql 密码错误,您也看不到任何错误。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
2) In application.properties, please use:
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
These property names can be found from SpringBoot document: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html:
这些属性名称可以从 SpringBoot 文档中找到:http: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html :
回答by SwEngin
The correct data source url is:
正确的数据源网址是:
jdbc:mysql://HostName:Port(3306 is the default)/DBName
回答by Vipul Agarwal
You are missing the @Repository
annotation on your Repository. Add that and see if that works for you.
您缺少@Repository
存储库上的注释。添加它,看看它是否适合你。
If not,
1. share the complete stack trace that you are getting.
2. Also, are you able to reach to the controller endpoint?
如果没有,
1. 分享您获得的完整堆栈跟踪。
2. 另外,你能到达控制器端点吗?
回答by Rohan Surdikar
I noticed your entity does not implements Serializable interface.
我注意到您的实体没有实现 Serializable 接口。
If this is not the issue please take a look at the spring boot example from there github repository for reference.
如果这不是问题,请查看 github 存储库中的 spring boot 示例以供参考。