英文:
MySQL does not show the Users inside the database even if they are present
问题
我在使用Spring Boot和MySql进行一个小项目时遇到了一个奇怪的问题。当我尝试将新用户添加到数据库时,它显示成功消息,但是当我在mysql shell中查看时,即使存在(因为使用Get实际上可以找到插入的用户),在数据库中什么都没有。现在我不明白问题是什么。我附上了我Spring项目的applications.properties
文件。
每次我发出Get请求时,都会返回以下消息:
D:\>curl -G localhost:8080/demo/first -d name=Biagio
{"name":"Biagio","timestamp":"2020-10-05T08:54:18.623+00:00","email":"biagio@gmal.com","surname":"Biagio2"}
我的数据库:
+----+------------------------+----------+----------------------------+---------+
| id | email | name | stmp | surname |
+----+------------------------+----------+----------------------------+---------+
| 32 | mirketto90@yahoo.it | Mirko | 2020-10-01 12:31:47.827000 | NULL |
| 36 | biagio@gmail.com | Biagio | 2020-10-01 16:31:31.687000 | Vaso |
| 37 | biagio@gmail.com | Biagio | 2020-10-01 16:31:50.077000 | Vaso |
| 38 | biagio@gmail.com | Biagio | 2020-10-01 18:35:45.992000 | Vaso |
| 39 | | Mirko | 2020-10-02 13:24:05.840000 | Vaso |
| 40 | mirkoparioli@gmail.com | Mirko | 2020-10-02 13:24:23.383000 | Vaso |
| 47 | giovanni@gmal.com | Giovanni | 2020-10-05 12:11:05.997000 | John |
+----+------------------------+----------+----------------------------+---------+
我确保我正在使用正确的数据库(db_example),这是我的应用程序入口类:
@SpringBootApplication
public class AccessingDataMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataMysqlApplication.class, args);
}
}
这是我的主控制器类:
@RestController
@RequestMapping("/demo")
public class MainController {
@Autowired
private UserService userService;
@Transactional
@PostMapping(path = "/demo/add")
public String addNewUser(@PathVariable("name") String name, @PathVariable("email") String email, @PathVariable("surname") String surname) {
UserDto n = new UserDto();
n.setName(name);
n.setSurname(surname);
n.setEmail(email);
userService.create(n);
return "User Saved in DB";
}
@SuppressWarnings({
"rawtypes",
"unchecked"
})
@GetMapping("/demo/first")
public ResponseEntity < UserDto > fetchUser(@PathVariable("name") String name) {
System.out.println(name);
try {
UserDto namefound = userService.findFirstByName(name);
System.out.println("Name found");
ResponseEntity < UserDto > user = new ResponseEntity < UserDto > (namefound, HttpStatus.OK);
return user;
} catch(NoResultException ne) {
System.out.println("User not found");
return new ResponseEntity("User not found with name : " + name, HttpStatus.NOT_FOUND);
}
}
}
英文:
I have a strange problem with MySql. I did a small project with Spring-Boot MySql but when I try to add a new User to the database it gives me the success message but then going to see on mysql shell nothing appears in the database even if there is (because with the Get it actually finds the inserted user). Now I don't understand what the problem is. I attach the applications. properties of my Spring project.
every time I ask for Get it returns this MESSAGE:
D:\>curl -G localhost:8080/demo/first -d name=Biagio
{"name":"Biagio","timestamp":"2020-10-05T08:54:18.623+00:00","email":"biagio@gmal.com","surname":"Biagio2"}
My Database:
+----+------------------------+----------+----------------------------+---------+
| id | email | name | stmp | surname |
+----+------------------------+----------+----------------------------+---------+
| 32 | mirketto90@yahoo.it | Mirko | 2020-10-01 12:31:47.827000 | NULL |
| 36 | biagio@gmail.com | Biagio | 2020-10-01 16:31:31.687000 | Vaso |
| 37 | biagio@gmail.com | Biagio | 2020-10-01 16:31:50.077000 | Vaso |
| 38 | biagio@gmail.com | Biagio | 2020-10-01 18:35:45.992000 | Vaso |
| 39 | | Mirko | 2020-10-02 13:24:05.840000 | Vaso |
| 40 | mirkoparioli@gmail.com | Mirko | 2020-10-02 13:24:23.383000 | Vaso |
| 47 | giovanni@gmal.com | Giovanni | 2020-10-05 12:11:05.997000 | John |
+----+------------------------+----------+----------------------------+---------+
I'm sure I'm using the right database (db_example), this is my
@SpringBootApplication
public class AccessingDataMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataMysqlApplication.class, args);
}
}
@RestController@RequestMapping("/demo")
public class MainController {
@Autowired
private UserService userService;
@Transactional
//@RequestMapping(value = "/add/", method = RequestMethod.POST)
@PostMapping(path = "/demo/add")
public String addNewUser(@PathVariable("name") String name, @PathVariable("email") String email, @PathVariable("surname") String surname) {
UserDto n = new UserDto();
n.setName(name);
n.setSurname(surname);
n.setEmail(email);
userService.create(n);
return "User Saved in DB";
}
@SuppressWarnings({
"rawtypes",
"unchecked"
})
//@RequestMapping(value = "/fetchUser/{name}", method = RequestMethod.GET)
@GetMapping("/demo/first")
public ResponseEntity < UserDto > fetchUser(@PathVariable("name") String name) {
System.out.println(name);
try {
UserDto namefound = userService.findFirstByName(name);
System.out.println("Name found");
ResponseEntity < UserDto > user = new ResponseEntity < UserDto > (namefound, HttpStatus.OK);
return user;
} catch(NoResultException ne) {
System.out.println("User not found");
return new ResponseEntity("User not found with name : " + name, HttpStatus.NOT_FOUND);
}
}
}
答案1
得分: 1
-
添加用户到数据库的操作<br/>
在 PostMapping 中修改您的路径从
@PostMapping(path = "/demo/add")
修改为@PostMapping(path = "/demo/add/{name}/{email}/{surname}")
这是因为在您的 addNewUser 方法中,您接受了名字、邮箱、姓作为 PathVariable。<br/>
为了更清楚地解释 @PathVariable - 它是一个注解,指示哪个方法参数应与哪个 URI 模板变量匹配。在您的情况下,例如,<br/>
{name} - 是 URI 模板变量
addNewUser 方法中的 String name - 是方法参数(这是您可以在方法的其余部分中使用 {name} 值的别名)<br/>您必须像这样访问 API,
localhost:8080/demo/add/Biagio/biagio@gmail.com/Vaso
-
通过名字获取用户 <br/>
在 GetMapping 中修改路径<br/>
类似上述解释,从
@GetMapping("/demo/first")
修改为@GetMapping("/demo/first/{name}")
您必须像这样访问 API,
localhost:8080/demo/first/Biagio
英文:
-
For adding user to the database<br/>
Modify your path in PostMappingFrom
@PostMapping(path = "/demo/add")
to@PostMapping(path = "/demo/add/{name}/{email}/{surname}")
this is because in your addNewUser method you accept the name,email,surname as PathVariable.<br/>
To bring a small clarity about the @PathVariable - it is a annotation that indicates which method parameter should be matched which URI template variable.In your case for example,<br/>
{name} - Is the URI template variable
String name in addNewUser method - Is the method parameter(which the alias name that you can use the {name} value in rest of the method)<br/>You must access the api like,
localhost:8080/demo/add/Biagio/biagio@gmail.com/Vaso
-
To get user by name <br/>
Modify path in GetMapping<br/>
Similar to above explanation,From
@GetMapping("/demo/first")
to@GetMapping("/demo/first/{name}")
You must access the api like,
localhost:8080/demo/first/Biagio
答案2
得分: 1
我自己解决了这个问题。当在UserServiceImpl上使用@Autowired时,有太多的“private”。该项目公平且连接良好。有任何事情不要犹豫写给我。
英文:
I solved the problem myself. There was simply too much "private" when @Autowired on UserServiceImpl.The project is fair and well connected. For anything do not hesitate to write me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论