使用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>
server:
port: 8005
spring:
application:
name: postgres
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/test
username: postgres
password: root
@SpringBootApplication
@MapperScan("com.test.postgre.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
@Data
@Table(name = "user")
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
}
@RestController
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("findAll")
public ResponseEntity findAll(){
return userService.findAll();
}
}
@Repository
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM public.\"user\"")
List<User> selectAll();
}
@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,控制台报错如下
使用mubatisplus的封装SQL提示id不存在,
然后改用自己写sql:
@Repository
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM public.\"user\"")
List<User> selectAll();
}
sql是按照postgresql的可视化工具-pgAdmin4中查询sql复制过来的,
重启服务,发现接口返回成功
1.为啥使用mybatisplus的封装语句不行?少了什么吗,如果是mysql,这样的流程是没问题的,有大佬指点一下吗?
@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);
}
public interface UserService {
ResponseEntity findAll();
ResponseEntity add(User user);
ResponseEntity update(User user);
ResponseEntity delete(Integer id);
}
@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("删除成功");
}
@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);
测试删除时,在可视化页面pgAdmin4中同样的sql,删除成功,但是在代码中执行时,删除记录为0条,求助大佬解答!!!