Mybatis如何从数据库中获取数据存为List类型(存为model)
目录
- 从数据库中获取数据存为List类型(存为model)
- Mybatis存储List类型数据
从数据库中获取数据存为List类型(存为model)
从数据库中获取的数据,存到一个model中,需要注意两点。
- 一、在dao中,只能用List类型接受结果
- 二、要在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{ /** * 很奇怪,在这里不能直接强转类型为ArrayList<GradeCheck> * 只能在下面return的时候强转类型..... * */ 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类型数据
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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)