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)文章
Java實現(xiàn)讀取TXT和CSV文件內(nèi)容
這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)讀取TXT和CSV文件內(nèi)容的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-02-02
詳解Java中方法next()和nextLine()的區(qū)別與易錯點
這篇文章主要介紹了詳解Java中方法next()和nextLine()的區(qū)別與易錯點,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
java實現(xiàn)在普通類中注入service或mapper
這篇文章主要介紹了java實現(xiàn)在普通類中注入service或mapper的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot整合Mybatis實現(xiàn)商品評分的項目實踐
本文介紹了SpringBoot整合Mybatis-plus框架實現(xiàn)對商品評分的功能實現(xiàn)流程和前端接口實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02

