Mybatis如何從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model)
從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model)
從數(shù)據(jù)庫中獲取的數(shù)據(jù),存到一個(gè)model中,需要注意兩點(diǎn)。
- 一、在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{
/**
* 很奇怪,在這里不能直接強(qiáng)轉(zhuǎn)類型為ArrayList<GradeCheck>
* 只能在下面return的時(shí)候強(qiáng)轉(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存儲(chǔ)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>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)讀取TXT和CSV文件內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)讀取TXT和CSV文件內(nèi)容的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
詳解Java中方法next()和nextLine()的區(qū)別與易錯(cuò)點(diǎn)
這篇文章主要介紹了詳解Java中方法next()和nextLine()的區(qū)別與易錯(cuò)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
java實(shí)現(xiàn)在普通類中注入service或mapper
這篇文章主要介紹了java實(shí)現(xiàn)在普通類中注入service或mapper的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot整合Mybatis實(shí)現(xiàn)商品評(píng)分的項(xiàng)目實(shí)踐
本文介紹了SpringBoot整合Mybatis-plus框架實(shí)現(xiàn)對(duì)商品評(píng)分的功能實(shí)現(xiàn)流程和前端接口實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
RocketMQ中的消費(fèi)模式和消費(fèi)策略詳解
這篇文章主要介紹了RocketMQ中的消費(fèi)模式和消費(fèi)策略詳解,RocketMQ 是基于發(fā)布訂閱模型的消息中間件,所謂的發(fā)布訂閱就是說,consumer 訂閱了 broker 上的某個(gè) topic,當(dāng) producer 發(fā)布消息到 broker 上的該 topic 時(shí),consumer 就能收到該條消息,需要的朋友可以參考下2023-10-10
MyBatis詳細(xì)執(zhí)行流程的全紀(jì)錄
這篇文章主要給大家介紹了關(guān)于MyBatis詳細(xì)執(zhí)行流程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

