If your boyfriend knows – or can guess – the passcode to your phone, or your Apple ID and Apple ID password, he could easily get into your phone (when he had possession of it), or into your account – no "hacking" required. You may want to change your password and/or passcode to keep him out.
If you think your Apple Account has been compromised - Apple Support
Set a passcode on iPhone - Apple Support
If you are sharing an Apple ID with him, and he is an "ex-boyfriend" who is harassing you, continuing to share that Apple ID is a very bad idea. Apple recommends that each person have their own Apple ID.
As for "some kind of MDM", it's more likely that your carrier has installed one or more entries on your phone to let it use Wi-Fi hotspots associated with their network. Many U.S. carriers do this as a way of offloading data from their cellular networks, and it is not evidence of "hacking" or remote control of the phone (other than to "encourage" the phone to use those Wi-Fi hotspots).
You can "factory erase" your iPhone – but unless you make a copy of your data first – e.g., by synchronizing data to iCloud and doing an iCloud backup for the remaining data – wiping your phone will cause to to lose EVERYTHING on your phone – photos, contacts, the lot. Backing up stuff, resetting your phone, and then restoring your data may be no panacea if you are restoring, e.g., passcodes and passwords that your ex-boyfriend knows.
So my guess is that you need to focus on securing all of your accounts and passwords FIRST. (Not just your iPhone passcode and Apple ID password, but any other passwords that he might have learned.)
After that, you might not even need to do a backup / reset / restore. But doing a reset will be pointless as long as he knows your passwords.