目录
Mybatis-plus可以说是Mybatis的增强版,增强有以下几点:
1、相比较于Mybatis可以生成service,serviceImpl,controller层的代码,减少代码工作量。
2、service层实现了通用的CURD(增删改查功能),减少了用户人员的代码量,避免了大量的重复代码。
依赖环境集成
注意事项:
1.mybatis-plus和mybatis-plus-generator版本必须一致 。
2.mybatis-plus 和 springboot 的版本最好对应,可以上https://mvnrepository.com/搜索mybatis-plus-boot-starter,然后点击对应的版本,查看对应的springboot版本。
导入依赖:
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-generatorartifactId>
- <version>3.2.0version>
- dependency>
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.2.0version>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.8version>
- dependency>
- mybatis-plus:
- mapper-locations:
- # xml文件的路径
- - classpath*:mapper/*.xml
- # 实体类的包路径
- type-aliases-package: com.cw.bean
@MapperScan({"mapper接口所在的包名"}),可以多个。
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class TestUserMapper {
-
- @Autowired
- private UserMapper userMapper;
-
- @Test
- public void testInsert(){
- User user=new User();
- user.setAge(12);
- user.setName("曹操");
- user.setPassword("123");
- user.setMail("caocao@qq.com");
- user.setUserName("曹操");
- user.setAddress("北京");
- //result数据库受影响的行数
- int result = userMapper.insert(user);
- System.out.println("result=>"+result);
- //获取自增长后的id值
- System.out.println(user.getId());//自增后的id会回填到对象中
- }
- }
- @Test
- public void testDeleteById() {
- //执行删除操作
- int result = this.userMapper.deleteById(6L);
- System.out.println("result = " + result);
- }
- @Test
- public void testDeleteByMap() {
- Map
columnMap = new HashMap<>(); - columnMap.put("age",20); columnMap.put("name","张三");
-
- //将columnMap中的元素设置为删除的条件,多个之间为and关系
- int result = this.userMapper.deleteByMap(columnMap);
- System.out.println("result = " + result);
- }
- @Test public void testDeleteByMap() {
- User user = new User();
- user.setAge(20);
- user.setName("张三");
-
- //将实体对象进行包装,包装为操作条件
- QueryWrapper
wrapper = new QueryWrapper<>(user); - int result = this.userMapper.delete(wrapper);
- System.out.println("result = " + result);
- }
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class UserMapperTest {
-
- @Autowired private UserMapper userMapper;
-
- @Test
- public void testUpdateById() {
- User user = new User(); user.setId(6L); //主键
- user.setAge(21); //更新的字段
- //根据id更新,更新不为null的字段
- this.userMapper.updateById(user);
- }
-
- }
- @Test public void testUpdate() {
- User user = new User(); user.setAge(22); //更新的字段
- //更新的条件
- QueryWrapper
wrapper = new QueryWrapper<>(); - wrapper.eq("id", 6);
- //执行更新操作
- int result = this.userMapper.update(user, wrapper);
- System.out.println("result = " + result);
- }
- @Test
- public void testSelectById() {
- //根据id查询数据
- User user = this.userService.getById(2L);
- System.out.println("result = " + user);
- }
- @Test
- public void testSelectBatchIds() {
- //根据id集合批量查询
- List
users = this.userService.listByIds(Arrays.asList(2L, 3L, 10L)); - for (User user : users) {
- System.out.println(user);
- }
- }
- @Test public void testList() {
- QueryWrapper
wrapper = new QueryWrapper(); - wrapper.eq("name", "李四");
- List
user = this.userMapper.list(wrapper); - }
- @Test public void testSelectOne() {
- QueryWrapper
wrapper = new QueryWrapper(); - wrapper.eq("name", "李四");
- User user = this.userService.getOne(wrapper);
- }