Mybatis使⽤动态语句实现批量删除(delete结合foreach)Mybatis 使⽤动态语句实现批量删除(delete结合foreach)
在下⾯的案例本⼈将演⽰如何使⽤动态语句批量删除数据库数据,本⼈在数据库建了⼀张emp员⼯表(表的数据⾃⼰填
充),表的结构如下:
批量删除的核⼼代码为,在实体映射⽂件中配置如下的代码:
<!-- 批量删除 -->
<delete id="deleteMoreEmp" parameterType="int[]">
<!-- delete from emp where empno in(7789,7790) -->
<!-- forEach : ⽤来循环 collection : ⽤来指定循环的数据的类型可以填的值有:array,list,map item
: 循环中为每个循环的数据指定⼀个别名 index : 循环中循环的下标 open : 开始 close : 结束 separator : 数组中元素之间的分隔符  delete from emp where empno in
<foreach collection="array" item="arr" index="no" open="("
separator="," close=")">
#{arr}
</foreach>
</delete>
下⾯是项⽬的结构(本⼈使⽤的是maven搭建的web项⽬):
IEmpDAO.java为接⼝提供批量删除数据⽅法,EmpDAOImpl.java为接⼝的实现类,MybatisSqlSessionFactory.java为本⼈创建的获取sqlSession的⼯具类,Emp.java为实体类,l为映射⽂件,l为mybatis主配置⽂
件,Test.java为测试类,l为maven引⼊依赖的⽂件。
1、IEmpDAO.java为接⼝提供批量删除数据⽅法
/**
* 批量删除
*
* @param arr
* @return
*/
public boolean doRemoveeMore(int[] arr);
传⼊的参数为emp的主键数组。
2、EmpDAOImpl.java为接⼝的实现类
public boolean doRemoveeMore(int[] arr) {
SqlSession sqlSession = null;
try {
sqlSession = MySqlSession();
int result = sqlSession.delete("cn.sz.hcq.pojo.Emp.deleteMoreEmp",
arr);
sqlSessionmit();
return result > 0 ? true : false;
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisSqlSessionFactory.closeSqlSession();
}
return false;
}
3、MybatisSqlSessionFactory.java为本⼈创建的获取sqlSession的⼯具类
package cn.sz.hcq.factory;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisSqlSessionFactory {
// 配置⽂件
private static final String RESOURCE = "l";
private static Reader reader = null;
private static SqlSessionFactoryBuilder builder = null;
private static SqlSessionFactory factory = null;
// 可以在同⼀个线程范围内,共享⼀个对象
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
// 静态代码块(类加载的时候执⾏⼀次)
static {
try {
reader = ResourceAsReader(RESOURCE);
builder = new SqlSessionFactoryBuilder();
factory = builder.build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getMySqlSession() {
// 从本地线程中获取session连接
SqlSession sqlSession = ();
// 连接为空则创建连接,并将该连接添加到本地线程中去
if (sqlSession == null) {
if (factory == null) {
rebuildFactory();
}
sqlSession = factory.openSession();
}
threadLocal.set(sqlSession);
return sqlSession;
}
// 创建⼯⼚
public static void rebuildFactory() {
try {
reader = ResourceAsReader(RESOURCE);
builder = new SqlSessionFactoryBuilder();
factory = builder.build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭连接
public static void closeSqlSession() {
SqlSession sqlSession = ();
if (sqlSession != null) {
// 关闭session
sqlSession.close();
}
// 同时将本地线程中置为null(防⽌⽤户再次调⽤时出现空的session)  threadLocal.set(null);
}
}
4、Emp.java为实体类
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;mysql中delete语句
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {  pno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {  ame = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
< = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {  this.hiredate = hiredate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {  thism = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {  this.deptno = deptno;
}
}
}
5、l为映射⽂件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.sz.hcq.pojo.Emp">
<!-- 批量删除 -->
<delete id="deleteMoreEmp" parameterType="int[]">
<!-- delete from emp where empno in(7789,7790) -->
<!-- forEach : ⽤来循环 collection : ⽤来指定循环的数据的类型可以填的值有:array,list,map item
: 循环中为每个循环的数据指定⼀个别名 index : 循环中循环的下标 open : 开始 close : 结束 separator : 数组中元素之间的分隔符 -->  delete from emp where empno in
<foreach collection="array" item="arr" index="no" open="("
separator="," close=")">
#{arr}
</foreach>
</delete>
</mapper>
6、l为mybatis主配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<!--数据源 -->
<environments default="myconn">
<environment id="myconn">
<!--事务管理⽅式 -->
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接参数 -->
<dataSource type="POOLED">
<!-- type:数据源连接的⽅式,POOLED:连接池⽅式, UNPOOLED: ⾮连接池的⽅式,JNDI:java命名与⽬录接⼝⽅式 -->
<property name="driver" value="sql.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<!-- 引⼊实体映射⽂件 -->
<mappers>
<mapper resource="cn/sz/hcq/l" />
</mappers>
<!-- 配置的参数 -->
</configuration>
7、l为maven引⼊依赖的⽂件

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。