跟着孔浩的视频,学习mybatis。
获取mybatis-config.xml,创建工厂类的过程写在了一个DBUtil类中,提供获取session的静态方法。读取xml文件的初始化代码写在static 语句块中。
如果mybatis的mapper.xml中的sql写的有错误,Resources.getResourceAsStream方法读取xml的代码被包含在try catch中,但是只catch了找不到xml文件的IOException,导致xml中的语法、书写错误造成的exception没有被捕获。要注意在catch块中一定要对Exception处理。
mybatis的sql配置中,对象中的对象字段的属性是用对象名来作前缀的(真绕口)
public class Address{
public User u;
String address;
String postcode;
String street;
String area;
\\省略getter setter
}
public class User{
String usercode;
String username;
}
在写根据usercode查询address的sql时,应该是:
select * from t_address where u.usercode = ?
或者
select * from t_address where User.usercode = ? \\User或user都行,首字母大小写随意,真心很智能
不能直接写 where usercode=?。
本文详细讲解了如何使用MyBatis框架进行数据库操作,包括mybatis-config.xml的配置、工厂类DBUtil的实现,以及如何在读取mapper.xml时捕获并处理可能的异常。此外,还介绍了如何正确地在SQL配置中引用对象属性,并以实例展示了如何查询特定对象关联的数据。
1440

被折叠的 条评论
为什么被折叠?



