第十三章课后作业

1、如何使用MyBatis注解方式实现表数据的增、删、改、查?

  1. 增加数据(Insert) 使用@Insert注解可以将对象插入到数据库表中。
    
            import org.apache.ibatis.annotations.Insert;
    
            public interface UserMapper {
    
                @Insert("INSERT INTO users (username, password) VALUES (#{username}, #{password})")
                void insertUser(User user);
            }
    
            
  2. 删除数据(Delete) 使用@Delete注解可以从数据库表中删除数据。
    
            import org.apache.ibatis.annotations.Delete;
    
            public interface UserMapper {
    
                @Delete("DELETE FROM users WHERE id = #{id}")
                void deleteUserById(int id);
            }
    
            
  3. 更新数据(Update) 使用@Update注解可以更新数据库表中的数据。
    
    import org.apache.ibatis.annotations.Update;
    
    public interface UserMapper {
    
        @Update("UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}")
        void updateUser(User user);
    }
    
            
  4. 查询数据(Select) 使用@Select注解可以从数据库表中查询数据。
    
    import org.apache.ibatis.annotations.Select;
    
    public interface UserMapper {
    
        @Select("SELECT * FROM users WHERE id = #{id}")
        User getUserById(int id);
    }
    
            
    UserMapper 是一个MyBatis的Mapper接口,它定义了与用户表相关的操作。@Insert、@Delete、@Update、@Select 注解分别对应了插入、删除、更新和查询操作。 确保在MyBatis配置文件中正确配置了这个Mapper接口,然后就可以在应用中注入UserMapper并使用这些方法了。
  5. 
    
            
下一章作业