2022/11/13 mybatisplus swagger vue分离

package com.example.demos.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "sys_user")
public class User {
    @TableId
    private Integer id;
    private String username;
    @JsonIgnore
    private String password;
    private String nickname;
    private String email;
    private String phone;
    private String address;
}

package com.example.demos.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demos.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

//@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("select * from sys_user")
    List<User> findAll();

    @Insert("insert into sys_user(username,password,nickname,email,phone,address) values " +
            "(#{username},#{password},#{nickname},#{email},#{phone},#{address});")
    int insert(User user);

    int update(User user);

    @Delete("delete from sys_user where id = #{id}")
    Integer deleteById(@Param("id") Integer id);

    @Select("select * from sys_user limit #{pageNum},#{pageSize}")
    List<User> selectPage(Integer pageNum,  Integer pageSize);


    @Select("select count(*) from sys_user")
    Integer selectTotal();
}

package com.example.demos.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demos.entity.User;
import com.example.demos.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper,User> {
    public boolean saveUser(User user) {
        return saveOrUpdate(user);
    }
//    @Autowired
//    private UserMapper userMapper;
//
//    public int save(User user){
//        if(user.getId() == null){
//           return  userMapper.insert(user);
//        }else{
//           return  userMapper.update(user);
//        }
//    }

}

package com.example.demos.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.example.demos.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

注意swagger的版本,可能需要application.yml配置
2022/11/13 mybatisplus swagger vue分离_第1张图片
mybatisplus的缺点在于 原来的UserMapper的方法重写mybatisplus自带的
postman测试失败 路径没问题,看参数个数。参数对应
但是参数不够容易404 所以解决加默认值

    @GetMapping("/page")
    public IPage<User> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String nickname,
                                @RequestParam(defaultValue = "") String address){
        IPage<User> page  = new Page<>(pageNum,pageSize);
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("username",username);
        objectQueryWrapper.like("nickname",nickname);
        objectQueryWrapper.like("address",address);
        return userService.page(page,objectQueryWrapper);
    }

vue分离需要单独div包括起来 而不是几个独立的标签元素

你可能感兴趣的:(后端,vue.js,java,mybatis)