Ajax实现批量删除
1、在IUserDao中,对于IUserDao接口,写deleteAll方法的声明
void deleteAll(@Param("ids") List<Integer> ids);
2、在UserMapper中,实现Dao接口,也就是对数据库进行操作
对List列表进行遍历实现批量删除
<delete id="deleteAll">
delete from tb_user where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
3、在IUserService中,对于IUseService接口,写deleteAll方法的声明
void deleteAll(List<Integer> ids);
4、在UserServiceImpl中实现IUserService中声明的接口
@Override
public void deleteAll(List<Integer> ids) {
userDao.deleteAll(ids);
}
5、在UserController中写deleteAll方法
删除操作不需要返回值,deleteAll返回空字符串。
为了实现批量删除,对userList字符串进行处理,得到待删除用户的id列表,再调用userService对象的deleteAll方法进行批量删除
@RequestMapping("deleteAll.do")
@ResponseBody
public String deleteAll(String userList){
String[] strings = userList.split(",");
List<Integer> ids=new ArrayList<>();
for(String s:strings){
ids.add(Integer.parseInt(s));
}
userService.deleteAll(ids);
return "";
}
user-list.jsp:
批量删除是基于删除这个功能实现的
在界面使用复选框批量选择用户后,调用deleteAll函数
function deleteAll() {
var checkedNum=$("input[name='ids']:checked").length;
alert(checkedNum);
if(checkedNum==0){
alert("请至少选择一个进行删除!!!");
return;
}
if(confirm("确认要删除这些用户吗?")){
var userList=new Array();
$("input[name='ids']:checked").each(
function () {
userList.push($(this).val())
}
);
alert(userList);
$.ajax({
type:"post",
url: "${pageContext.request.contextPath}/user/deleteAll.do",
data:{userList:userList.toString()},
success:function () {
alert("删除成功");
location.reload();
},
error:function () {
alert("删除失败");
}
});
}
}
springboot
ssm简化了我们的编程工作,但配置相关文件比较繁琐
springboot遵循默认大于配置的原则,使得配置要简单许多
创建
阿里云spring boot工程脚手架: https://start.aliyun.com/
因为springboot项目包结构和之前的ssm项目包结构差不多
所以可以用maven创建项目,再修改pom.xml、添加application.yml、Springboot3application类
pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>springboot3</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置
application.yml配置文件:对服务器端口号、mysql驱动、mtybatis的mapper进行配置
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/whlg?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: xyj123
mybatis:
type-aliases-package: com.zr.bean
mapper-locations: classpath:mapper/*.xml
实现
(1)从Springboot3application类中的main方法启动
@SpringBootApplication
public class Springboot3Application {
public static void main(String[] args) {
SpringApplication.run(Springboot3Application.class,args);
}
}
(2)在dao中创建相应的接口
@Mapper
public interface UserDao {
List<User> findAll();
}
(3)在UserMapper中实现Dao接口,也就是对数据库进行操作
<?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="com.zr.dao.UserDao">
<select id="findAll" resultType="user">
select * from tb_user
</select>
</mapper>
(4)在service中声明、实现服务的接口
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> findAll(){
return userDao.findAll();
}
}
(5)Controller层HelloSpringboot类,控制页面的跳转
@Controller
@ResponseBody
@RequestMapping("hello")
public class HelloSpringboot {
@Autowired
private UserService userService;
@RequestMapping("s")
public String hello(){
return "hello springboot";
}
@RequestMapping("find")
public String find(){
List<User> all = userService.findAll();
return all.toString();
}
}
运行结果
批量删除
springboot——直接输入:http://localhost:8081/hello/s
springboot——直接输入:http://localhost:8081/hello/find