From 88aad66d94319a9263c020939748ccf00f6afbbc Mon Sep 17 00:00:00 2001 From: v1ll4n Date: Tue, 2 Jul 2024 16:29:10 +0800 Subject: [PATCH] add mybatis plus --- .../sample/UserMapperWithAnnotation.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 java-mybatis-plus-mapper/sample/UserMapperWithAnnotation.java diff --git a/java-mybatis-plus-mapper/sample/UserMapperWithAnnotation.java b/java-mybatis-plus-mapper/sample/UserMapperWithAnnotation.java new file mode 100644 index 0000000..36a1533 --- /dev/null +++ b/java-mybatis-plus-mapper/sample/UserMapperWithAnnotation.java @@ -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 { + @Select("SELECT * FROM users WHERE age = #{age} AND name = #{name} AND email = #{email}") + List selectUsersByMultipleFields(int age, String name, String email); + + @Select("SELECT * FROM ${tableName} WHERE age = #{age}") + List 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 findUsersByEmail(String email); + + // 动态 SQL 使用例子 + @Select("") + List findUsersByOptionalCriteria(@Param("name") String name, @Param("email") String email); + + // 批量删除 + @Delete("") + int deleteUsersByIds(@Param("ids") List ids); + + // 更新多个字段 + @Update("UPDATE users SET age = #{age}, email = #{email} WHERE id = #{id}") + int updateUserById(Long id, int age, String email); +}