您好,登錄后才能下訂單哦!
這篇文章主要介紹Mybatis怎么使用動態(tài)語句實(shí)現(xiàn)批量刪除,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
本人在數(shù)據(jù)庫建了一張emp員工表(表的數(shù)據(jù)自己填充),表的結(jié)構(gòu)如下:
批量刪除的核心代碼為,在實(shí)體映射文件中配置如下的代碼:
<!-- 批量刪除 --> <delete id="deleteMoreEmp" parameterType="int[]"> <!-- delete from emp where empno in(7789,7790) --> <!-- forEach : 用來循環(huán) collection : 用來指定循環(huán)的數(shù)據(jù)的類型 可以填的值有:array,list,map item : 循環(huán)中為每個循環(huán)的數(shù)據(jù)指定一個別名 index : 循環(huán)中循環(huán)的下標(biāo) open : 開始 close : 結(jié)束 separator : 數(shù)組中元素之間的分隔符 --> delete from emp where empno in <foreach collection="array" item="arr" index="no" open="(" separator="," close=")"> #{arr} </foreach> </delete>
本人使用的是maven搭建的web項(xiàng)目
IEmpDAO.java為接口提供批量刪除數(shù)據(jù)方法,EmpDAOImpl.java為接口的實(shí)現(xiàn)類,MybatisSqlSessionFactory.java為本人創(chuàng)建的獲取sqlSession的工具類,Emp.java為實(shí)體類,Emp.xml為映射文件,mybatis_cfg.xml為mybatis主配置文件,Test.java為測試類,pom.xml為maven引入依賴的文件。
/** * 批量刪除 * * @param arr * @return */ public boolean doRemoveeMore(int[] arr);
public boolean doRemoveeMore(int[] arr) { SqlSession sqlSession = null; try { sqlSession = MybatisSqlSessionFactory.getMySqlSession(); int result = sqlSession.delete("cn.sz.hcq.pojo.Emp.deleteMoreEmp", arr); sqlSession.commit(); return result > 0 ? true : false; } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally { MybatisSqlSessionFactory.closeSqlSession(); } return false; }
為本人創(chuàng)建的獲取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 = "mybatis_cfg.xml"; private static Reader reader = null; private static SqlSessionFactoryBuilder builder = null; private static SqlSessionFactory factory = null; // 可以在同一個線程范圍內(nèi),共享一個對象 private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); // 靜態(tài)代碼塊(類加載的時候執(zhí)行一次) static { try { reader = Resources.getResourceAsReader(RESOURCE); builder = new SqlSessionFactoryBuilder(); factory = builder.build(reader); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getMySqlSession() { // 從本地線程中獲取session連接 SqlSession sqlSession = threadLocal.get(); // 連接為空則創(chuàng)建連接,并將該連接添加到本地線程中去 if (sqlSession == null) { if (factory == null) { rebuildFactory(); } sqlSession = factory.openSession(); } threadLocal.set(sqlSession); return sqlSession; } // 創(chuàng)建工廠 public static void rebuildFactory() { try { reader = Resources.getResourceAsReader(RESOURCE); builder = new SqlSessionFactoryBuilder(); factory = builder.build(reader); } catch (IOException e) { e.printStackTrace(); } } // 關(guān)閉連接 public static void closeSqlSession() { SqlSession sqlSession = threadLocal.get(); if (sqlSession != null) { // 關(guān)閉session sqlSession.close(); } // 同時將本地線程中置為null(防止用戶再次調(diào)用時出現(xiàn)空的session) threadLocal.set(null); } }
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; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.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) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/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 : 用來循環(huán) collection : 用來指定循環(huán)的數(shù)據(jù)的類型 可以填的值有:array,list,map item : 循環(huán)中為每個循環(huán)的數(shù)據(jù)指定一個別名 index : 循環(huán)中循環(huán)的下標(biāo) open : 開始 close : 結(jié)束 separator : 數(shù)組中元素之間的分隔符 --> delete from emp where empno in <foreach collection="array" item="arr" index="no" open="(" separator="," close=")"> #{arr} </foreach> </delete> </mapper>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--數(shù)據(jù)源 --> <environments default="myconn"> <environment id="myconn"> <!--事務(wù)管理方式 --> <transactionManager type="JDBC"></transactionManager> <!--數(shù)據(jù)庫連接參數(shù) --> <dataSource type="POOLED"> <!-- type:數(shù)據(jù)源連接的方式 ,POOLED:連接池方式, UNPOOLED: 非連接池的方式 ,JNDI:java命名與目錄接口方式 --> <property name="driver" value="org.gjt.mm.mysql.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> <!-- 引入實(shí)體映射文件 --> <mappers> <mapper resource="cn/sz/hcq/pojo/Emp.xml" /> </mappers> <!-- 配置的參數(shù) --> </configuration>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.sz.hcq.pro</groupId> <artifactId>Mybatis_04</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> </dependencies> </project>
public class Test { public static void main(String[] args) { IEmpDAO empDAO = new EmpDAOImpl(); System.out.println("----------批量刪除----------"); int[] arr = { 7791, 7792 };//刪除的主鍵 boolean removeeMore = empDAO.doRemoveeMore(arr); System.out.println("批量刪除的結(jié)果:" + removeeMore); } }
完成代碼后,運(yùn)行測試類就可以完成批量刪除。
以上是“Mybatis怎么使用動態(tài)語句實(shí)現(xiàn)批量刪除”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。