2

高级Web技术实验(一)

 2 years ago
source link: https://1905060202.github.io/2022/04/08/%E9%AB%98%E7%BA%A7Web%E6%8A%80%E6%9C%AF%E5%AE%9E%E9%AA%8C-%E4%B8%80/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
胡小宁的博客

高级Web技术实验(一)

发表于2022-04-08|更新于2022-04-08|计算机基础
阅读量:19

高级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:

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:

<?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>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK