MyBatis批量添加、修改和删除
程序员文章站
2024-03-08 13:01:46
废话不多说了,直接步入正题了。
1、批量添加元素session.insert(string string,object o)
public void bat...
废话不多说了,直接步入正题了。
1、批量添加元素session.insert(string string,object o)
public void batchinsertstudent(){ list<student> ls = new arraylist<student>(); for(int i = 5;i < 8;i++){ student student = new student(); student.setid(i); student.setname("maoyuanjun" + i); student.setsex("man" + i); student.settel("tel" + i); student.setaddress("浙江省" + i); ls.add(student); } sqlsession session = sessionfactoryutil.getsqlsessionfactory().opensession(); session.insert("mybatisdemo.domain.student.batchinsertstudent", ls); session.commit(); session.close(); } <insert id="batchinsertstudent" parametertype="java.util.list"> insert into student (id,name,sex,tel,address) values <foreach collection="list" item="item" index="index" separator="," > (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address}) </foreach> </insert>
2、批量修改session. insert (string string,object o)
实例1:
public void batchupdatestudent(){ list<integer> ls = new arraylist<integer>(); for(int i = 2;i < 8;i++){ ls.add(i); } sqlsession session = sessionfactoryutil.getsqlsessionfactory().opensession(); session.insert("mybatisdemo.domain.student.batchupdatestudent",ls); session.commit(); session.close(); } <update id="batchupdatestudent" parametertype="java.util.list"> update student set name = "5566" where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")" > #{item} </foreach> </update>
实例2:
public void batchupdatestudentwithmap(){ list<integer> ls = new arraylist<integer>(); for(int i = 2;i < 8;i++){ ls.add(i); } map<string,object> map = new hashmap<string,object>(); map.put("idlist", ls); map.put("name", "mmao789"); sqlsession session = sessionfactoryutil.getsqlsessionfactory().opensession(); session.insert("mybatisdemo.domain.student.batchupdatestudentwithmap",map); session.commit(); session.close(); } <update id="batchupdatestudentwithmap" parametertype="java.util.map" > update student set name = #{name} where id in <foreach collection="idlist" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </update>
3、批量删除session.delete(string string,object o)
public void batchdeletestudent(){ list<integer> ls = new arraylist<integer>(); for(int i = 4;i < 8;i++){ ls.add(i); } sqlsession session = sessionfactoryutil.getsqlsessionfactory().opensession(); session.delete("mybatisdemo.domain.student.batchdeletestudent",ls); session.commit(); session.close(); } <delete id="batchdeletestudent" parametertype="java.util.list"> delete from student where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete>
好了,本文到此结束,希望对大家有所帮助。
上一篇: Spring实现文件上传功能
推荐阅读
-
用java实现对MySql数据库中数据的读取、插入、修改和删除
-
【MySQL】MySQL数据库 篇三——操作数据库(添加、删除、修改、插入)
-
SQL-(8)数据库的创建、查询、修改和删除
-
Mybatis 中的sql批量修改方法实现
-
mybatis创建一个或多个新用户 insert 字段和表名不确定时动态添加问题
-
mybatis创建一个或多个新用户 insert 字段和表名不确定时动态添加问题
-
Android简单修改原有应用和添加应用的方法
-
thinkphp利用模型通用数据编辑添加和删除的实例代码
-
实习日志--6、SSM框架实现批量删除和权限管理功能
-
【学习日记】SSM项目-批量删除、拦截和用户权限功能开发