MyBatis批量添加、修改和删除

网友投稿 221 2023-07-22


MyBatis批量添加、修改和删除

废话不多说了,直接步入正题了。

1、批量添加元素session.insert(String string,Object o)

public void batchInsertStudent(){

List ls = new ArrayList();

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 = SesnVPwigYsionFactoryUtil.getSqlSessionFactory().openSession();

session.insert("mybatisdemo.domain.Student.batchInsertStudent", ls);

session.commit();

session.close();

}

INSERT INTO STUDENT (id,name,sex,tel,address)

VALUES

(#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})

2、批量修改session. insert (String string,Object o)

实例1:

public void batchUpdateStudent(){

List ls = new ArrayList();

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 STUDENT SET name = "5566" WHERE id IN

#{item}

实例2:

public void batchUpdateStudentWithMap(){

List ls = new ArrayList();

for(int i = 2;i < 8;i++){

ls.add(i);

}

Map map = new HashMap();

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 STUDENT SET name = #{name} WHERE id IN

#{item}

3、批量删除session.delete(String string,Object o)

public void batchDeleteStudent(){

List ls = new ArrayList();

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 FROM STUDENT WHERE id IN

#{item}

好了,本文到此结束,希望对大家有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于Java中两种jersey文件上传方式
下一篇:Spring自定义配置Schema可扩展(一)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~