在演练过程中,我添加了指定的模型,控制器和repo。我通过spring Boot 添加了所有必需的依赖项(我相信),并通过MySql shell创建了DB。
数据库是相同的例子,以及基本的根用户,直到我可以得到这个工作,然后将添加另一个用户。
任何帮助将不胜感激,因为我有这个同样的问题,每次我试图建立一个数据库。
我的模型
package com.avorion.dndwiki.Model;
import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id;
@Entity // This tells Hibernate to make a table out of this class public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO) private Integer id;
private String name;
private String email;
public Integer getId() { return id;
}
public void setId(Integer id) { this.id = id;
}
public String getName() { return name;
}
public void setName(String name) { this.name = name;
}
public String getEmail() { return email;
}
public void setEmail(String email) { this.email = email;
} }
字符串
我的控制器
package com.avorion.dndwiki.Controller;
import com.avorion.dndwiki.Model.User; import com.avorion.dndwiki.Repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody;
@Controller // This means that this class is a Controller @RequestMapping(path="/demo") // This means URL's start with /demo (after Application path) public class MainController {
@Autowired // This means to get the bean called userRepository Which is auto-generated by Spring, we will use it to handle the data private UserRepository userRepository;
@PostMapping(path="/add") // Map ONLY POST Requests public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User(); n.setName(name); n.setEmail(email); userRepository.save(n); return "Saved";
}
@GetMapping(path="/all") public @ResponseBody Iterable<User> getAllUsers() { This returns a JSON or XML with the users return userRepository.findAll();
} }
型
我的回购
package com.avorion.dndwiki.Repository;
import com.avorion.dndwiki.Model.User; import org.springframework.data.repository.CrudRepository;
//This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Integer> {
}
型
我的应用属性
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=root@localhost
spring.datasource.password=thisIsntMyActualPassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
型
我的Pom(这里有很多,我只是在练习)
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Avorion</groupId>
<artifactId>DnDWiki</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DnDWiki</name>
<description>DnDWiki</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
型
我试着用不同的用户和命名约定设置了一些数据库。我已经重新设置了MySql以及询问其他软件工程师。
1条答案
按热度按时间fjnneemd1#
请确保您的密码和用户名正确。我不确定您是否需要在用户名中使用@localhost