简单使用"/>
MP的简单使用
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
maven依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version>
</dependency>
mapper接口
public interface UserMapper extends BaseMapper<User> {
}
springboot启动类
@SpringBootApplication
@MapperScan("com.luo.springboot01.dao")
public class Springboot01Application {public static void main(String[] args) {SpringApplication.run(Springboot01Application.class, args);}
}
controller层
@Autowired
private UserMapper mapper;//按条件查找,为null就查询全部
@RequestMapping("/select")public List selectOnConditional(User user){List<User> list = mapper.selectList(new QueryWrapper<>(user));return list;}//通过id查询数据
@RequestMapping("/one/{id}")public User selectById(@PathVariable("id")long id){return mapper.selectById(id);
}//通过id删除数据
@RequestMapping("/one/{id}")public int deleteById(@PathVariable("id")long id){int i = mapper.deleteById(id);return i>0?200:-1;}//批量删除
@RequestMapping("/deleteBatchIds")public int deidas(int[] ids){int i = mapper.deleteBatchIds(Arrays.asList(ids));return i>0?200:-1;}//增加对象
@RequestMapping("/add")public int add(User user){int i = mapper.insert(user);return i>0?200:-1;}
//更新
@RequestMapping("/update")public int updateById(User user){int i = mapper.updateById(user);return i>0?200:-1;}
更多推荐
MP的简单使用
发布评论