In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Mybatis how to use dynamic statements to achieve batch deletion, the article is very detailed, has a certain reference value, interested friends must read it!
Next I will demonstrate how to use dynamic statements to delete database data in bulk
I have created an emp employee table in the database (the data of the table is filled in by myself). The structure of the table is as follows:
The core code for batch deletion is to configure the following code in the entity mapping file:
Delete from emp where empno in # {arr} the following is the structure of the project
I use the web project built by maven
IEmpDAO.java provides batch deletion method for the interface, EmpDAOImpl.java is the implementation class of the interface, MybatisSqlSessionFactory.java is the tool class created by myself to obtain sqlSession, Emp.java is the entity class, Emp.xml is the mapping file, mybatis_cfg.xml is the mybatis main configuration file, Test.java is the test class, and pom.xml introduces dependent files for maven.
1. IEmpDAO.java provides batch deletion method for the interface / * * batch deletion * * @ param arr * @ return * / public boolean doRemoveeMore (int [] arr). 2. EmpDAOImpl.java is the implementation class public boolean doRemoveeMore (int [] arr) {SqlSession sqlSession = null of the interface. 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;} 3, MybatisSqlSessionFactory.java
The tool class that is created for me to get 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 {/ / configuration file private static final String RESOURCE = "mybatis_cfg.xml"; private static Reader reader = null; private static SqlSessionFactoryBuilder builder = null Private static SqlSessionFactory factory = null; / / you can share an object private static ThreadLocal threadLocal = new ThreadLocal () within the same thread scope; / / static code block (executed once when the class is loaded) static {try {reader = Resources.getResourceAsReader (RESOURCE); builder = new SqlSessionFactoryBuilder () Factory = builder.build (reader);} catch (IOException e) {e.printStackTrace ();}} public static SqlSession getMySqlSession () {/ / get the session connection SqlSession sqlSession = threadLocal.get () from the local thread / / create a connection if the connection is empty and add the connection to the local thread to if (sqlSession = = null) {if (factory = = null) {rebuildFactory ();} sqlSession = factory.openSession () } threadLocal.set (sqlSession); return sqlSession;} / / create factory public static void rebuildFactory () {try {reader = Resources.getResourceAsReader (RESOURCE); builder = new SqlSessionFactoryBuilder () Factory = builder.build (reader);} catch (IOException e) {e.printStackTrace ();}} / / close the connection public static void closeSqlSession () {SqlSession sqlSession = threadLocal.get () If (sqlSession! = null) {/ / close session sqlSession.close ();} / / also set the local thread to null (prevent empty session when the user calls again) threadLocal.set (null) }} 4. Emp.java is entity class 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 }} 5. Emp.xml is the mapping file delete from emp where empno in # {arr} 6, Mybatis_cfg.xml is the mybatis main configuration file 7 、 Pom.xml introduces the dependent file 4.0.0 cn.sz.hcq.pro Mybatis_04 0.0.1-SNAPSHOT war org.mybatis mybatis 3.2.3 for maven Mysql mysql-connector-java 5.1.39 8 、 Test.java is the test class public class Test {public static void main (String [] args) {IEmpDAO empDAO = new EmpDAOImpl () System.out.println ("- batch delete -"); int [] arr = {7791, 7792}; / / deleted primary key boolean removeeMore = empDAO.doRemoveeMore (arr); System.out.println ("result of batch deletion:" + removeeMore);}}
Once the code is complete, the batch deletion can be completed by running the test class.
The above is all the contents of the article "how to use dynamic statements to achieve batch deletion in Mybatis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.