Updated:

1. Build the bookshelf ring and table

/fill ~-2 ~ ~2 ~2 ~1 ~6 minecraft:bookshelf
/fill ~-1 ~ ~3 ~1 ~1 ~5 minecraft:air
/fill ~ ~ ~2 ~ ~1 ~2 minecraft:air
/setblock ~ ~ ~4 minecraft:enchanting_table

This places a 5x5, two-high block of shelves two blocks south of you, hollows out the middle 3x3, opens a doorway and puts the table in the center. That is 30 shelves, well over the 15 needed.

2. Bookshelf rules

3. XP and lapis

/xp add @s 30 levels
/give @s minecraft:lapis_lazuli 64

The level 30 option needs 30 levels and 3 lapis, and costs 3 levels. On Bedrock use /xp 30L @s (xp command).

4. Skip the table

/enchant @s sharpness 5

Adds an exact enchantment to the held item, no luck involved.

5. Add an anvil and grindstone

/setblock ~2 ~ ~1 minecraft:anvil
/setblock ~-2 ~ ~1 minecraft:grindstone[face=floor]
/give @s minecraft:enchanted_book[stored_enchantments={mending:1}]

Place them on both sides in front of the bookshelves. The anvil applies books and the grindstone removes enchantments. On 1.21.4 and older write {levels:{mending:1}}. See the enchanted book command.

Troubleshooting

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.