Mybatis一对多延迟加载实现代码解析
需求:一个用户可以有多个账户,查询用户时实现账户的延迟加载
1、用户实体类
packagecom.yl.bean; importjava.io.Serializable; importjava.sql.Date; importjava.util.List; /** *用户实体类 */ publicclassUserimplementsSerializable{ privateIntegerid;//id privateStringusername;//用户名 privateDatebirthday;//生日 privateStringsex;//性别 privateStringaddress;//地址 privateListaccountList;//用户所拥有的账户 publicUser(){ } publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } publicDategetBirthday(){ returnbirthday; } publicvoidsetBirthday(Datebirthday){ this.birthday=birthday; } publicStringgetSex(){ returnsex; } publicvoidsetSex(Stringsex){ this.sex=sex; } publicStringgetAddress(){ returnaddress; } publicvoidsetAddress(Stringaddress){ this.address=address; } publicList getAccountList(){ returnaccountList; } publicvoidsetAccountList(List accountList){ this.accountList=accountList; } @Override publicStringtoString(){ return"User{"+ "id="+id+ ",username='"+username+'\''+ ",birthday="+birthday+ ",sex='"+sex+'\''+ ",address='"+address+'\''+ '}'; } }
2、账户实体类
packagecom.yl.bean; importjava.io.Serializable; /** *账户实体类 */ publicclassAccountimplementsSerializable{ privateIntegerid;//账户id privateIntegeruid;//用户id privateDoublemoney;//余额 privateUseruser;//账户所属用户 publicAccount(){ } publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicIntegergetUid(){ returnuid; } publicvoidsetUid(Integeruid){ this.uid=uid; } publicDoublegetMoney(){ returnmoney; } publicvoidsetMoney(Doublemoney){ this.money=money; } publicUsergetUser(){ returnuser; } publicvoidsetUser(Useruser){ this.user=user; } @Override publicStringtoString(){ return"Account{"+ "id="+id+ ",uid="+uid+ ",money="+money+ '}'; } }
3、用户持久层接口
packagecom.yl.dao; importcom.yl.bean.User; importjava.util.List; /** *用户持久层接口 */ publicinterfaceIUserDao{ /** *查询所有用户 */ ListqueryAll(); /** *根据id查询用户 */ UserqueryById(intid); }
4、账户持久层接口
packagecom.yl.dao; importcom.yl.bean.Account; importcom.yl.bean.User; importjava.util.List; /** *账户持久层接口 */ publicinterfaceIAccountDao{ /** *查询所有账户 */ ListqueryAll(); /** *根据用户id查询账户 */ List queryByUid(intuid); }
5、全局配置文件
6、用户映射配置文件
SELECT*FROMUSER select*fromuserwhereid=#{id}
7、账户映射配置文件
SELECT*FROMaccount select*fromaccountwhereuid=#{uid}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。