Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mybatis: how to check if a row exists and return a boolean?

Tags:

mysql

mybatis

The database is MySQL. The code should look like as below:

public interface UserMapper {
    @Select("....")
    boolean checkUserExists(@Param("email") String email);
}

Is it possible, and how to write the SELECT SQL? (can Exists be used?)

like image 252
Zach Avatar asked Nov 28 '22 23:11

Zach


1 Answers

Yes, you can use EXISTS

@Select("SELECT EXISTS(SELECT 1 FROM my_table WHERE email=#{email})")
boolean checkUserExists(@Param("email") String email);

Note that "SELECT 1" is there for a reason, you don't need to select any actual columns

like image 78
Bostone Avatar answered Dec 16 '22 01:12

Bostone