02_MybatisPlus_CRUD_通用Service接口

项目结构图
在这里插入图片描述

Mapper

public interface EmployeeMapper extends BaseMapper<Employee> {
    //自定义分页、、、此处在idea种爆红,暂且不用在意,不影响执行
    IPage<Employee> getByAge(IPage page,Integer gender);
}

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tulingxueyuan.mbp.mapper.EmployeeMapper">
    <select id="getByAge" resultType="com.tulingxueyuan.mbp.pojo.Employee">
        SELECT * FROM tbl_employee where age=#{age}
    </select>
</mapper>

配置文件

# 数据源
spring:
  datasource:
    username: root
    password: admin
    url: jdbc:mysql://localhost:3306/mybatisplus?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    #初始化时运行sql脚本
    schema: classpath:sql/schema.sql
    initialization-mode: never
logging:
  level:
    root: info
    com.tulingxueyuan: debug
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false
  # mp 默认的mapper.xml路径地址是classpath*:/mapper/**/*.xml 即默认为mapper文件夹下
  # 如果不是需要通过以下配置设置
  mapper-locations: classpath:/mapperxx/*.xml

pagehelper:
  helper-dialect: mysql
  reasonable: true

实现通用增删改查的service接口

//实现通用增删改查的service接口

/***
 * 通用 Service CRUD 封装IService (opens new window)接口,
 * 进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页
 * 前缀命名方式区分 Mapper 层避免混淆,(mapper用select等字眼,service用get等字眼)
 * 泛型 T 为任意实体对象
 * 建议如果存在自定义通用 Service 方法的可能,
 * 请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类 ServiceImpl
 * 对象 Wrapper 为 条件构造器
 */
public interface EmployeeService extends IService<Employee> {
}

自定义通用Service方法

/***
 *  service实现类   继承mp提供通用的service基类 ServiceImpl
 *  ServiceImpl<EmployeeMapper, Employee>
 *      2个泛型 1.EmployeeMapper  Mapper接口
 *              2.Employee       对应Pojo(pojo:对象、实体)
 */
@Service
public class EmployeeImplService extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    //以后需要什么方法就可以在这里写,同时mybatisPlus已经内置多种方法
}

程序启动类

@SpringBootApplication
@MapperScan("com.tulingxueyuan.mbp.mapper")
public class QuickStartApplication {

    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }

    // 最新版
    // 配置分页插件 PaginationInnerInterceptor(DbType.MYSQL)
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}
测试

@SpringBootTest
class QuickStartApplicationTests {

    @Autowired
    EmployeeService service;

    @Autowired
    EmployeeMapper mapper;

    @Test
    public void query() {
        Employee employee = service.getById(1);
        System.out.println(employee);
    }

    @Test
    void saveOrUpdate() {
        // 会根据主键 先查询  如果存在就更新,不存在就插入(插入后主键是自增策略,不一定是8)
        Employee xushu = new Employee(8, "tuling", "123@qq.com", 1, 18);
        service.saveOrUpdate(xushu);
    }

    @Test
    void removeByIds() {
        //Arrays.asList 该方法是将数组转化成List集合的方法。
        List<Integer> ids = Arrays.asList(4, 8);
        //sql==> DELETE FROM tbl_employee WHERE id IN ( ? , ?)
        System.out.println(ids);
        //根据id批量删除
        service.removeByIds(ids);
    }

    @Test
    void listByIds() {
        List<Integer> ids = Arrays.asList(1, 3);

        //批量查询
        List<Employee> lists = service.listByIds(ids);
        System.out.println(lists);
    }

    @Test
    void page() {
        // 分页信息
        //构造一个用于分页的IPage对象,从第一个行数据开始,每页三行数据
        IPage<Employee> iPage = new Page<>(1, 3);
        //service传入上述构造的IPage对象
        IPage<Employee> page = service.page(iPage);
        //获取记录集
        List<Employee> records = page.getRecords();
        for (Employee record:records){
            System.err.println(record);
        }
        //System.out.println(page.getPages());
    }

    //自定义分页
    @Test
    void xmlPage() {
        IPage<Employee> iPage = new Page<>(1, 1);

        System.out.println(mapper.getByGender(iPage, 1).getRecords());
    }
}

关于自定义分页
SQL执行过程:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心醉瑶瑾前

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值