《web应用技术》第5次课后作业

任务:

1、在idea中配置好数据源

2、视频案例中只给出了查询所有结果的示例,请自己完成添加、删除、修改操作的代码。以下供参考。

复制代码
package com.example.pojo;

public class User {
    private Integer id;   //id(主键)
    private String name;  //姓名
    private Short age;    //年龄
    private Short gender; //性别
    private String phone; //手机号

    public User(Integer id, String name, Short age, Short gender, String phone) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
    }

    public User() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public Short getGender() {
        return gender;
    }

    public void setGender(Short gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }





}

package com.example.mapper;

import com.example.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //查询所有用户数据
    @Select("select id, name, age, gender, phone from user")
    public List<User> list();

    @Insert("insert into user(name, age, gender, phone) values (#{name}, #{age}, #{gender}, #{phone})")
    public void insert(User user);

    @Delete(" delete from user where id= #{id} ")
    public void delete(int id);

    @Select("select * from user where id= #{id} ")
    public User get(int id);

    @Update("update user set name=#{name} where id=#{id} ")
    public int update(User user);


}

package com.example;

import com.example.mapper.UserMapper;
import com.example.pojo.Peot;
import com.example.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class MybatisApplicationTests {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void listAll(){
        List<User> userList = userMapper.list();
        for (User user : userList) {
            System.out.print(user.getId()+",");
            System.out.print(user.getName()+",");
            System.out.print(user.getAge()+",");
            System.out.print(user.getGender()+",");
            System.out.println(user.getPhone());
        }
    }

    @Test
    public void update() {
        User c= userMapper.get(8);
        c.setName("wangwei");
        userMapper.update(c);
        listAll();
    }
    @Test
    public void get() {
        User c= userMapper.get(8);
        System.out.print(c.getId()+",");
        System.out.print(c.getName()+",");
        System.out.print(c.getAge()+",");
        System.out.print(c.getGender()+",");
        System.out.println(c.getPhone());
    }
    @Test
    public void delete() {
        userMapper.delete(8);
        listAll();
    }
    @Test
    public  void insert() {
        User c = new User();
        c.setName("王维");
        c.setGender((short)1);
        c.setAge((short)1);
        c.setPhone("12343234543");
        userMapper.insert(c);
        listAll();
    }


}

3、idea的sql 控制台,可以测试sql语句是否正确。所以mapper接口中的sql语句可以先测试。对于有参数的情况,用?代替。

运行sql语句时,idea会有参数输入窗口。

2、对以下案例使用mybatis进行添加、删除、修改、更新的操作。

相关推荐
lovingsoft2 小时前
Chrome Lighthouse 详解
前端·chrome
Qlly2 小时前
全栈工程师怎么接需求?从一句「加个功能」聊到能开发、能验收
前端·后端
马可家的菠萝2 小时前
你的微信“文件传输助手”,是不是已经成了第二个网盘?
前端·后端·架构
略略略啦2 小时前
Vibe Coding 基于Spec DD和TDD的实践指南
前端
shmily麻瓜小菜鸡3 小时前
前端实现图片与PDF文字识别敏感信息打码方案
前端·pdf·状态模式
打工仔折腾 AI3 小时前
LangChain 工具调用实战:从 Tool 定义到 Agent 执行链路拆解
java·前端·人工智能·后端·python·langchain·ai agent 实战
汉堡大王95273 小时前
单次输出拉到100万Token:拆完 Gemini 4 Argon 的续写协议,才看懂谷歌这7个月在憋什么
前端·人工智能·后端
看浪的路人3 小时前
第9讲:大模型应用监控与可观测性——OpenTelemetry + Prometheus + Grafana 全链路追踪
java·前端·spring
内存过载3 小时前
用表格模拟甘特图:项目排期的土办法
前端·低代码
葡萄城技术团队3 小时前
用 SpreadJS 数据验证实现省市区三级联动
前端