高级Web技术实验一
掌握 MyBatis 的环境搭建、核⼼配置⽂件、映射⽂件,学会使⽤基于 xml 和基于注解的 MyBatis 进⾏关 系数据库的增删查改操作。
实验要求
1,技术选型:Java + Spring + MyBatis;推荐使⽤的数据库为 MySQL,引擎为 innoDB 存储引擎;
2, 实验结果在 Test 包中创建测试类,将实验结果直接输出到控制台,暂时不需要使⽤前端⻚⾯进⾏展示;
3,提交实验报告,项⽬代码推荐提交⾄ Github 并在实验报告中给出项⽬的仓库地址。
实验过程
在mysql中建立mybatis库以及两张表
1.我们通过Navicat建立mabatis_test库:

2.在navicat中操作,建立题目要求的两张表格:
c_course:

c_school:

数据库的建表工作已经完成,下面就是对spring以及mybatis进行配置!
配置mybatis
1.通过idea创建maven项目,在resources文件夹中创建 mybatis-config.xml核心配置文件!

2.建立pojo与mapper
pojo对应实体类,里面有属性与get set toString等方法。
其中CourseMapper.xml:

3.测试结果:
插入课程:


查询id=2的课程信息:

查询所有计算机学院开设的课程信息:

将id=4这门课程的课时数修改为32+8=40:


输出所有的学院开设的课程信息:

实验代码
CourseMapper:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.yaning.mybatis.mapper;
import com.yaning.mybatis.pojo.c_course;
import java.util.List;
public interface CourseMapper { int insertCourse(); c_course selectCourse(); List<c_course> selectAllcourseByCS(); int updateCourse(); List<c_course> selectAllCourse(); }
|
CourseMapper.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybtis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yaning.mybatis.mapper.CourseMapper"> <insert id="insertCourse"> insert into c_course values(5,'大数据存储','32','1') </insert> <!-- 查询必须设计resultType或resultmap--> <!-- 字段名和属性名不一致用resultmap--> <select id="selectCourse" resultType="com.yaning.mybatis.pojo.c_course"> select * from c_course where id = 2 </select>
<select id="selectAllcourseByCS" resultType="com.yaning.mybatis.pojo.c_course"> select * from c_course where schools='1' </select>
<update id="updateCourse"> update c_course set hours = 40 where id =4 </update>
<select id="selectAllCourse" resultType="com.yaning.mybatis.pojo.c_course"> select * from c_course </select> </mapper>
|