跟着孔浩的视频,学习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=?。