org.postgresql.util.PSQLException

1.使用springboot整合postgresql时报错:org.postgresql.util.PSQLException

1.1依赖

使用spring boot和postgresql搭建简单demo

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

1.2 配置postgresql数据库

server:
  port: 8005
spring:
  application:
    name: postgres
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/test
    username: postgres
    password: root

1.3 启动类

@SpringBootApplication
@MapperScan("com.test.postgre.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

1.4 实体

@Data
@Table(name = "user")
public class User {

    @TableId(type = IdType.AUTO)
    private Integer id;

    private String name;

    private Integer age;
}

1.5 controller

@RestController
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("findAll")
    public ResponseEntity findAll(){
        return userService.findAll();
    }
}

1.6 mapper

@Repository
public interface UserMapper extends BaseMapper<User> {
    @Select("SELECT * FROM public.\"user\"")
    List<User> selectAll();
}

1.7实现类

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseEntity findAll() {
        List<User> list = userMapper.selectList(null);
        //List list = userMapper.selectAll();

        log.info("list:{}",list);
        return ResponseEntity.ok(list);
    }
}

这看着没什么问题,但是启动后,调用接口http://localhost:8005/user/findAll,报500,控制台报错如下
org.postgresql.util.PSQLException_第1张图片
使用mubatisplus的封装SQL提示id不存在,
然后改用自己写sql:
org.postgresql.util.PSQLException_第2张图片

@Repository
public interface UserMapper extends BaseMapper<User> {
    @Select("SELECT * FROM public.\"user\"")
    List<User> selectAll();
}

sql是按照postgresql的可视化工具-pgAdmin4中查询sql复制过来的,
org.postgresql.util.PSQLException_第3张图片
重启服务,发现接口返回成功
在这里插入图片描述

2.疑问

1.为啥使用mybatisplus的封装语句不行?少了什么吗,如果是mysql,这样的流程是没问题的,有大佬指点一下吗?

3 补充增删改接口代码

3.1 controller

 @PostMapping("add")
    public ResponseEntity add(@RequestBody User user){
        return userService.add(user);
    }

    @PostMapping("update")
    public ResponseEntity update(@RequestBody User user){
        return userService.update(user);
    }

    @DeleteMapping("delete")
    public ResponseEntity delete(Integer id){
        return userService.delete(id);
    }

3.2 service

public interface UserService {
    ResponseEntity findAll();

    ResponseEntity add(User user);

    ResponseEntity update(User user);

    ResponseEntity delete(Integer id);
}

3.3实现类

@Override
    public ResponseEntity add(User user) {
        userMapper.insertUser(user.getName(),user.getAge());
        return ResponseEntity.ok("插入成功");
    }

    @Override
    public ResponseEntity update(User user) {
        userMapper.updateUser(user.getId(),user.getName(),user.getAge());
        return ResponseEntity.ok("修改成功");
    }

    @Override
    public ResponseEntity delete(Integer id) {
        int i = userMapper.deleteUserById(id);
        log.info("delete {}",i);
        return ResponseEntity.ok("删除成功");
    }

3.4 mapper

@Insert("INSERT INTO public.\"user\"(name, age) VALUES ( #{name}, #{age});")
    void insertUser(String name,Integer age);

    @Update("UPDATE public.\"user\" SET name=#{name}, age=#{age} WHERE id=#{id}")
    void updateUser(Integer id, String name, Integer age);

    @Delete("DELETE FROM public.\"user\" WHERE id=#{id}")
    int deleteUserById(Integer id);

3.5 疑问

测试删除时,在可视化页面pgAdmin4中同样的sql,删除成功,但是在代码中执行时,删除记录为0条,求助大佬解答!!!

你可能感兴趣的:(postgresql,java,mybatis)