springboot整合Mybatis-plus配置

第一步:导进mybatis-plus的包

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

自动配置:

MybatisPlusAutoConfiguration 配置类,MybatisPlusProperties 配置项绑定。

mybatis-plus:xxx 就是对mybatis-plus的定制

SqlSessionFactory 自动配置好。底层是容器中默认的数据源
mapperLocations 自动配置好的。有默认值。classpath*:/mapper/**/*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下

容器中也自动配置好了 SqlSessionTemplate
@Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan(“com.atguigu.admin.mapper”) 批量扫描就行

优点:
只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力

第二步:配置数据库文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: abcde
    url: jdbc:mysql://localhost:3306/springbootdata
    type: com.alibaba.druid.pool.DruidDataSource
server:
  port: 8081

第三步:配置实体类和对应的mapper

person实体类:

PersonMapper:

package com.codel.mybatis_plus.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.codel.mybatis_plus.pojo.Person;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

//把方法注入容器里面  如果不想每一个都写,就在启动类里面配置
@Mapper
public interface PersonMapper extends BaseMapper<Person> {
    List<Person> getPersonAll();


}


第三步:写service和controller

PersonService

package com.codel.mybatis_plus.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.codel.mybatis_plus.pojo.Person;

import java.util.List;

//继承这个接口,里面都是写好的sql语句,可以直接调用
public interface PersonService extends IService<Person> {
    List<Person> getPersonAll();
}


PersonController :

package com.codel.mybatis_plus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codel.mybatis_plus.pojo.Person;
import com.codel.mybatis_plus.service.impl.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class PersonController {
    @Autowired
    PersonServiceImpl personService;

    @GetMapping("/getperson")
    public String getPerson(Model model) {
        List<Person> personAll = personService.getPersonAll();
        model.addAttribute("persons", personAll);
        return "person/List";
    }

    //    分页
    @GetMapping("/testPage")
    public String page(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        //构造分页参数
        Page<Person> page = new Page<>(pn, 2);
        //调用page进行分页 获得分页信息
        Page<Person> personPage = personService.page(page, null);
//        userPage.getRecords()
//        userPage.getCurrent()
//        userPage.getPages()
//        获得所有对象信息
        List<Person> records = personPage.getRecords();

        model.addAttribute("page", personPage);
        model.addAttribute("persons", records);
        return "person/List";

    }

    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id, Model model,
                             @RequestParam(value = "pn", defaultValue = "1") int pn,
                             RedirectAttributes redirectAttributes) {

        boolean b = personService.removeById(id);
        redirectAttributes.addAttribute("pn", pn);
        return "redirect:/testPage";
    }

}

看上面的controller可以知道,要使用分页,就要使用分页插件,下面进行分页的配置
可以从官网上面找到模板的
Mybatis-plus官网网址

package com.codel.mybatis_plus.myconfig;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisConfig {


    // 最新版  分页插件   没有配置这个分页插件的是不可以进行分页的
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setMaxLimit(500L);
        paginationInnerInterceptor.setOverflow(true);

        interceptor.addInnerInterceptor(paginationInnerInterceptor);//这是分页拦截器
        return interceptor;
    }
}

第四步:写html前端页面

html页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>输出</title>
</head>
<body>
<div>
    <table border="1" >
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <th>email</th>
            <th>操作</th>
        </tr>
        <tr th:each="person: ${persons}">
            <td th:text="${person.id}"></td>
            <td th:text="${person.name}"></td>
            <td th:text="${person.age}"></td>
            <td th:text="${person.email}"></td>
            <td>
                <!--      把两个数字传给后端去使用          -->
                <a th:href="@{/deleteUser/{id}(id=${person.id},pn=${page.current})}">删除</a>
            </td>
        </tr>

    </table>
    <div>
        当前第[[${page.current}]]页 总计 [[${page.pages}]]页 共[[${page.total}]]条记录
    </div>
    <div>
        <ul>
            <li><a href="#">← 前一页</a></li>
            <li th:class="${num == page.current?'active':''}" th:each="num:${#numbers.sequence(1,page.pages)}">
                <a th:href="@{/testPage(pn=${num})}">[[${num}]]</a>
            </li>
            <li><a href="#">下一页 → </a></li>
        </ul>
    </div>
</div>
</body>
</html>
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值