Mybatis如何從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model)
更新時間:2022年01月20日 08:47:35 作者:_Naive_
這篇文章主要介紹了Mybatis如何從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model)
從數(shù)據(jù)庫中獲取的數(shù)據(jù),存到一個model中,需要注意兩點。
- 一、在dao中,只能用List類型接受結(jié)果
- 二、要在mapper中寫清楚resultType
//DAO @Override public ArrayList<YourModel> getMainInfo(int id) { // TODO Auto-generated method stub List<YourModel> result = null; try{ sqlSession = this.getSqlSession(); }catch (Exception e){ e.printStackTrace(); YourModel yourModel = new YourModel(); try{ /** * 很奇怪,在這里不能直接強轉(zhuǎn)類型為ArrayList<GradeCheck> * 只能在下面return的時候強轉(zhuǎn)類型..... * */ result = sqlSession.selectList(this.NAMESPACE.concat("getMainInfo"), yourModel); }catch (Exception e){ return null; } return (ArrayList<YourModel>)result; } //mapper List<model> findByIds(Long... ids); <select id="findByIds" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from model(tableName) where ID in <foreach item="item" index="index" collection="array" open="(" separaotr="," close=")"> #{item} </foreach> </select>
Mybatis存儲List類型數(shù)據(jù)
Dao層
void insertList(List<TZpcjsj> list);
*Mapper
?<!--批量 插入記錄 --> ? ? <insert id="insertList" > ? ? ? ? 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 ? ? ? ? <foreach collection="list" item="item" index="index" separator=','> ? ? ? ? ? ? ? ? (#{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}) ? ? ? ? </foreach> ? ? </insert>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot集成JWT實現(xiàn)身份認證(權(quán)鑒)的方法步驟
本文主要介紹了springboot集成JWT實現(xiàn)身份認證(權(quán)鑒)的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04基于Java實現(xiàn)一個簡單的數(shù)據(jù)同步組件
這篇文章主要為大家詳細介紹了如何基于Java實現(xiàn)一個簡單的數(shù)據(jù)同步組件,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下2023-06-06SpringBoot整合Gson 整合Fastjson的實例詳解
這篇文章主要介紹了SpringBoot整合Gson 整合Fastjson的實例詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11詳解Java如何判斷ResultSet結(jié)果集是否為空
ResultSet 表示 select 語句的查詢結(jié)果集。這篇文章主要為大家詳細介紹了Java如何判斷ResultSet結(jié)果集是否為空,感興趣的可以了解一下2023-02-02SpringBoot多數(shù)據(jù)源的兩種實現(xiàn)方式實例
最近在項目開發(fā)中,需要為一個使用MySQL數(shù)據(jù)庫的SpringBoot項目,新添加一個PLSQL數(shù)據(jù)庫數(shù)據(jù)源,下面這篇文章主要給大家介紹了關(guān)于SpringBoot多數(shù)據(jù)源的兩種實現(xiàn)方式,需要的朋友可以參考下2022-04-04