需求分析
- 执行sql
- 对结果集进行友好封装
- 使用简单
- 性能优化
代码设计
代码架构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/320862edf22e9b121f60d80d41ad8382.png)
代码执行流程
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f7d83e8058f6deb1ead4fa0530f3c2a8.png)
代码精剪
初始化
@Component
public class MybatisInit implements ApplicationContextAware{
@Value("${mybatis.mapperPackage}")
private String mapperPackage;
@PostConstruct
public void init() throws BeansException {
String packagePath = getPackagePath(mapperPackage);
String path = getClass().getClassLoader().getResource(packagePath).getPath();
File file = new File(path);
File[] files = file.listFiles();
ClassLoader classLoader = this.getClass().getClassLoader();
for (File child : files) {
try {
String filePath = child.getAbsolutePath();
String externalName = filePath.substring(filePath.lastIndexOf('/') + 1,filePath.length() - 6);
externalName = mapperPackage + "." + externalName;
Class<?> aClass = classLoader.loadClass(externalName);
MapperRegistry.addMapper(aClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
@PreDestroy
private void destroy(){
ConnectFact