采用Spring+SpringMVC+Mybatis技术框架开发一个培训班学员信息管理模块,实现以下功能: (1)增删改查培训班信息,当培训班还有学习的学员时,不能删除培训班信息; (2)增删改查学员信息,能够上传学员照片; (3)按培训班名称查询培训班信息,支持培训班名称模糊查询和分页显示; (4)按学员名查询学员信息,支持学员名模糊查询和分页显示;代码
时间: 2024-02-04 13:04:00 浏览: 87
好的,下面是一个简单的培训班学员信息管理模块的代码示例。该示例使用Spring+SpringMVC+Mybatis技术框架进行开发,并实现了上述要求的所有功能。
1. 创建数据库表
首先,您需要创建两个数据库表,一个是"training_class"表,另一个是"student"表,用于存储培训班和学员的信息。"training_class"表中包含培训班的名称、开始时间、结束时间等信息。"student"表中包含学员的姓名、性别、年龄、联系方式、照片等信息,并且其中的培训班ID字段与"training_class"表中的ID字段关联。
2. 创建Java实体类
然后,您需要创建两个Java实体类,一个是"TrainingClass"类,另一个是"Student"类,分别对应上述两个数据库表。这里只展示了"TrainingClass"类的代码,"Student"类的代码类似。
```java
public class TrainingClass {
private int id;
private String name;
private Date startTime;
private Date endTime;
private List<Student> students;
// 省略getter和setter方法
}
```
3. 创建Mapper接口和XML文件
接下来,您需要创建两个Mapper接口,一个是"TrainingClassMapper"接口,另一个是"StudentMapper"接口,分别对应上述两个数据库表。在这些接口中,您需要定义一些基本的CURD操作,例如添加、删除、修改和查询培训班和学员信息的方法。然后,您需要创建两个对应的XML配置文件,分别与这些Mapper接口相关联,并在其中定义一些SQL语句来实现这些CURD操作。这里只展示了"TrainingClassMapper"接口和XML文件的代码,"StudentMapper"类似。
```java
public interface TrainingClassMapper {
void insertTrainingClass(TrainingClass trainingClass);
void deleteTrainingClassById(int id);
void updateTrainingClass(TrainingClass trainingClass);
TrainingClass selectTrainingClassById(int id);
List<TrainingClass> selectAllTrainingClasses();
List<TrainingClass> selectTrainingClassesByName(String name);
int countTrainingClasses();
List<TrainingClass> selectTrainingClassesByPage(@Param("start") int start, @Param("pageSize") int pageSize);
}
```
```xml
<mapper namespace="com.example.mapper.TrainingClassMapper">
<insert id="insertTrainingClass" parameterType="com.example.entity.TrainingClass">
insert into training_class (name, start_time, end_time) values (#{name}, #{startTime}, #{endTime})
</insert>
<delete id="deleteTrainingClassById" parameterType="int">
delete from training_class where id = #{id}
</delete>
<update id="updateTrainingClass" parameterType="com.example.entity.TrainingClass">
update training_class set name = #{name}, start_time = #{startTime}, end_time = #{endTime} where id = #{id}
</update>
<select id="selectTrainingClassById" parameterType="int" resultType="com.example.entity.TrainingClass">
select * from training_class where id = #{id}
</select>
<select id="selectAllTrainingClasses" resultType="com.example.entity.TrainingClass">
select * from training_class
</select>
<select id="selectTrainingClassesByName" parameterType="String" resultType="com.example.entity.TrainingClass">
select * from training_class where name like concat('%', #{name}, '%')
</select>
<select id="countTrainingClasses" resultType="int">
select count(*) from training_class
</select>
<select id="selectTrainingClassesByPage" parameterType="map" resultType="com.example.entity.TrainingClass">
select * from training_class limit #{start}, #{pageSize}
</select>
</mapper>
```
4. 创建Service类
接下来,您需要创建两个Service类,一个是"TrainingClassService"类,另一个是"StudentService"类,用于封装Mapper接口中定义的方法,并提供一些业务逻辑的处理。例如,在"TrainingClassService"类中,您需要编写一些业务逻辑代码,例如判断删除培训班信息时是否还有学员在学习,以及处理查询结果的分页显示等。这里只展示了"TrainingClassService"类的代码,"StudentService"类似。
```java
@Service
public class TrainingClassService {
@Autowired
private TrainingClassMapper trainingClassMapper;
public void insertTrainingClass(TrainingClass trainingClass) {
trainingClassMapper.insertTrainingClass(trainingClass);
}
public void deleteTrainingClassById(int id) throws Exception {
TrainingClass trainingClass = trainingClassMapper.selectTrainingClassById(id);
if (trainingClass != null && trainingClass.getStudents().size() > 0) {
throw new Exception("该培训班还有学员在学习,不能删除!");
}
trainingClassMapper.deleteTrainingClassById(id);
}
public void updateTrainingClass(TrainingClass trainingClass) {
trainingClassMapper.updateTrainingClass(trainingClass);
}
public TrainingClass selectTrainingClassById(int id) {
return trainingClassMapper.selectTrainingClassById(id);
}
public List<TrainingClass> selectAllTrainingClasses() {
return trainingClassMapper.selectAllTrainingClasses();
}
public List<TrainingClass> selectTrainingClassesByName(String name) {
return trainingClassMapper.selectTrainingClassesByName(name);
}
public int countTrainingClasses() {
return trainingClassMapper.countTrainingClasses();
}
public List<TrainingClass> selectTrainingClassesByPage(int currentPage, int pageSize) {
int start = (currentPage - 1) * pageSize;
return trainingClassMapper.selectTrainingClassesByPage(start, pageSize);
}
}
```
5. 创建Controller类
然后,您需要创建两个Controller类,一个是"TrainingClassController"类,另一个是"StudentController"类,用于处理HTTP请求,并调用相应的Service类中的方法。例如,在"TrainingClassController"类中,您需要编写一些处理增删改查培训班信息的方法,并将请求转发到相应的页面。这里只展示了"TrainingClassController"类的代码,"StudentController"类似。
```java
@Controller
@RequestMapping("/trainingClass")
public class TrainingClassController {
@Autowired
private TrainingClassService trainingClassService;
@RequestMapping("/list")
public String list(Model model, @RequestParam(required = false, defaultValue = "1") int currentPage,
@RequestParam(required = false, defaultValue = "5") int pageSize) {
List<TrainingClass> trainingClasses = trainingClassService.selectTrainingClassesByPage(currentPage, pageSize);
int totalCount = trainingClassService.countTrainingClasses();
int totalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
model.addAttribute("trainingClasses", trainingClasses);
model.addAttribute("totalCount", totalCount);
model.addAttribute("totalPages", totalPages);
model.addAttribute("currentPage", currentPage);
model.addAttribute("pageSize", pageSize);
return "trainingClassList";
}
@RequestMapping("/add")
public String add(Model model) {
model.addAttribute("trainingClass", new TrainingClass());
return "trainingClassForm";
}
@RequestMapping("/save")
public String save(TrainingClass trainingClass) {
if (trainingClass.getId() == 0) {
trainingClassService.insertTrainingClass(trainingClass);
} else {
trainingClassService.updateTrainingClass(trainingClass);
}
return "redirect:/trainingClass/list";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") int id, RedirectAttributes redirectAttributes) {
try {
trainingClassService.deleteTrainingClassById(id);
} catch (Exception e) {
redirectAttributes.addFlashAttribute("message", e.getMessage());
}
return "redirect:/trainingClass/list";
}
@RequestMapping("/edit/{id}")
public String edit(Model model, @PathVariable("id") int id) {
TrainingClass trainingClass = trainingClassService.selectTrainingClassById(id);
model.addAttribute("trainingClass", trainingClass);
return "trainingClassForm";
}
@RequestMapping("/search")
public String search(Model model, @RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") int currentPage,
@RequestParam(required = false, defaultValue = "5") int pageSize) {
List<TrainingClass> trainingClasses = trainingClassService.selectTrainingClassesByName(name);
int totalCount = trainingClasses.size();
int totalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
int start = (currentPage - 1) * pageSize;
int end = Math.min(start + pageSize, totalCount);
model.addAttribute("trainingClasses", trainingClasses.subList(start, end));
model.addAttribute("totalCount", totalCount);
model.addAttribute("totalPages", totalPages);
model.addAttribute("currentPage", currentPage);
model.addAttribute("pageSize", pageSize);
model.addAttribute("name", name);
return "trainingClassList";
}
}
```
6. 创建JSP页面
最后,您需要创建一些JSP页面,用于展示培训班和学员的信息,并提供一些操作的界面。例如,您可以创建一个名为"trainingClassList.jsp"的页面,用于展示所有的培训班信息,并提供一些增删改查的操作按钮。另外,您还可以创建一个名为"studentList.jsp"的页面,用于展示所有的学员信息,并提供上传照片的功能。这里只展示了"trainingClassList.jsp"页面的代码。
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>培训班列表</title>
</head>
<body>
<h1>培训班列表</h1>
<p>
<a href="${pageContext.request.contextPath}/trainingClass/add">添加培训班</a>
</p>
<form action="${pageContext.request.contextPath}/trainingClass/search" method="get">
<label>按培训班名称查询:</label>
<input type="text" name="name" value
阅读全文