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

时间:2024-04-25 20:47:44

任务:

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进行添加、删除、修改、更新的操作。