Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 统计表中的行总数_Java_Mysql_Spring_Mybatis - Fatal编程技术网

Java 统计表中的行总数

Java 统计表中的行总数,java,mysql,spring,mybatis,Java,Mysql,Spring,Mybatis,我试图使用Spring、Mybatis和Thymeleaf框架计算Mysql表中的所有行数。我有这样一份数据表: 表:用户 id name 1 John 2 Jack 3 William 4 Harry 我想计算用户表中的所有行数或获取表中的最大值(这里是4),我在服务类中使用select语句: UserService.java @Select ("Select count (id) from user) List <User> countById (

我试图使用Spring、Mybatis和Thymeleaf框架计算Mysql表中的所有行数。我有这样一份数据表: 表:用户

  id name
  1 John
  2 Jack
  3 William
  4 Harry
我想计算用户表中的所有行数或获取表中的最大值(这里是4),我在服务类中使用select语句: UserService.java

@Select ("Select count (id) from user)
List <User> countById ();
并在HTML之外显示它 user.html

<div>
<span th: text = "$ {user.id}> </span>
</div>


@Select annotation应更改为@Query annotation,并且方法的返回类型应为long

这应该起作用:

@Query("SELECT count(u.id) FROM user u)
long countById ();
在view part中,您可以按如下方式输出它:

<div>
<span th: text = "${user}> </span>
</div>


@选择change to
@Query
,你可以从这个博客上阅读更多关于编写查询的内容:OP询问的是MyBatis,而不是JPA。因此注释应保持
@选择
。除此之外,其余的答案都是正确的。
<div>
<span th: text = "${user}> </span>
</div>