MySQL即使存在用户,也不会在数据库中显示他们。

huangapple go评论59阅读模式
英文:

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:\&gt;curl -G localhost:8080/demo/first -d name=Biagio
{&quot;name&quot;:&quot;Biagio&quot;,&quot;timestamp&quot;:&quot;2020-10-05T08:54:18.623+00:00&quot;,&quot;email&quot;:&quot;biagio@gmal.com&quot;,&quot;surname&quot;:&quot;Biagio2&quot;}

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(&quot;/demo&quot;)
public class MainController {

  @Autowired
  private UserService userService;

  @Transactional
  //@RequestMapping(value = &quot;/add/&quot;, method = RequestMethod.POST)
  @PostMapping(path = &quot;/demo/add&quot;)
  public String addNewUser(@PathVariable(&quot;name&quot;) String name, @PathVariable(&quot;email&quot;) String email, @PathVariable(&quot;surname&quot;) String surname) {

    UserDto n = new UserDto();
    n.setName(name);
    n.setSurname(surname);
    n.setEmail(email);
    userService.create(n);
    return &quot;User Saved in DB&quot;;
  }

  @SuppressWarnings({
    &quot;rawtypes&quot;,
    &quot;unchecked&quot;
  })
  //@RequestMapping(value = &quot;/fetchUser/{name}&quot;, method = RequestMethod.GET)
  @GetMapping(&quot;/demo/first&quot;)
  public ResponseEntity &lt; UserDto &gt; fetchUser(@PathVariable(&quot;name&quot;) String name) {
    System.out.println(name);

    try {
      UserDto namefound = userService.findFirstByName(name);
      System.out.println(&quot;Name found&quot;);
      ResponseEntity &lt; UserDto &gt; user = new ResponseEntity &lt; UserDto &gt; (namefound, HttpStatus.OK);
      return user;
    } catch(NoResultException ne) {
      System.out.println(&quot;User not found&quot;);
      return new ResponseEntity(&quot;User not found with name : &quot; + name, HttpStatus.NOT_FOUND);
    }

  }
}

答案1

得分: 1

  1. 添加用户到数据库的操作<br/>
    在 PostMapping 中修改您的路径

    @PostMapping(path = &quot;/demo/add&quot;)
    修改为 @PostMapping(path = &quot;/demo/add/{name}/{email}/{surname}&quot;)

    这是因为在您的 addNewUser 方法中,您接受了名字、邮箱、姓作为 PathVariable。<br/>
    为了更清楚地解释 @PathVariable - 它是一个注解,指示哪个方法参数应与哪个 URI 模板变量匹配。

    在您的情况下,例如,<br/>
    {name} - 是 URI 模板变量
    addNewUser 方法中的 String name - 是方法参数(这是您可以在方法的其余部分中使用 {name} 值的别名)<br/>

    您必须像这样访问 API,

     localhost:8080/demo/add/Biagio/biagio@gmail.com/Vaso
    
  2. 通过名字获取用户 <br/>
    在 GetMapping 中修改路径<br/>
    类似上述解释,

    @GetMapping(&quot;/demo/first&quot;)
    修改为 @GetMapping(&quot;/demo/first/{name}&quot;)

    您必须像这样访问 API,

     localhost:8080/demo/first/Biagio
    
英文:
  1. For adding user to the database<br/>
    Modify your path in PostMapping

    From @PostMapping(path = &quot;/demo/add&quot;)
    to @PostMapping(path = &quot;/demo/add/{name}/{email}/{surname}&quot;)

    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
    
  2. To get user by name <br/>
    Modify path in GetMapping<br/>
    Similar to above explanation,

    From @GetMapping(&quot;/demo/first&quot;)
    to @GetMapping(&quot;/demo/first/{name}&quot;)

    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.

huangapple
  • 本文由 发表于 2020年10月7日 18:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定