Spring boot Demo Series (3): Spring web + mybatis plus

Time:2020-9-16

1 Overview

Spring Web+MyBatis PlusOne of theDemoThe content is similar to the previous one, so the focus is onMyBatis Plushere.

2 daolayer

MyBatis PlusCompared withMyBaitsCan simplify a lot of configuration, can use code generator to generate quicklyEntityMapperAnd the code of each module. In addition, for ordinaryCRUDTwo interface implementations are provided

  • ISerivce<T>
  • BaseMapper<T>

The simplest of themIService<T>OfCRUDThe interface is as follows:

  • save(T entity): insert, return Boolean
  • saveOrUpdate(T entity): insert or update, return Boolean
  • removeById(Serializable id): delete, return Boolean
  • updateById(Serializable id): update, return Boolean
  • getById(Serializable id): query, returnT
  • list(): query all, returnList<T>

The above is the operation method based on the primary key, and there are also the methods based on the primary keyWrapperPlease check the official documents for details.

The simplestBaseMapper<T>OfCRUDThe interface is as follows:

  • insert(T eneity): insert, returnint
  • deleteById(Serializable id): delete, returnint
  • updateById(T entity): update, returnint
  • selectById(Serializable id): query, returnT

The same can be said for the same reasonWrapperPlease check the official website for details. The following demos demonstrate the implementation of these two methodsDemo

2.1 BaseMapper<T>

BaseMapper<T>The implementation mode ofIService<T>To be relatively simple, you need an inheritance firstBaseMapper<T>Interface:

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

Then, it directly injects and uses:

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusMapper {
    private final UserMapper mapper;
    public boolean save(User user)
    {
        if(mapper.selectById(user.getId()) != null)
            return mapper.updateById(user) == 1;
        return mapper.insert(user) == 1;
    }

    public boolean delete(String id)
    {
        return mapper.deleteById(id) == 1;
    }

    public User select(String id)
    {
        return mapper.selectById(id);
    }

    public List<User> selectAll()
    {
        return mapper.selectList(null);
    }
}

becauseinsert/updateById/deleteByIdIt’s all backint, indicatingSQLThe number of rows affected by the statement operation. Since all operations are on a single entity, the return value and the1Judgment can tell whether the operation is successful.

2.2 IService<T>

You also need to create an interface and inherit itIService<T>

public interface UserService extends IService<User> {
}

Then business class inheritanceServiceImpl<UserMapper,User>And realizeUserServiceThis oneUserMapperIt’s up thereUserMapper

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusIService extends ServiceImpl<UserMapper,User> implements UserService {
    public boolean save(User user)
    {
        return saveOrUpdate(user);
    }

    public boolean delete(String id)
    {
        return removeById(id);
    }

    public User select(String id)
    {
        return getById(id);
    }

    public List<User> selectAll()
    {
        return list();
    }
}

becauseremove/saveOrUpdateBoth return Boolean values, so you don’t need toBaseMapperThe return value is the same as1Yes.

3 Controllerlayer

TwoController, respectivelyIService<T>as well asBaseMapper<T>

@RestController
@RequestMapping("/iservice")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusIServiceController {
    private final MyBatisPlusIService myBatisPlusIService;
    @GetMapping("select")
    public User select1(@RequestParam String id)
    {
        return myBatisPlusIService.select(id);
    }

    @GetMapping("select/{id}")
    public User select2(@PathVariable("id") String id)
    {
        return myBatisPlusIService.select(id);
    }

    @GetMapping("selectAll")
    public List<User> selectAll()
    {
        return myBatisPlusIService.selectAll();
    }

    @GetMapping("delete")
    public boolean delete1(@RequestParam String id)
    {
        return myBatisPlusIService.delete(id);
    }

    @GetMapping("delete/{id}")
    public boolean delete2(@PathVariable("id") String id)
    {
        return myBatisPlusIService.delete(id);
    }

    @PostMapping("save")
    public boolean save(@RequestBody User user)
    {
        return myBatisPlusIService.save(user);
    }
}
@RestController
@RequestMapping("/mapper")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusMapperController {
    private final MyBatisPlusMapper myBatisPlusMapper;
    @GetMapping("select")
    public User select1(@RequestParam String id)
    {
        return myBatisPlusMapper.select(id);
    }

    @GetMapping("select/{id}")
    public User select2(@PathVariable("id") String id)
    {
        return myBatisPlusMapper.select(id);
    }

    @GetMapping("selectAll")
    public List<User> selectAll()
    {
        return myBatisPlusMapper.selectAll();
    }

    @GetMapping("delete")
    public boolean delete1(@RequestParam String id)
    {
        return myBatisPlusMapper.delete(id);
    }

    @GetMapping("delete/{id}")
    public boolean delete2(@PathVariable("id") String id)
    {
        return myBatisPlusMapper.delete(id);
    }

    @PostMapping("save")
    public boolean save(@RequestBody User user)
    {
        return myBatisPlusMapper.save(user);
    }
}

4 others

4.1 entity class

@Getter
@Setter
@AllArgsConstructor
public class User {
    private String id;
    private String username;
    private String password;
    @Override
    public String toString()
    {
        return "id:"+id+"\nusername:"+username+"\npassword:"+password+"\n";
    }
}

4.2 configuration class

Configuration class is mainly to add a@MapperScan

@Configuration
@MapperScan("com.example.demo.dao")
public class MyBatisPlusConfig {
}

4.3 configuration file

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: test
    password: test

Modify as needed.

4.4 database

SQLThe file is in the source link.

5 test

The test runs directlytestThe file under the directory is enough. The author has done two simple tests

Spring boot Demo Series (3): Spring web + mybatis plus

Spring boot Demo Series (3): Spring web + mybatis plus

6 source code

JavaEdition:

KotlinEdition:

  • Github
  • Code cloud