上节是删除单条数据库记录,本节实现删除多条数据库记录。
根据姓名删除指定学生信息,指定一个学生姓名参数。
MyBatisStudentDao中编写
public int deleteStudentBystudentName(String studentNameParam){
sqlSession=MyBatisSqlSessionFactory.getSqlSession();
int rowCount=sqlSession.delete("com.teikin.mybatis.dao.mapper.MyBatisStudentDao.deleteStudentBystudentName",studentNameParam);
sqlSession.commit();
sqlSession.close();
return rowCount;
}
MyBatisStudentDao.xml中编写sql映射语句
<delete id="deleteStudentBystudentName" parameterType="java.lang.String">
<!-- delete from student where student_name like '%${studentParam}%'-->
delete from student where student_name like concat('%',#{studentParam},'%')
</delete>
Test测试类
@Test
public void testDeleteStudentBystudentName(){
String studentParam ="易同学";
myBatisStudentDao = new MyBatisStudentDao();
int rowCount = myBatisStudentDao.deleteStudentBystudentName(studentParam);
System.out.println("testDeleteStudentBystudentName影响的行数:" + rowCount);
}
运行结果
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 24293395.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@172b013]
DEBUG [main] - ==> Preparing: delete from student where student_name like concat('%',?,'%')
DEBUG [main] - ==> Parameters: 易同学(String)
DEBUG [main] - <== Updates: 1
DEBUG [main] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@172b013]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@172b013]
DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@172b013]
DEBUG [main] - Returned connection 24293395 to pool.
testDeleteStudentBystudentName影响的行数:1