广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot整合Mybatis与MybatisPlus方法详细讲解
  • 338
分享到

SpringBoot整合Mybatis与MybatisPlus方法详细讲解

SpringBoot整合MybatisSpringBoot整合MybatisPlus 2023-01-28 06:01:44 338人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、整合mybatis操作1、配置模式2、注解模式3、混合模式二、整合 MyBatis-Plus 完成CRUD1、什么是MyBatis-Plus2、整合MyBatis-Plus3

一、整合MyBatis操作

官网:MyBatis · GitHub

SpringBoot官方的Starter:spring-boot-starter-*

第三方的starter的格式: *-spring-boot-starter

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

1、配置模式

全局配置文件

  • sqlSessionFactory: 自动配置好了
  • SqlSession:自动配置了 SqlSessionTemplate 组合了SqlSession
  • @Import(AutoConfiguredMapperScannerReGIStrar.class);
  • Mapper: 只要我们写的操作MyBatis的接口标准了 @Mapper 就会被自动扫描进来
  • 可以修改配置文件中 mybatis 开始的所有配置;
@EnableConfigurationProperties(MybatisProperties.class) : MyBatis配置项绑定类。
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration{}
@ConfigurationProperties(prefix = "mybatis")
public class MybatisProperties
# 配置mybatis规则
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件位置
  mapper-locations: classpath:mybatis/mapper*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下
  • 容器中也自动配置好了 SqlSessionTemplate
  • @Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan("com.atguigu.admin.mapper") 批量扫描就行
  • 优点:

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

    3、CRUD功能

        @GetMapping("/user/delete/{id}")
        public String deleteUser(@PathVariable("id") Long id,
                                 @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                                 RedirectAttributes ra){
            userService.removeById(id);
            ra.addAttribute("pn",pn);
            return "redirect:/dynamic_table";
        }
        @GetMapping("/dynamic_table")
        public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,Model model){
            //表格内容的遍历
    //        response.sendError
    //     List<User> users = Arrays.asList(new User("zhangsan", "123456"),
    //                new User("lisi", "123444"),
    //                new User("haha", "aaaaa"),
    //                new User("hehe ", "aaDDD"));
    //        model.addAttribute("users",users);
    //
    //        if(users.size()>3){
    //            throw new UserTooManyException();
    //        }
            //从数据库中查出user表中的用户进行展示
            //构造分页参数
            Page<User> page = new Page<>(pn, 2);
            //调用page进行分页
            Page<User> userPage = userService.page(page, null);
    //        userPage.getRecords()
    //        userPage.getCurrent()
    //        userPage.getPages()
            model.addAttribute("users",userPage);
            return "table/dynamic_table";
        }

    Service

    @Service
    public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    }
    public interface UserService extends IService<User> {
    }

    到此这篇关于SpringBoot整合Mybatis与MybatisPlus方法详细讲解的文章就介绍到这了,更多相关SpringBoot整合Mybatis与MybatisPlus内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

    --结束END--

    本文标题: SpringBoot整合Mybatis与MybatisPlus方法详细讲解

    本文链接: https://www.lsjlt.com/news/178230.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作