栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MybatisPlus IService Service层简化代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MybatisPlus IService Service层简化代码

MybatisPlus ServiceImpl Service层简化代码,使开发更注重于业务开发。

Iservice源码

这是一个接口,里面有一些增删改查的方法。

ServiceImpl是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 baseMapper {
}
service文件
@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());
    }

结果展示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号