SpringBoot整合Mybatis实操和打印SQL语句
pringBoot2.x整合Mybatis3.x增删改查实操, 控制台打印sql语句 1、控制台打印sql语句 #增加打印sql语句,一般用于本地开发测试 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2、增加mapper代码 @Select("SELECT * FROM user") @Results({ @Result(column = "create_time",property = "createTime") //javaType = java.util.Date.class }) List<User> getAll(); @Select("SELECT * FROM user WHERE id = #{id}") @Results({ @Result(column = "create_time",property = "createTime") }) User findById(Long id);
@Update("UPDATE user SET name=#{name} WHERE id =#{id}") void update(User user);
@Delete("DELETE FROM user WHERE id =#{userId}") void delete(Long userId); 3、增加API
@GetMapping("find_all") public Object findAll(){ return JsonData.buildSuccess(userMapper.getAll()); } @GetMapping("find_by_Id") public Object findById(long id){ return JsonData.buildSuccess(userMapper.findById(id)); } @GetMapping("del_by_id") public Object delById(long id){ userMapper.delete(id); return JsonData.buildSuccess(); } @GetMapping("update") public Object update(String name,int id){ User user = new User(); user.setName(name); user.setId(id); userMapper.update(user); return JsonData.buildSuccess(); }
