How does Mybatis get the data from the database and save it as List type
This article mainly introduces how Mybatis gets the data from the database as List type, which is very detailed and has certain reference value. Friends who are interested must read it!
Get data from the database and save it as List (save as model)
Data obtained from the database, stored in a model, need to pay attention to two points.
In dao, only the List type can be used to accept the result.
Second, write resultType clearly in mapper
/ / DAO @ Override public ArrayList getMainInfo (int id) {/ / TODO Auto-generated method stub List result = null; try {sqlSession = this.getSqlSession ();} catch (Exception e) {e.printStackTrace (); YourModel yourModel = new YourModel () Try {/ * it's strange that you can't force the type to ArrayList directly here * you can only change the type when you return below. * * / result = sqlSession.selectList (this.NAMESPACE.concat ("getMainInfo"), yourModel);} catch (Exception e) {return null;} return (ArrayList) result;} / / mapperList findByIds (Long... Ids); select from model (tableName) where ID in # {item} Mybatis stores List type data
Dao layer
Void insertList (List list)
* Mapper
INSERT INTO t_zpcjsj (nian_fen,hang_hao,belong_to_account,zhong_ming,lai_yuan,chang_du,bi_qiang,ma_zhi,ling_zhong,yi_fen,chu_miao_qi,kai_hua_qi,tu_xu_qi,szs_miao_qi,szs_hua_qi,zqd_miao_qi,zqd_hua_qi,shou_huo_zhu_shu,zytx_zhu_xing,zytx_ye_xing,zytx_ling_xing,zytx_zhu_gao,zytx_jie_ling_xing Zytx_ye_xu_xing,ku_wei_bing_zhi,huang_wei_bing_zhi,tian_jian_jue_xuan,mark) VALUES (# {item.nianFen}, # {item.hangHao}, # {item.belongToAccount}, # {item.zhongMing}, # {item.laiYuan}, # {item.changDu}, # {item.biQiang}, # {item.maZhi}, # {item.lingZhong}, # {item.yiFen}, # {item.chuMiaoQi}, # {item.kaiHuaQi} # {item.tuXuQi}, # {item.szsMiaoQi}, # {item.szsHuaQi}, # {item.zqdMiaoQi}, # {item.zqdHuaQi}, # {item.shouHuoZhuShu}, # {item.zytxZhuXing}, # {item.zytxYeXing}, # {item.zytxLingXing}, # {item.zytxZhuGao}, # {item.zytxJieLingXing}, # {item.zytxYeXuXing}, # {item.kuWeiBingZhi}, # {item.huangWeiBingZhi}, # {item.tianJianJueXuan} # {item.mark}) these are all the contents of the article "how does Mybatis get data from the database and save it as List type" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!