Files
springboot-scaffold/src/main/java/com/example/scaffold/mapper/UserMapper.java
likingcode dd2b4d0e4b fix: 修复部署问题
- 修复 RootController 返回 index.html
- 修复 MyBatis 缓存配置
- 修复 session scope bean 注入问题
2026-03-07 09:17:16 +00:00

78 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.example.scaffold.mapper;
import com.example.scaffold.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import java.util.List;
/**
* User MyBatis Mapper - 演示 MyBatis 注解方式
*
* 学习要点:
* 1. @Mapper - 标记为 MyBatis Mapper 接口
* 2. @Select/@Insert/@Update/@Delete - SQL 注解
* 3. @Options - 额外选项如返回自增ID
* 4. @ResultMap - 结果映射
* 5. @CacheNamespace - 二级缓存
*/
@Mapper
@CacheNamespace
public interface UserMapper {
/**
* 查询所有用户
*/
@Select("SELECT * FROM users ORDER BY created_at DESC")
List<User> findAll();
/**
* 根据ID查询 - 使用一级缓存
*/
@Select("SELECT * FROM users WHERE id = #{id}")
User findById(@Param("id") Long id);
/**
* 根据用户名模糊查询 - 动态SQL演示
*/
@Select("SELECT * FROM users WHERE username LIKE CONCAT('%', #{username}, '%')")
List<User> findByUsernameLike(@Param("username") String username);
/**
* 插入用户 - 返回自增ID
*/
@Insert("INSERT INTO users(username, email, phone, bio, active, created_at, updated_at) " +
"VALUES(#{username}, #{email}, #{phone}, #{bio}, #{active}, NOW(), NOW())")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insert(User user);
/**
* 更新用户
*/
@Update("UPDATE users SET username=#{username}, email=#{email}, phone=#{phone}, " +
"bio=#{bio}, active=#{active}, updated_at=NOW() WHERE id=#{id}")
int update(User user);
/**
* 删除用户
*/
@Delete("DELETE FROM users WHERE id = #{id}")
int deleteById(@Param("id") Long id);
/**
* 统计用户数量
*/
@Select("SELECT COUNT(*) FROM users")
long count();
/**
* 批量插入 - 演示脚本SQL
*/
@Insert("<script>" +
"INSERT INTO users(username, email, phone, active, created_at, updated_at) VALUES " +
"<foreach collection='users' item='u' separator=','>" +
"(#{u.username}, #{u.email}, #{u.phone}, #{u.active}, NOW(), NOW())" +
"</foreach>" +
"</script>")
int batchInsert(@Param("users") List<User> users);
}