MybatisPlus ServiceImpl Service层简化代码,使开发更注重于业务开发。
Iservice源码这是一个接口,里面有一些增删改查的方法。
ServiceImpl原码
介绍一下使用 导入依赖
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.baomidou
mybatis-plus-boot-starter
3.1.2
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter
mysql
mysql-connector-java
5.1.47
org.projectlombok
lombok
true
1.18.4
application.yml配置文件
server:
port: 8000
spring:
application:
name: person
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/test?serverTimezone=UTC
username: root
password: root
mybatis-plus:
configuration:
map-underscore-to-camel-case: false #自动驼峰命名
type-enums-package: com.eunm
type-aliases-package: com.domain #扫描实体类
启动类
@SpringBootApplication
@MapperScan("com.dao")
public class PlusApplication {
public static void main(String[] args) {
SpringApplication.run(PlusApplication.class,args);
}
@Bean //分页插件
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
pojo实体类与数据库对应
@Data
@TableName("person")
public class Person {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private sex person_sex;
}
Mapper文件
public interface PersonMapper extends baseMapperservice文件{ }
@Service public class PersonService extends ServiceImpl测试{ }
@Autowired
private PersonService personService;
@Test
public void selectAll(){
//查询所有
List list = personService.list(null);
System.out.println(list);
System.out.println("============");
//根据id查询
Person byId = personService.getById(1);
System.out.println(byId);
System.out.println("============");
//插入数据
Person person = new Person();
person.setName("ll");
person.setAge(16);
person.setPerson_sex(sex.WOMAN);
boolean save = personService.save(person);
QueryWrapper personQueryWrapper = new QueryWrapper<>();
personQueryWrapper.eq("name",person.getName());
//删除数据
boolean remove = personService.remove(personQueryWrapper);
System.out.println(save+"=="+remove);
System.out.println("============");
//分页
IPage page = personService.page(new Page<>(1, 2));
System.out.println(page.getRecords());
}
结果展示



