基于ssm框架的运动会管理系统的设计与实现数据库代码
时间: 2023-09-20 15:08:56 浏览: 109
基于SSM的运动会管理系统
为了方便起见,我们假设我们需要设计和实现一个运动会管理系统的数据库,其中包含以下表格:
1. 学校表格(school):包含学校的ID(school_id)、学校名称(school_name)和学校所在地(school_location)。
2. 运动员表格(athlete):包含运动员的ID(athlete_id)、姓名(athlete_name)、性别(athlete_gender)、年龄(athlete_age)和所属学校(athlete_school)。
3. 项目表格(event):包含项目的ID(event_id)、项目名称(event_name)、男子比赛记录(event_record_m)、女子比赛记录(event_record_f)和比赛日期(event_date)。
4. 参赛表格(participate):包含参赛的ID(participate_id)、参赛运动员(participate_athlete)、参赛项目(participate_event)和参赛成绩(participate_score)。
下面是基于SSM框架的数据库代码实现:
1. 首先,在MySQL数据库中创建相应的表格:
CREATE TABLE `school` (
`school_id` int(11) NOT NULL AUTO_INCREMENT,
`school_name` varchar(255) NOT NULL,
`school_location` varchar(255) NOT NULL,
PRIMARY KEY (`school_id`)
);
CREATE TABLE `athlete` (
`athlete_id` int(11) NOT NULL AUTO_INCREMENT,
`athlete_name` varchar(255) NOT NULL,
`athlete_gender` varchar(255) NOT NULL,
`athlete_age` int(11) NOT NULL,
`athlete_school` int(11) NOT NULL,
PRIMARY KEY (`athlete_id`),
CONSTRAINT `athlete_school_fk` FOREIGN KEY (`athlete_school`) REFERENCES `school` (`school_id`)
);
CREATE TABLE `event` (
`event_id` int(11) NOT NULL AUTO_INCREMENT,
`event_name` varchar(255) NOT NULL,
`event_record_m` double NOT NULL,
`event_record_f` double NOT NULL,
`event_date` date NOT NULL,
PRIMARY KEY (`event_id`)
);
CREATE TABLE `participate` (
`participate_id` int(11) NOT NULL AUTO_INCREMENT,
`participate_athlete` int(11) NOT NULL,
`participate_event` int(11) NOT NULL,
`participate_score` double NOT NULL,
PRIMARY KEY (`participate_id`),
CONSTRAINT `participate_athlete_fk` FOREIGN KEY (`participate_athlete`) REFERENCES `athlete` (`athlete_id`),
CONSTRAINT `participate_event_fk` FOREIGN KEY (`participate_event`) REFERENCES `event` (`event_id`)
);
2. 在SSM框架中,我们需要使用MyBatis框架来实现数据库的访问。因此,我们需要在MyBatis的配置文件中添加相应的数据库连接信息和Mapper配置信息。
在mybatis-config.xml文件中添加以下内容:
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/sports" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/SchoolMapper.xml" />
<mapper resource="com/example/mapper/AthleteMapper.xml" />
<mapper resource="com/example/mapper/EventMapper.xml" />
<mapper resource="com/example/mapper/ParticipateMapper.xml" />
</mappers>
</configuration>
3. 在SSM框架中,我们需要使用Mapper接口来定义数据库访问的方法。因此,我们需要为每个表格创建一个Mapper接口,并在接口中定义相应的方法。
例如,在SchoolMapper接口中添加以下内容:
public interface SchoolMapper {
public School getSchoolById(int school_id);
public void addSchool(School school);
public void updateSchool(School school);
public void deleteSchool(int school_id);
}
4. 在SSM框架中,我们需要使用Mapper XML文件来实现Mapper接口中定义的方法。因此,我们需要为每个Mapper接口创建一个Mapper XML文件,并在XML文件中定义相应的SQL语句。
例如,在SchoolMapper.xml文件中添加以下内容:
<mapper namespace="com.example.mapper.SchoolMapper">
<resultMap id="SchoolResultMap" type="com.example.bean.School">
<id property="school_id" column="school_id" />
<result property="school_name" column="school_name" />
<result property="school_location" column="school_location" />
</resultMap>
<select id="getSchoolById" resultMap="SchoolResultMap" parameterType="int">
SELECT * FROM school WHERE school_id = #{school_id}
</select>
<insert id="addSchool" parameterType="com.example.bean.School">
INSERT INTO school (school_name, school_location) VALUES (#{school_name}, #{school_location})
</insert>
<update id="updateSchool" parameterType="com.example.bean.School">
UPDATE school SET school_name = #{school_name}, school_location = #{school_location} WHERE school_id = #{school_id}
</update>
<delete id="deleteSchool" parameterType="int">
DELETE FROM school WHERE school_id = #{school_id}
</delete>
</mapper>
5. 最后,在SSM框架中,我们需要使用Service和Controller层来实现业务逻辑和数据交互。因此,我们需要为每个表格创建一个Service和Controller,并在其中调用相应的Mapper方法。
例如,在SchoolService接口中添加以下内容:
public interface SchoolService {
public School getSchoolById(int school_id);
public void addSchool(School school);
public void updateSchool(School school);
public void deleteSchool(int school_id);
}
在SchoolServiceImpl实现类中添加以下内容:
@Service
public class SchoolServiceImpl implements SchoolService {
@Autowired
private SchoolMapper schoolMapper;
public School getSchoolById(int school_id) {
return schoolMapper.getSchoolById(school_id);
}
public void addSchool(School school) {
schoolMapper.addSchool(school);
}
public void updateSchool(School school) {
schoolMapper.updateSchool(school);
}
public void deleteSchool(int school_id) {
schoolMapper.deleteSchool(school_id);
}
}
在SchoolController控制器中添加以下内容:
@Controller
@RequestMapping("/school")
public class SchoolController {
@Autowired
private SchoolService schoolService;
@RequestMapping("/get/{school_id}")
public String getSchoolById(@PathVariable int school_id, Model model) {
School school = schoolService.getSchoolById(school_id);
model.addAttribute("school", school);
return "school";
}
@RequestMapping("/add")
public String addSchool(@RequestParam("school_name") String school_name, @RequestParam("school_location") String school_location) {
School school = new School();
school.setSchool_name(school_name);
school.setSchool_location(school_location);
schoolService.addSchool(school);
return "redirect:/school/list";
}
@RequestMapping("/update")
public String updateSchool(@RequestParam("school_id") int school_id, @RequestParam("school_name") String school_name, @RequestParam("school_location") String school_location) {
School school = new School();
school.setSchool_id(school_id);
school.setSchool_name(school_name);
school.setSchool_location(school_location);
schoolService.updateSchool(school);
return "redirect:/school/list";
}
@RequestMapping("/delete/{school_id}")
public String deleteSchool(@PathVariable int school_id) {
schoolService.deleteSchool(school_id);
return "redirect:/school/list";
}
}
这样,我们就完成了基于SSM框架的运动会管理系统的设计和实现数据库代码。
阅读全文