-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v1ll4n
committed
Jul 2, 2024
1 parent
2eb0690
commit 88aad66
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
java-mybatis-plus-mapper/sample/UserMapperWithAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import org.apache.ibatis.annotations.*; | ||
import java.util.List; | ||
|
||
public interface UserMapper extends BaseMapper<User> { | ||
@Select("SELECT * FROM users WHERE age = #{age} AND name = #{name} AND email = #{email}") | ||
List<User> selectUsersByMultipleFields(int age, String name, String email); | ||
|
||
@Select("SELECT * FROM ${tableName} WHERE age = #{age}") | ||
List<User> selectUsersByTableName(String tableName, int age); | ||
|
||
@Delete("DELETE FROM users WHERE id = #{id}") | ||
int deleteUserById(Long id); | ||
|
||
@Update("UPDATE users SET email = #{email} WHERE id = #{id}") | ||
int updateUserEmailById(Long id, String email); | ||
|
||
@Insert("INSERT INTO users (name, age, email) VALUES (#{name}, #{age}, #{email})") | ||
int insertUser(String name, int age, String email); | ||
|
||
@Select("SELECT * FROM users WHERE email LIKE CONCAT('%', #{email}, '%')") | ||
List<User> findUsersByEmail(String email); | ||
|
||
// 动态 SQL 使用例子 | ||
@Select("<script>" + | ||
"SELECT * FROM users " + | ||
"<where> " + | ||
" <if test='name != null'> AND name = #{name} </if>" + | ||
" <if test='email != null'> AND email = #{email} </if>" + | ||
"</where>" + | ||
"</script>") | ||
List<User> findUsersByOptionalCriteria(@Param("name") String name, @Param("email") String email); | ||
|
||
// 批量删除 | ||
@Delete("<script>" + | ||
"DELETE FROM users WHERE id IN " + | ||
"<foreach item='id' collection='ids' open='(' separator=',' close=')'>" + | ||
" #{id}" + | ||
"</foreach>" + | ||
"</script>") | ||
int deleteUsersByIds(@Param("ids") List<Long> ids); | ||
|
||
// 更新多个字段 | ||
@Update("UPDATE users SET age = #{age}, email = #{email} WHERE id = #{id}") | ||
int updateUserById(Long id, int age, String email); | ||
} |