SpringBoot-集成通用mapper
SpringBoot-集成通用mapper
SpringBoot-集成通用mapper
我们在SpringBoot中整合了MyBatis,但是大量重复的增删改查还是很头疼的问题,MyBatis也给出了解决方案:通用Mapper。
Mapper的作用:自动生成增删改查的SQL语句 大大减化对单表的操作,不过多表操作还是要我们实现的。
首先我们将SpringBoot中的MyBatis启动器删除,加入通用Mapper的启动器(此启动器中已经整合了MyBatis需要的相关依赖)。
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
然后我们需要在Mapper接口中继承Mapper类,其泛型需要指定表对应的Bean对象。(注意导包)
package cn.rayfoo.mapper;
import cn.rayfoo.pojo.Hero;
import tk.mybatis.mapper.common.Mapper;
public interface HeroMapper extends Mapper<Hero> {
}
3. 在Bean对象中加入@Table注解和Id注解,用于指定表名称以及主键如果主键是自增长需要用@KeySql指定,如果查询时要忽某个字段需要使用@Transient指定要忽略的字段。
package cn.ryafoo.core.bean;
import com.sun.javafx.beans.IDProperty;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
* @author 张瑞丰
* @description
* @date 2019/11/14
*/
@Data
@Table(name = "person")
public class Person {
@Id
@KeySql(useGeneratedKeys = true)
private Integer id;
private String name;
@Transient
private Integer age;
}
4. 在启动类上修改MapperScan的包名为通用Mapper的包
package cn.ryafoo.core;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("cn.ryafoo.core.mapper")
@SpringBootApplication
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
SpringBoot-集成通用mapper的更多相关文章
- springboot集成通用mapper详细配置
通常,我们利用mybatis写持久层方法.要么按照传统定义mapper方法,定义xml文件的方式,全部手写.要么需要通过mybatis-generator逆向工程插件生成大量的xxxExample文件 ...
- SpringBoot之集成通用Mapper
第一种: 1.引入POM坐标,需要同时引入通用mapper和jpa <dependency> <groupId>tk.mybatis</groupId> <a ...
- Springboot整合通用mapper
通用Mapper的分享使用 参考博客 Mybatis的通用mapper和Hibernate一样都实现了JPA接口,简化了数据库的操作 和Hibernate的对比 Hibernate和Mybatis都是 ...
- springboot搭建通用mapper
对于搭建一个小项目自己测试玩如果采用传统的SSM框架配置起来太过于繁琐,使用springboot简化配置再搭配通用mapper简直不要太方便,话不多说,直接上代码. 首先是pom文件,直接去sprin ...
- 集成通用Mapper
通用Mapper集成 1.引入jar包 <mapper.version>3.0.1</mapper.version><persistence-api.version> ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- springboot与通用mapper的整合
找到springboot工程下的pom.xml文件,导入如下的依赖jar包 <!--配置通用Mapper start--> <dependency> <groupId&g ...
- Springboot 整合通用mapper和pagehelper展示分页数据(附github源码)
简介 springboot 设计目的就是为了加速开发,减少xml的配置.如果你不想写配置文件只需要在配置文件添加相对应的配置就能快速的启动的程序. 通用mapp 通用mapper只支持对单表的操作,对 ...
- springboot中通用mapper结合mybatis generator的使用
通用mapper就是指的是 tk.mybatis 包下的.这个是通用mapper就是说自动生成的dao层需要继承这个框架提供的mapper类.而我们之前用的org.mybatis这个最开始是普通的 ...
- Mybatis 代码生成器(集成通用Mapper)
0.确保通用Mapper被正确配置 1.pom.xml追加 <properties> <targetJavaProject>${basedir}/src/main/java&l ...
随机推荐
- python爬虫(七) mozillacookiejar
MozillaCookiejar 保存百度得Cookiejar信息: from urllib import request from urllib import parse from http.coo ...
- Python学习第十八课——继承,接口继承等
1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...
- python基础day05
上节内容变量if else注释 # ''' msg ''' 3个引号 打印多行 ', "" 双单引号的意义是一样的 缩进 本节内容pycharm使用 集成开发环境(IDE,Inte ...
- 移动端一像素边框解决方案[css scale]
新建一个border.css的文件,然后将代码复制粘贴,然后引用border.css样式文件,然后给需要添加边框的元素,加相应的类样式. tips: border-bottom[一像素下边框]:bor ...
- UniGUI的SQLite数据库(04)
1]放FDConnection1和FDQuery1到界面上 一定要 放一个 FDPhysSQLiteDriverLink1到ServerModule上 2]在OnFormCreate事件里写 FDQ ...
- Genymotion设置代理至BurpSuite和Charles
环境 Genymotion VirtualBox BurpSuite Charles 准备 怎么下载安装就不用说了,因为genymotion要依赖VirtualBox,所以要先把VirtualBox装 ...
- Tensorflow之AttributeError: module 'tensorflow' has no attribute 'mul'
tf.mul已经在新版本中被移除,请使用 tf.multiply 代替
- mysql5.6源码安装(转)
mysql5.6源码安装 转自 jabbok博客园 https://www.cnblogs.com/jabbok/p/9418344.html 1 编译安装 1 2 3 4 5 6 groupadd ...
- ubuntu 12.04 配置vsftpd 服务,添加虚拟用户,ssl加密
1.对于12.04的vsftpd 有一些bug,推荐安装版本vsftpd_2.3.5-1ubuntu2ppa1_amd64.debapt-get install python-software-pro ...
- django静态文件处理
django静态文件处理 从开始接接触python这门语言已有四年了,中间陆续的学习,又不断的忘记,所以基本上是没有系统的知识体系.但是挺喜欢这门简洁,强大的动态语言.最近自己私人有个项目要做,虽 ...