一.SpringBoot中使用Mybatis

springBoot中使用mybatis跟以前spring中使用方法一样.

1.mybatis配置:

spring:
datasource:
url: jdbc:mysql://localhost:3306/?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: ******
#mybatis配置
#需要配置别名的实体类的包
mybatis:
type-aliases-package: com.lanou.spring_bootm.entity
##mapper文件所在的位置
mapper-locations: classpath:mapper/*mapper.xml

2.配置mapper接口.

package com.lanou.spring_bootm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;
public interface EmpMapper extends BaseMapper {
List<Emp> findAll(); }

3.配置mapper文件.

在resources文件夹下创建mapper包

在mapper包中创建mapper.xml来写sql语句.

<?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.lanou.spring_bootm.mapper.EmpMapper">
<select id="findAll" resultType="emp">
select uuid,username,birthday,email from emp
</select>
</mapper>

  

二.MybatisPlus

mybatisPlus是对mybatis的一个扩展.

使用mybatisplus直接将配置文件中mybatis改为mybatis-plus

mybatis-plus:
type-aliases-package: com.lanou.spring_bootm.entity
##mapper文件所在的位置
mapper-locations: classpath:mapper/*mapper.xml

mybatis-plus中定义好了很多对数据库简单的增删改查,和分页功能

用法很简单,将mapper中的接口继承于BaseMapper接口,

使用该接口中的方法不需要经过mapper.xml文件,

package com.lanou.spring_bootm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;

//BaseMapper是mybatisplus中的接口
public interface EmpMapper extends BaseMapper {
//自定义的查询方法,对应xml中的id
List<Emp> findAll(); }

mybatis-plus中的分页功能需要要在config文件中配置Bean

//在springBoot中习惯使用java的配置方式进行配置
//SSM项目中也能用java方式进行配置

//这个注解相当于给这个类配置成配置文件.
@Configuration
public class MyBatisConfig { //配置MybatisPlus中的分页拦截
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
} }

  

SpringBoot_Mybatis MyBatisPlus的更多相关文章

  1. 优雅高效的MyBatis-Plus工具快速入门使用

    目前正在维护的公司的一个项目是一个ssm架构的java项目,dao层的接口有大量数据库查询的方法,一个条件变化就要对应一个方法,再加上一些通用的curd方法,对应一张表的dao层方法有时候多达近20个 ...

  2. MyBatis-plus 代码自动生成器

    MyBatis-plus  代码自动生成器 1.添加pom文件依赖 <!-- Mybatis-Plus 自动生成实体类--> <dependency> <groupId& ...

  3. MyBatis-plus 代码生成器

    1.添加pom文件依赖 <!-- Mybatis-Plus 自动生成实体类--> <dependency> <groupId>com.baomidou</gr ...

  4. springboot集成mybatisplus

    介绍: Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生.(摘自mybatis-plus官网)Mybati ...

  5. 基于SpringBoot从零构建博客网站 - 整合lombok和mybatis-plus提高开发效率

    在上一章节中<技术选型和整合开发环境>,确定了开发的技术,但是如果直接这样用的话,可能开发效率会不高,为了提高开发的效率,这里再整合lombok和mybatis-plus两个组件. 1.l ...

  6. 使用Springboot + Gradle快速整合Mybatis-Plus

    使用Springboot + Gradle快速整合Mybatis-Plus 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] MyBatis-Plus(简称 MP)是一个 MyBatis ...

  7. Mybatis-Plus入门示例

    1.内容: Mybatis-Plus只是在Mybatis的基础上,实现了功能增强,让开发更加简洁高效. Mybatis-Plus并没有修改Mybatis的任何特性. 2.入门示例: 2.1 需求:使用 ...

  8. MyBatis-plus二级缓存使用

    MyBatis二级缓存使用 注意点: 在最新的3.x版本,实现二级缓存的配置也有了一些改变. 官方建议在service使用缓存,但是你也可以直接在mapper层缓存,这里的二级缓存就是直接在Mappe ...

  9. SpringBoot整合系列--整合MyBatis-plus

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/10125279.html SpringBoot整合MyBatis-plus 步骤 第一步: ...

随机推荐

  1. CodeForces-1221A-2048 Game-思维题

    You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every int ...

  2. 使用nginx访问本地电脑的目录文件

    cat /usr/local/opt/nginx/ //nginx路径 cd /usr/local/opt/nginx/html //localhost的指向 ln -s ~/Documents do ...

  3. Error resolving template,template might not exist or might not be accessible by any of the configured Template Resolvers

    template might not exist or might not be accessible by any of the configured Template Resolvers at o ...

  4. NuGet包介绍

    Antlr 各种语言的语法识别器.解析器.编译和翻译器 Microsoft.AspNet.Web.Optimization 绑定优化CSS和JavaScript文件,也就是App_Start下的Bun ...

  5. !important的用法及作用

    定义及语法 !important,作用是提高指定样式规则的应用优先权(优先级).语法格式{ cssRule !important },即写在定义的最后面,例如:box{color:red !impor ...

  6. 网络请求NSLog结果不全

    碰到了两次 NSLog请求结果只有一部分 如果NSLog语句中没有汉语就可以全部输出了,不清楚原因. NSLog(@"%@",object);//这样结果是全的 NSLog(@&q ...

  7. C 语言源代码说明

    void bdmain(void){/* 禁止 Cache 和 MMU */ cache_disable(); mmu_disable(); /* 端口初始化 */ port_init(); /* 中 ...

  8. Android Studio Gradle无法获取pom文件

    错误提示: Error:Execution failed for task ':app:lintVitalRelease'. > Could not resolve all artifacts ...

  9. Dom关于位置和尺寸的api

    parentNode    直接父级//和offsetParent不同 inner2.parentNode <!DOCTYPE html> <html id="html&q ...

  10. mysql清除主从复制关系

    mysql清除主从复制关系 网页转自https://www.cnblogs.com/wjoyxt/p/5343662.html mysql主从复制中,需要将主从复制关系清除,需要取消其从库角色.这可通 ...