As the population ages, the increase in the number of middle-aged and older adults with diabetes poses new challenges to the allocation of resources in the healthcare system. Developing accurate diabetes prediction models is a critical public health strategy to improve the efficient use of healthcare resources and ensure timely and effective treatment. In order to improve the identification of diabetes in middle-aged and older patients, a Bagging-RF model is proposed. In the study, two diabetes datasets on Kaggle were first preprocessed, including unique heat coding, outlier removal, and age screening, after which the data were categorized into three age groups, 50–60, 60–70, and 70–80, and balanced using the SMOTE technique. Then, the machine learning classifiers were trained using the Bagging-RF integrated model with eight other machine learning classifiers. Finally, the model’s performance was evaluated by accuracy, F1 score, and other metrics. The results showed that the Bagging-RF model outperformed the other eight machine learning classifiers, exhibiting 97.35%, 95.55%, 95.14% accuracy and 97.35%, 97.35%, 95.14% F1 Score at the Diabetes Prediction Dataset for diabetes prediction for the three age groups of 50–60, 60–70, and 70–80; and 97.03%, 94.90%, 93.70% accuracy and 97.03%, 94.90%, 93.70% F1 Score at the Diabetes Prediction Dataset. 95.55%, 95.13% F1 Score; and 97.03%, 94.90%, 93.70% accuracy; and 97.03%, 94.89%, 93.70% F1 Score at Diabetes Prediction Dataset. In addition, while other integrated learning models, such as ET, RF, Adaboost, and XGB, fail to outperform Bagging-RF, they also show excellent performance.