一、将前面的代码继续完善功能
1、采用XML映射文件的形式来映射sql语句;
2、采用动态sql语句的方式,实现条件查询的分页。
ScreSessionXmlController
package com.example.Controller;
import com.example.Service.ScreSessionService;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.SreSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/scres")
public class ScreSessionXmlController {
@Autowired
private ScreSessionService screSessionService;
//查询全部,返回的是Result类型的json数据。
@GetMapping("/scresessionfindAllJson")
public Result findAllJson() {
return Result.success(screSessionService.findAll());
}
//按id删除
@GetMapping("/deleteScreSession")
public void delete(@RequestParam("sessionid") Integer sessionid) {
screSessionService.deleteScreSession(sessionid);
}
//按id查找
@GetMapping("/sessionfindById/{sessionid}")
public Result scresessionfindById(@PathVariable("sessionid") Integer sessionid) {
return Result.success(screSessionService.scresessionfindById(sessionid));
}
//增加数据
@GetMapping("/scresessionadd")
public Result addUser(@RequestBody SreSession sreSession) {
boolean result = screSessionService.addUser(sreSession);
if (result) {
//成功 code==1;
return Result.success();
} else {
return Result.error("添加失败");
}
}
//修改数据
@GetMapping("/editScreSession")
public Result editScreSession(@RequestBody SreSession sreSession) {
boolean r = screSessionService.editScreSession(sreSession);
if (r) {
//成功 code==1;
return Result.success();
} else {
return Result.error("更新失败");
}
}
@GetMapping("/scresession/{page}/{pageSize}")
public Result findAll(@PathVariable Integer page,
@PathVariable Integer pageSize) {
PageBean pageBean = screSessionService.list(page, pageSize);
return Result.success(pageBean);
}
@GetMapping("scresession1/{page}/{pageSize}")
public Result findAll_chaxun(@PathVariable Integer page,
@PathVariable Integer pageSize,
Integer sessionid,
Integer movieid
){
PageBean pageBean= screSessionService.list_chaxun(page,pageSize,sessionid,movieid);
return Result.success(pageBean);
}
}
ScreSessionMapper1
package com.example.Mapper;
import com.example.pojo.SreSession;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface ScreSessionMapper1 {
public List<SreSession> list_chaxun(@Param("sessionid") Integer sessionid, @Param("movieid") Integer movieid);
public List<SreSession> list();
public List<SreSession> findAll();
public int deleteScreSession(Integer sessionid);
public SreSession ScreSessionfindById(Integer sessionid);
public int add(SreSession sreSession);
public boolean editScreSession(SreSession sreSession);
}
ScreSessionMapperXml
<?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.example.mapper.ScreSessionMapper1">
<!-- 查询 -->
<select id="list" resultType="com.example.pojo.SreSession">
select * from screeningsession
</select>
<!-- 条件分页查询 -->
<select id="list_chaxun" resultType="com.example.pojo.SreSession">
select * from screeningsession
<where>
<if test="sessionid != null and sessionid != ''">
name like concat('%',#{sessionid},'%')
</if>
<if test="movieid != null and movieid != ''">
and movieid like concat('%',#{movieid},'%')
</if>
</where>
</select>
<!-- findAll -->
<select id="findAll" resultType="com.example.pojo.SreSession">
select * from screeningsession
</select>
<!-- 删除 -->
<delete id="deleteScreSession" parameterType="java.lang.Integer">
DELETE FROM screeningsession WHERE sessionid = #{sessionid}
</delete>
<!-- 增加-->
<select id="ScreSessionfindById" parameterType="java.lang.Integer" resultType="com.example.pojo.SreSession">
SELECT * FROM screeningsession WHERE sessionid = #{sessionid}
</select>
<!-- 添加一个SreSession -->
<insert id="add" parameterType="com.example.pojo.SreSession">
INSERT INTO screeningsession (sessionid, movieid,theaterhallid,screeningdatetime,ticketprice,seatcount) VALUES (#{sessionid}, #{movieid}, #{theaterhallid},#{screeningdatetime},#{ticketprice},#{seatcount},)
</insert>
<!-- 更新一个SreSession -->
<update id="editScreSession" parameterType="com.example.pojo.SreSession">
UPDATE screeningsession
SET sessionid = #{sessionid}, movieid = #{movieid}, theaterhallid = #{theaterhallid}, screeningdatetime = #{screeningdatetime},ticketprice = #{ticketprice},seatcount = #{seatcount},
WHERE sessionid = #{sessionid}
<!-- 注意:这里需要根据SreSession的可更新属性和数据库表的实际列进行替换 -->
</update>
</mapper>
前端代码的方法
methods: {
handleSizeChange(val) {
this.pageSize = val;
this.currentPage = 1; // 通常改变每页大小后,回到第一页
this.findAll();
console.log(`每页 ${val} 条`);
},
handleCurrentChange(val) {
this.currentPage = val;
this.findAll();
console.log(`当前页: ${val}`);
},
handleDelete(row) {
if (window.confirm("确定要删除该条数据吗???")) {
axios.delete(`/scres/deleteScreSession?sessionid=${row.sessionid}`) // 假设row有一个SessionID属性
.then((response) => {
alert("删除成功");
this.findAll(); // 假设这个方法会从后端获取数据并更新UI
})
.catch((error) => {
console.error(error);
alert("删除失败,请稍后再试:" + error.message); // 显示更详细的错误信息
});
}
},
findAll() {
var url = `/scres/scresession/${this.currentPage}/${this.pageSize}`
axios.get(url)
.then(res => {
this.tableData = res.data.data.rows; // 假设这里是实际数据的数组
this.total = res.data.data.total;
console.log(this.tableData);
console.log(this.total);
}).catch(error => {
console.error(error);
});
},
onSubmit() {
var url = `/scres/scresession1/${this.currentPage}/${this.pageSize}?sessionid=${encodeURIComponent(this.formInline.sessionid)}&movieid=${encodeURIComponent(this.formInline.movieid)}`
console.log(this.formInline.sessionid);
console.log(this.formInline.movieid);
axios.get(url)
.then(res => {
this.tableData = res.data.data.rows;
this.total = res.data.data.total;
console.log(this.tableData);
console.log(this.total);
}).catch(err => {
console.error(err);
})
},
二 gitee的初步使用
团队gitee仓库