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

XML中的通用代码模板



项目列表:
新增,查询用户,分页查询用户:后端Java代码:前后端分离_第1张图片

新增

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中:




    
        insert into user(username,usernick,userpasd) values (#{username},#{usernick},#{userpasd})
    

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;
    }
}

你可能感兴趣的:(JAVA,前后端分离,java,spring,maven,springboot)