[]()目标* 利用MybatisPlus提供的IService接口和ServiceImpl类更方便的创建自定义Service;
[]()方法* 创建UserService接口并继承在IService;
import com.baomidou.mybatisplus.extension.service.IService;import edu.sctu.demo.mybatis.plus.model.UserEntity;public interface UserService extends IService<UserEntity> {}
- 创建接口的实现类UserServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import edu.sctu.demo.mybatis.plus.mapper.UserMapper;import edu.sctu.demo.mybatis.plus.model.UserEntity;import edu.sctu.demo.mybatis.plus.service.UserService;import org.springframework.stereotype.Service;@Servicepublic class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity>
implements UserService {}
- 使用UserService提供的更加丰富的功能;
@SpringBootApplicationpublic class Application implements CommandLineRunner {
@Autowired(required = false)
private UserMapper userMapper;
@Autowired
private UserService userService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(userMapper.selectList(null));
// 带条件的查询
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "chen");
System.out.println(userMapper.selectList(queryWrapper));
System.out.println(userService.count());
}}
[]()结语本文介绍了利用MybatisPlus提供的IService接口以及ServiceImpl通用类提供的丰富的方法,更加便捷的用于操作数据库。