Welcome to Android Central! How do you know your phone was hacked? Do you mean that you lost all of your data, or that others have gained access to your private information?
Rooting your phone won't prevent anyone from hacking your phone, it's not some magic procedure. It just allows apps that won't run on your phone, because they need root elevation, to run.
If the bootloader can be unlocked, you can use Magisk.