新增,查询用户,分页查询用户:后端Java代码:前后端分离

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

项目列表:
在这里插入图片描述

新增

Controller:

package com.example.springboot.Controller;


import com.example.springboot.Common.User;
import com.example.springboot.Service.UserService;
import com.example.springboot.Util.Page;
import com.example.springboot.Util.R;
import com.example.springboot.Util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin("http://localhost:8082")  //跨域请求
public class UserController {
    @Autowired
    private  UserService userService;
    //插入用户信息
    @RequestMapping("/userInsert")
    public Result<?> insertUser(  User user){
        userService.insertUser(user);
        return Result.sucess();
    }
}

Service中(接口):

package com.example.springboot.Service;

import com.example.springboot.Common.User;

public interface UserService {
    void insertUser(User user);
}

ServiceImpl中:

package com.example.springboot.Service.ServiceImpl;

import com.example.springboot.Common.User;
import com.example.springboot.Mapper.UserMapper;
import com.example.springboot.Service.UserService;
import com.example.springboot.Util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;


@Service

public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void insertUser(User user) {
        System.out.println("插入用户");
        userMapper.insertUser(user);
    }
}

Mapper中:

package com.example.springboot.Mapper;

import com.example.springboot.Common.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;


@Mapper
@Repository
public interface UserMapper {
    // 插入用户
    void insertUser(User user);
}

MapperXml中:

<?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.example.springboot.Mapper.UserMapper">
    <insert id="insertUser"  parameterType="com.example.springboot.Common.User">
        insert into user(username,usernick,userpasd) values (#{username},#{usernick},#{userpasd})
    </insert>
</mapper>

user实体类:

package com.example.springboot.Common;

public class User {
    private  int id;
    private  String username;
    private String userpasd;
    private String usernick;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", userpasd='" + userpasd + '\'' +
                ", usernick='" + usernick + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpasd() {
        return userpasd;
    }

    public void setUserpasd(String userpasd) {
        this.userpasd = userpasd;
    }

    public String getUsernick() {
        return usernick;
    }

    public void setUsernick(String usernick) {
        this.usernick = usernick;
    }

    public User(){}
    public User(int id, String username, String userpasd, String usernick) {
        this.id = id;
        this.username = username;
        this.userpasd = userpasd;
        this.usernick = usernick;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值