乐趣区

关于java:SSM

POJO

@Data
public class User {

    private Integer id;

    private String name;

    private Integer age;
}

UserMapper

@Mapper
public interface UserMapper {List<User> selectAll();

    int insert(User user);

    int delete(Integer id);

    int update(User user);

}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zk.mapper.UserMapper">
    <select id="selectAll" resultType="com.zk.pojo.User">
        select * from user
    </select>
    <insert id="insert">
        insert into user(id,name,age) values (#{id},#{name},#{age});
    </insert>
    <delete id="delete">
        delete from user where id=#{id};
    </delete>
    <update id="update">
        update user set id=#{id},name=#{name},age=#{age} where id=#{id};
    </update>
</mapper>

UserService

public interface UserService {List<User> selectAll();

    int insert(User user);

    int delete(Integer id);

    int update(User user);
}

UserServiceImpl

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {return userMapper.selectAll();
    }

    @Override
    public int insert(User user) {return userMapper.insert(user);
    }

    @Override
    public int delete(Integer id) {return userMapper.delete(id);
    }

    @Override
    public int update(User user) {return userMapper.update(user);
    }
}

UserController

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "select")
    public List<User> selectAll(){return userService.selectAll();
    }

    @PutMapping(value = "insert")
    public String insert(@RequestBody User user){userService.insert(user);
        return "增加胜利";
    }

    @RequestMapping(value = "delete/{id}")
    public String delete(@PathVariable Integer id){userService.delete(id);
        return "删除胜利";
    }

    @PostMapping(value = "update")
    public String update(@RequestBody User user){userService.update(user);
        return "批改胜利";
    }

}
退出移动版