Spring集成Mybatis plus
Springboot中流行的持久层框架是Hibernate和Mybatis
前者已经成为Spring Data的规范,以极简和全自动的对象映射著称,后者是一个半自动化的ORM框架,SQL独立存放在XML文件中,提高自由度的同时也方便DBA进行校对和后续SQL优化
由于Mybatis plus完全基于Mybatis,且吸收了一部分HIbernate的优点,提供集成的CRUD,基本上现在开发中都使用Mybatis Plus替代原生Mybatis框架
敲黑板: MybatisPlus 不能和 Mybatis同时使用
使用流程(三步走)
- 添加Maven依赖
- MyBatisPlus 配置
- 编写3种文件(可使用插件生成,Idea Mybatis generater)
- 实体entity
- Dao层接口Mapper
- Xml文件中的Sql语句
第一步 添加Maven依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatisplus-spring-boot-starter</artifactId>
</dependency>
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>
复制代码
第二步 在application.yml中配置Mybatis Plus
使用properties配置文件的可以在www.toyaml.com/index.html进行yml文件的转换
重点!!!
第一行的xml文件扫描,此处的xml文件存放在resource下的mapper路径下(我的路径是resource/mapper/xx模块/xxx.xml)
第二行的实体扫描!!!要根据自己的entity目录进行配置 自己根据自己的实体类路径修改typeAliasesPackage,或者使用我的这种模块化工程路径(工程根目录/modules/xx模块/entity/xxx.class)
mybatis-plus:
mapper-locations: classpath:mapper/**/*.xml #__对应第三步的mapper路径__#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: io.renren.modules.*.entity #__对应第三步的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#序列接口实现类配置#key-generator: com.baomidou.springboot.xxx#逻辑删除配置
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
复制代码
没添加mysq数据源的再添加如下配置
spring:
datasource:
continue-on-error: false
driver-class-name: com.mysql.jdbc.Driver
username: root ##根据自己MySQL的用户名修改
url: jdbc:mysql://127.0.0.1:3306/renren_test ##根据自己MySQL的ip和端口修改
password: root ##根据自己MySQL的密码修改复制代码
第三步 进入正题
之前的步骤都是Mybatis的配置和管理,以下才是真正的业务代码
- entity 文件放在entity目录下(要与第二步的实体扫描路径对应,忽略的回去仔细看第二步)
@TableName("sword")
public class Sword {@TableIdprivate Long id;private String name;}
复制代码
之前用过JPA的可能直接运行发现并没有在数据库中建表,因为Mybatis不支持自动根据实体类创建表,所以需要自己建表,如果要引入这个功能可以引入JPA,只利用JPA的自动建表功能,本文为了简单明了不涉及,可以查看我另一篇文章Mybatis加入JPA的自动建表功能
- mapper放在Dao目录下
@Repository
@Mapper
public interface SwordMapper {Sword selectSword(Pagination page, String id);void insertSword(@Param("name") String name);
}
复制代码
- xml(要与第二步的mapper扫描路径对应,忽略的回去仔细看第二步)
<?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="io.renren.modules.test.Dao.SwordMapper"><select id="selectSword" resultType="io.renren.modules.test.entity.Sword">select * from sword limit 1</select><insert id="insertSword">insert into sword (name) values (#{name})</insert></mapper>
复制代码
Mybatis框架的文件引入方式:
- entity通过第二步的配置typeAliasesPackage进行查找,也可以通过注解方式查找
- Mybatis的xml与mapper对应,mapper提供java层的接口,xml保存相对应的sql语句
- xml在第二步的配置中由Mybatis发现,相对应的Mapper文件在xml中通过namespace的方式关联
最后一步 测试
在Controller文件中实现Restful接口 SwordController.class
@RestController
@RequestMapping
public SwordController {@Autowired // 自动注入mapperprivate SwordMapper swordMapper;@GetMapping("sword")public sword(UserEntity userEntity) {Sword sword1 = new Sword();sword1.setName("这是一把剑");Sword sword = swordMapper.selectSword("1");return sword;}
}
复制代码
访问127.0.0.1:8080/demo/sword
大功告成!
后记
在其他的博客中大家可能看到,有很多利用sqlsession或者SqlSessionFactoryBean来加载Mybatis配置文件,我这里又没有sqlsession,经常有人会被搞得一头雾水,这里解释一下,在原生的mybatis中,sqlsession可以由SqlSessionFactory创建;而在mybatis-spring中则需要通过SqlSessionFactoryBean来创建,并传入datasource。 像这样
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation"> <value>classpath:mybatis/mapper.xml</value> </property> <property name="dataSource" ref="dataSource" />
</bean>
复制代码
但是在Springboot中,mybatis-spring-boot支持自动创建并注册SqlSessionFactoryBean,所以sqlsession的配置并不需要。感谢Springboot.
Spring集成Mybatis plus相关推荐
- Spring集成Mybatis,spring4.x整合Mybatis3.x
Spring集成Mybatis,spring4.x整合Mybatis3.x ============================== 蕃薯耀 2018年3月14日 http://www.cnblo ...
- Spring集成MyBatis框架
Spring集成MyBatis框架 Java在写数据库查询时,我接触过四种方式: 1.纯Java代码,引用对应的数据库驱动包,自己写连接与释放逻辑(可以用连接池) 这种模式实际上性能是非常不错的,但是 ...
- Spring集成Mybatis多数据源配置
既然在整理Mybatis那就把经常用的这个多数据源的笔记也整一下吧. Spring集成Mybatis在之前就已经提到了.Spring集成Mybatis 集成Mybatis多数据源有两种方式: 1.创建 ...
- Spring集成MyBatis
将mybatis与spring进行整合,主要解决的问题就是讲SqlSessionFactory对象交由spring来管理,所以,该整合,只需要将SqlSessionFactory的对象生成器SqlSe ...
- SSM + Shiro 整合 (2)- 实现 Spring 集成 MyBatis
项目源码:https://github.com/weimingge14/Shiro-project 演示地址:http://liweiblog.duapp.com/Shiro-project/logi ...
- Spring集成Mybatis配置映射文件方法详解
Spring ORM模块集成Mybatis使用到了mybatis-spring,在配置mybatis映射文件的时候,一般不直接在Mybatis的配置文件里进行配置,而会在Spring的配置文件里使用M ...
- Spring集成Mybatis错误Result Maps collection already contains value for XXX
Spring在集成Mybatis出现如下错误: SpringResult Maps collection already contains value for com.guowei.maven.fra ...
- spring 集成mybatis——多数据源切换(附带定时器的配置)
新建com.millery.utils包在其下新建DataSourceContextHolder类 package com.millery.utils;public class DataSourceC ...
- spring集成mybatis后,打印SQL语句
1.美图 网上说mybatis的早前版本配置打印sql还比较简单,在3.0.6之后配置方式修改了. 现在的spring-mybatis.xml配置如下: <bean id="sqlSe ...
最新文章
- 解题报告:AcWing 1165. 单词环(01分数规划、hash、经验优化)
- TensorFlow实现案例汇集:代码+笔记
- 【TypeScript】防止对象改变
- Masonry自动布局详解一:基本用法
- 【数据库学习笔记】——创建数据库连接对象connection
- 【LeetCode笔记】146. LRU缓存机制(Java、双向链表、哈希表)
- 企业微信_获取access_token
- [AT2699]Flip and Rectangles
- 哪个专业学python语言_想学Python编程?你真的适合吗?
- mysql_query的返回值
- 【Linux初始化init系统】
- 如何计算页面的浏览率
- NEON 常用函数及其执行结果
- wps表格户主序号_WPS表格怎么设置自动排列序号?
- 2022年MySQL最新面试题
- Flash as2.0与3.0功能性能详细对比
- html区域框什么意思,如何理解【没有padding 、margin、border的情况下 ,行内框等于内容区域】这句话?...
- 哈工大和复旦计算机考研分数线,厦大 复旦 哈工大出分数线了 进来参考吧
- 吴恩达Deeplearning第二课代码bug修正大全
- Ubuntu最新版本(Ubuntu22.04LTS)安装nfs服务器及使用教程