初学者idea2020+SpringBoot + MySQL的完整小项目(续)(超详细)

承接上一篇博客,博客地址:https://blog.csdn.net/qq_41115379/article/details/109906004
代码书写,插入数据
首先在UserMapper里加入一个插入的sql代码:

package com.example.springboot_nstart.mapper;

import com.example.springboot_nstart.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

//这个就是和数据库来做对应操作id,而且这居然是一个接口
//我先试试看不叫 component这个注解
@Component
public interface UserMapper {
    //然后做一些数据库的对应操作,这次是查询操作
    @Select("select * from usertable order by user_id")
    List<User> getUserList();
    //这边再多一个插入数据的操作
    @Insert("insert into usertable(username,userpassword,age,sex,user_id) values(#{username},#{userpassword},#{age},#{sex},#{user_id})")
    int AddUser(User user);
}

并在UserService里新增插入的函数

package com.example.springboot_nstart.service;

import com.example.springboot_nstart.pojo.User;
import org.springframework.stereotype.Service;

import java.util.List;
//也是没有写对应的注解的,到时候可以改一下
@Service
public interface UserService {
    //和mapper相呼应,有一个对应的方法
    List<User> getUserList();
    //这里也要写对应的方法,但是不是int,而是其他的参数类型
    String AddUser(User user);

}

再修改UserServiceImpl函数:

package com.example.springboot_nstart.service.impl;

import com.example.springboot_nstart.mapper.UserMapper;
import com.example.springboot_nstart.pojo.User;
import com.example.springboot_nstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
//再网上看到 好像是 service和controller和compent 还有这个 都是没有本质区别的
@Repository
public class UserServiceImpl implements UserService {
    //这里就是需要把这个实现方法具体的写出来了,然后这里就需要加入注解了
    //这就是所谓的自动装配的功能吧
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getUserList() {
        try{
            //就是因为这些调用来调用去的,让我觉得很绕,我姑且认为是,需要提高系统的可调度性
            //所以要做各种分层的操作,减少耦合度,这里的话,相当于就是要用具体操作了,可是其实就是把mapper的方法调用一些
            List<User> users= userMapper.getUserList();
            return users;

        }catch (Exception e){
            //好像是 如果不throw的话,会报错。。。
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public String AddUser(User user) {
        //这里也是调用对应的方法就行了
        try{
            //add方法返回的是int,但这个方法返回的是String
            int i=userMapper.AddUser(user);
            return "插入了"+i+"条";

        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

}

并在UserController中修改:

package com.example.springboot_nstart.controller;

import com.example.springboot_nstart.pojo.User;
import com.example.springboot_nstart.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//因为controller的作用相当于是用来跳转到别的页面的,而不是返回当前页面的值,如果需要返回当前页面,相当于就是加了一个responsebody的限制
//而restContorller 是用来返回当前页面的东西的,但是不能实现跳转到其他页面的功能
@RestController
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("/getuserlist")
    public List<User> getUserList(){
        //再出现了service层之后,这个方法就可以在做对应的修改了,也就是调用了这个方法,参数也是需要修改一下的
        return userService.getUserList();
    }

    @RequestMapping("/addUser")
    public String addUser(User user){
        return userService.AddUser(user);
    }

}

这里和原文做了一个修改:因为用PostMapping会报错,索性就改成了RequestMapping。https://blog.csdn.net/qq_41973208/article/details/85008962

然后就是运行application代码
这里重点关注:就像我上篇博客说的,我没有把user_id改成自增,所以他这个属性也是自己输入的,和原博客之间有出入,所以会有两种不同的情况发生
1.如果将user_id修改成自动增加了,那么基本上代码可以不用动,直接输入:
http://localhost:8080/adduser和参数
例如:

http://localhost:8080/addUser?username=小白&userpassword=727&age=19&sex=

2.如果user_id不是自动增加的,那么需要在user这个类里面,创建一个 int user_id ,并写入他的set和get方法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
再输入:
http://localhost:8080/adduser和参数(包含user_id)
例如:

http://localhost:8080/addUser?username=小白&userpassword=727&age=19&sex=&user_id=9

结果显示:
在这里插入图片描述
查看数据库
在这里插入图片描述
over

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值