This paper focuses on a Central Pattern Generator (CPG)-based locomotion controller design for a boxfish-like robot. The bio-inspired controller is aimed at flexible switching in multiple 3D swimming patterns and exact attitude control of yaw and roll such that the robot will swim more like a real boxfish. The CPG network comprises two layers, the lower layer is the network of coupled linear oscillators and the upper is the transition layer where the lower-dimensional locomotion stimuli are transformed into the higher-dimensional control parameters serving for all the oscillators. Based on such a two-layer framework, flexible switching between multiple three-dimensional swimming patterns, such as swimming forwards/backwards, turning left/right, swimming upwards/downwards and rolling clockwise/counter-clockwise, can be simply realized by inputting different stimuli. Moreover, the stability of the CPG network is strictly proved to guarantee the intrinsic stability of the swimming patterns. As to exact attitude control, based on this open-loop CPG network and the sensory feedback from the Inertial Measurement Unit (IMU), a closed-loop CPG controller is advanced for yaw and roll control of the robotic fish for the first time. This CPG-based online attitude control for a robotic fish will greatly facilitate high-level practical underwater applications. A series of relevant experiments with the robotic fish are conducted systematically to validate the effectiveness and stability of the open-loop and closed-loop CPG controllers.