关键词:
spring boot整合mybatis-plus
简介
mybatis 增强工具包,简化 CRUD 操作。 文档
http://mp.baomidou.com
http://mybatis.plus
优点 | Advantages
- 无侵入:Mybatis-Plus 在 Mybatis 的基础上进行扩展,只做增强不做改变,引入 Mybatis-Plus 不会对您现有的 Mybatis 构架产生任何影响,而且 MP 支持所有 Mybatis 原生的特性
- 依赖少:仅仅依赖 Mybatis 以及 Mybatis-Spring
- 损耗小:启动即会自动注入基本CURD,性能基本无损耗,直接面向对象操作
- 预防Sql注入:内置Sql注入剥离器,有效预防Sql注入***
- 通用CRUD操作:内置通用Mapper、通用Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
- 多种主键策略:支持多达4种主键策略(内含分布式唯一ID生成器),可自由配置,完美解决主键问题
- 支持热加载:Mapper 对应的 XML 支持热加载,对于简单的 CRUD 操作,甚至可以无 XML 启动
- 支持ActiveRecord:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可实现基本 CRUD 操作
- 支持代码生成:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用(P.S. 比 Mybatis 官方的 Generator 更加强大!)
- 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
- 支持关键词自动转义:支持数据库关键词(order、key......)自动转义,还可自定义关键词
- 内置分页插件:基于Mybatis物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于写基本List查询
- 内置性能分析插件:可输出Sql语句以及其执行时间,建议开发测试时启用该功能,能有效解决慢查询
- 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,预防误操作
前面已经 创建了一个springboot项目,我们直接在该项目上整合mybatis-plus
准备工作
-
pom.xml jar引入: <mybatis-plus.version>3.0.6</mybatis-plus.version>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis-plus.version}</version> </dependency>
- 创建表
CREATE TABLE `my_info` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(3) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
项目结构
创建基本的项目结构,controller、dao、entity、service(impl)
如图:
controller :
@RestController
public class MyInfoController {
@Resource
MyInfoService myInfoService;
@GetMapping("myInfo")
public String myInfo(@RequestParam Integer id) {
MyInfo myInfo = myInfoService.getById(id);
return myInfo.toString();
}
}
dao :
public interface MyInfoMapper extends BaseMapper<MyInfo> {
}
entity :
@Data
@TableName(value = "my_info")
public class MyInfo {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
}
service :
public interface MyInfoService {
MyInfo getById(int id);
}
impl :
@Service("myInfoService")
public class MyInfoServiceImpl extends ServiceImpl<MyInfoMapper,MyInfo> implements MyInfoService {
@Override
public MyInfo getById(int id) {
return baseMapper.selectById(id);
}
}
当前,我们也可以在xml中自定义sql( MyInfoMapper.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.honghh.bootfirst.dao.MyInfoMapper">
<!--自定义SQL-->
</mapper>
application.yml 配置文件内容如下
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/boot_demo
username: root
password: 123456
#mybatis
mybatis-plus:
mapper-locations: classpath*:mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.honghh.bootfirst.entity
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
# Sequence序列接口实现类配置
#key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
#逻辑删除配置
#logic-delete-value: -1
#logic-not-delete-value: 0
#自定义填充策略接口实现
#meta-object-handler: com.baomidou.springboot.xxx
#自定义SQL注入器
sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
启动项目
完成上面的步骤,准备工作将近完成,现在开始启动项目,但是你会发现项目启动不起来,报错如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘myInfoController‘: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘myInfoService‘: Unsatisfied dependency expressed through field ‘baseMapper‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.honghh.bootfirst.dao.MyInfoMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java:40002) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41008) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at com.honghh.bootfirst.BootFirstApplication.main(BootFirstApplication.java:11) [classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘myInfoService‘: Unsatisfied dependency expressed through field ‘baseMapper‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.honghh.bootfirst.dao.MyInfoMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:452) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:526) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:636) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 19 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.honghh.bootfirst.dao.MyInfoMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 35 common frames omitted
上网查了一下,说一般是下面原因造成的:
- 1.applicationContext.xml配置中没有扫描包
- 2.controller层的@controller或者service层的@Service注解没写
- 3.service注入mapper失败
仔细看了下代码是mapper层没有注入,mapper注入方法有一下几种方式。
- 1.直接在mapper上写注解
@Mapper
public interface MyInfoMapper extends BaseMapper<MyInfo> {
}
- 2.在启动类上添加注解
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.honghh.*.dao")
public class BootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(BootFirstApplication.class, args);
}
}
mybatis-config中只是会为对应的mapper创建代理类,而想真正包装成bean,注入到spring容器中,还是需要靠AutoConfiguredMapperScannerRegistrar,它会根据扫描@Mapper注释或是@MapperScan指定的包下的接口,将其注册为bean。
- 3.也可以写一个配置类 MybatisPlusConfig
@Configuration
@MapperScan(basePackages = "com.honghh.*.dao")
public class MybatisPlusConfig {
/**
* mybatis-plus 分页插件
*/
@Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
}
在浏览器中输入 :http://localhost:8080/myInfo?id=1 显示如下
成功!!!此时你已经学会了整合mybatis-plus
参考文本
https://gitee.com/baomidou/mybatisplus-spring-boot
文章来源 : https://blog.csdn.net/qq_35098526/article/details/87782961
springboot整合mybatis-plus
springboot整合mybatis-plus简介mybatis增强工具包,简化CRUD操作。文档http://mp.baomidou.comhttp://mybatis.plus优点|Advantages无侵入:Mybatis-Plus在Mybatis的基础上进行扩展,只做增强不做改变,引入Mybatis-Plus不会对您现有的Mybatis构架产生任何影响... 查看详情
springboot整合mybatis-plus+durid数据库连接池(代码片段)
...及项目搭建springboot整合之版本号统一管理 springboot整合mybatis-plus+durid数据库连接池springboot整合swaggerspringboot整合mybatis代码快速生成springboot整合之统一结果返回springboot整合之统一异常处理springboot整合之Validated参数校验 sprin... 查看详情
实践丨springboot整合mybatis-plus项目存在mapper时报错
摘要:在SpringBoot运行测试Mybatis-Plus测试的时候报错的问题分析与修复本文分享自华为云社区《SpringBoot整合MybatisPlus项目存在Mapper时运行报错的问题分析与修复》,作者:攻城狮Chova。异常信息在SpringBoot运行测试Mybat... 查看详情
第276天学习打卡(知识点回顾springboot整合mybatis-plus)
知识点回顾springboot整合Mybatis-plus自动配置MyBatisPlusAutoConfiguration配置类,MyBatisPlusProperties配置项绑定。mybatis-plus:xxx就是对mybatis-plus的定制SqlSessionFactory自动配置好,底层是容器中默认的数据源mapperLocations自动配置好的。有... 查看详情
springboot整合mybatis-plus
maven依赖注意:本文使用的是mysql,数据库依赖就不展示了<!--引入mvbatie-plusstarter--> <dependency> <groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><versi 查看详情
springboot使用druid与mybatis-plus整合(代码片段)
SpringBoot使用Druid与Mybatis-plus整合需要导入的依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependenc 查看详情
springboot2.x:整合mybatis-plus
简介Mybatis-Plus是在Mybatis的基础上,国人开发的一款持久层框架。并且荣获了2018年度开源中国最受欢迎的中国软件TOP5同样以简化开发为宗旨的SpringBoot与Mybatis-Plus放在一起会产生什么样的化学反应呢?下面我们来领略一下两者配... 查看详情
实践丨springboot整合mybatis-plus项目存在mapper时报错
...#xff0c;作者:攻城狮Chova。异常信息在SpringBoot运行测试Mybatis-Plus测试的时候报错:rg.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatin 查看详情
springboot整合mybatis-plus
1.添加pom引用maven的引用很简单,官方已经给出starter,不需要我们考虑它的依赖关系了,此处使用的是2.3版本。<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artif 查看详情
使用springboot+gradle快速整合mybatis-plus
作者:Stanley罗昊QQ:1164110146MyBatis-Plus(简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。注:作者使用IDEA编辑器使用SpringInitializr快速初始化一个SpringBoot工程在IDEA编辑左上角Fli... 查看详情
springboot整合mybatis-plus+druid组件,实现增删改查
前言本篇文章主要介绍的是SpringBoot整合mybatis-plus,实现增删改查。GitHub源码链接位于文章底部。建库建表创建springboot数据库,创建t_user表,字段id主键自增,name,age。工程结构添加依赖新建一个maven项目,在pom文件中添加以下依... 查看详情
springboot整合mybatis-plus逆向工程(代码片段)
MyBatis-Plus(简称MP)是一个?MyBatis?的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。官方文档代码生成器AutoGenerator是MyBatis-Plus的代码生成器,通过AutoGenerator可以快速生成Entity、Mapper、MapperXML、Service... 查看详情
springboot整合mybatis-plus(代码片段)
...用三、减少硬编码四、代码生成器写在前面 mybatis-plus顾名思义就是mybatis的增强工具。对于mybatis的功能只做扩展、不做改变。体现在内置的Mapper对CRUD进行了封装、只需要通过简单的配置即可实现增删改查操作。而不... 查看详情
springboot整合mongodb操作工具类仿mybatis-plus风格(代码片段)
以下是仿照mybatis-plus风格操作mongodb的增删改查的工具类以及使用示例 pom文件引入依赖<!--mongodb--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb< 查看详情
使用springboot简单整合springsecurity和mybatis-plus(代码片段)
1、概述SpringSecurity是一个功能强大且高度可定制的身份验证和访问控制框架。它是用于保护基于Spring的应用程序的实际标准。SpringSecurity是一个框架,致力于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Sprin... 查看详情
一文吃透springboot整合mybatis-plus(保姆式教程)(代码片段)
...心性养成之路🥭本文内容:一文吃透SpringBoot整合mybatis-plus(保姆式教程)文章目录手动整合mybatis-plus详解1、引入依赖2、创建基本目录结构3、配置application.yml4、在entity包下创建实体类5、创建Mapper接口6、创建Mappe... 查看详情
springboot整合mybatis-plus分页自动填充功能(代码片段)
springboot整合Mybatis-Plus分页、自动填充功能功能此次分页、自动填充功能的实现是在SpringBoot整合druid、Mybatis-plus实现的基础上完成的,包括数据源配置、各种依赖添加、mapper和service的实现。不在重复记录。Java开发手册要求数... 查看详情
mybatis-plus的操作(新增,修改)(代码片段)
一、springboot整合mybatis-plus1.1springboot在整合mybatis-plus时,pom文件中的坐标一般同时会引入Druid。<!--springboot整合mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><ver... 查看详情