MyBatis-Plus 使用说明介绍
先看一下和MyBatis 不同点说明:
@GetMapping("/select_sql")
public Object getUserBySql() {
User user=new User(1L);
User selectUser=user.selectById();
user.deleteById(user);
return userService.selectListBySQL(); //普通走法
}
最大的不同点在entity层直接可以调用superEntity,直接实现默认的一些增删改查
/**
* 用户表
*/
@SuppressWarnings("serial")
public class User extends SuperEntity<User> { /**
* 名称
*/
private String name;
/**
* 年龄
*/
private AgeEnum age;
/**
* 这里故意演示注解可无
*/
@TableField("test_type")
@TableLogic
private Integer testType; /**
* 测试插入填充
*/
@TableField(fill = FieldFill.INSERT)
private Date testDate; private Long role;
private PhoneEnum phone; public User() {
}
public User(Long id){
this.setId(id);
} public User(Long id, String name, AgeEnum age, Integer testType) {
this.setId(id);
this.name = name;
this.age = age;
this.testType = testType;
} public User(String name, AgeEnum age, Integer testType) {
this.name = name;
this.age = age;
this.testType = testType;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public AgeEnum getAge() {
return this.age;
} public void setAge(AgeEnum age) {
this.age = age;
} public Integer getTestType() {
return this.testType;
} public void setTestType(Integer testType) {
this.testType = testType;
} public Long getRole() {
return this.role;
} public void setRole(Long role) {
this.role = role;
} public PhoneEnum getPhone() {
return this.phone;
} public void setPhone(PhoneEnum phone) {
this.phone = phone;
} public Date getTestDate() {
return testDate;
} public void setTestDate(Date testDate) {
this.testDate = testDate;
} @Override
public String toString() {
return "User [id=" + this.getId() + ", name=" + name + ", age=" + age
+ ", testType=" + testType + ", testDate="
+ testDate + ", role=" + role + ", phone=" + phone + "]";
}
SuperEntity 继承Model ,Model默认实现了简单的增删改查,不需要再去实现mapper层。
/**
* 演示实体父类
*/
public class SuperEntity<T extends Model> extends Model<T> { /**
* 主键ID , 这里故意演示注解可以无
*/
@TableId("test_id")
private Long id;
private Long tenantId; public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public Long getTenantId() {
return tenantId;
} public SuperEntity setTenantId(Long tenantId) {
this.tenantId = tenantId;
return this;
} @Override
protected Serializable pkVal() {
return this.id;
}
}
2.对于原生service层改造实现,默认实现baseMapper, baseMapper 默认实现了mapper的常规使用写法方式。
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { @Override
public boolean deleteAll() {
return retBool(baseMapper.deleteAll());
} @Override
public List<User> selectListBySQL() {
return baseMapper.selectListBySQL();
} @Override
public List<User> selectListByWrapper(Wrapper wrapper) {
return baseMapper.selectListByWrapper(wrapper);
//baseMapper.selectma
}
}
3.UserMapper 继承 superMapper,而superMapper继承baseMapper,你可以把一些公共的方法放在superMapper里,从而实现公用。
/**
* User 表数据库控制层接口
*/
public interface UserMapper extends SuperMapper<User> { /**
* 自定义注入方法
*/
int deleteAll(); @Select("select test_id as id, name, age, test_type from user")
List<User> selectListBySQL(); List<User> selectListByWrapper(@Param("ew") Wrapper wrapper); }
最后的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.baomidou.springboot.mapper.UserMapper"> <!-- 通用查询结果列 -->
<sql id="Base_Column_List">
test_id AS testId, name, age, test_type AS testType, role, phone
</sql> <delete id="deleteAll">
DELETE FROM USER
</delete> <select id="selectListByWrapper" resultType="com.baomidou.springboot.entity.User">
SELECT * FROM USER
<!-- 判断 wrapper 是否为空 emptyOfWhere -->
<where>
${ew.sqlSegment}
</where>
</select>
</mapper>
可以看到他们的关系是一层一层过滤设计的,用了公共代码方便了公用,也达到了解耦合。
MyBatis-Plus 使用说明介绍的更多相关文章
- MyBatis注解Annotation介绍及Demo
MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...
- 一、mybatis的插件介绍
摘自:https://www.cnblogs.com/qm-article/p/11785350.html mybatis的插件机制 一.mybatis的插件介绍 关于mybatis的插件,我想大 ...
- Mybatis通用Mapper介绍和使用
Mybatis通用Mapper介绍与使用 前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL. ...
- Mybatis拦截器介绍
拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...
- Mybatis(一)入门介绍
一.MyBatis的发展 MyBatis 是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到google code, 并且改名M ...
- 详解Mybatis通用Mapper介绍与使用
使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL.而且,当数据库表结构改动时,对应的所有SQL以及 ...
- MyBatis(1)优点&介绍&工程
本次全部学习内容:MyBatisLearning 一:jabc的相关说明: jdbc编程步骤: 加载数据库驱动 创建并获取数据库链接 创建jdbc statement对象 设置sql语句 设置sql语 ...
- Mybatis拦截器介绍及分页插件
1.1 目录 1.1 目录 1.2 前言 1.3 Interceptor接口 1.4 注册拦截器 1.5 Mybatis可拦截的方法 1.6 利用拦截器进行分页 1.2 前言 拦截器的一 ...
- jdbc、Mybatis、Hibernate介绍(非原创)
文章大纲 一.jdbc介绍二.Mybatis介绍三.Hibernate介绍四.jdbc.Mybatis.Hibernate比较五.参考文章 一.jdbc介绍 1. jdbc编程步骤 (1)加载数据 ...
- MyBatis注解Annotation介绍及Demo(转)
MyBatis可以利用SQL映射文件来配置,也可以利用Annotation来设置.MyBatis提供的一些基本注解如下表所示. 注解 目标 相应的XML 描述 @CacheNamespace 类 &l ...
随机推荐
- 编译Qt5.0连接MySql5.5数据库的驱动(5.0版本的编译,我记得5.2开始自带了)
第一步 1.准备好Mysql数据库安装文件,Qt5.0完整的离线安装包,以及Qt5.0的完整的源代码.安装好程序,假设Mysql的安装路径为:C:\MySQL5.5,Qt5.0的安装路径:C:\Qt\ ...
- JPA 报错解决方案 com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'test_db' when IDENTITY_INSERT is set to OFF.
这种错误插入数据时就是hibernate的自增长字段生成规则应该用native 在字段前加入注解 @GeneratedValue(generator="generator") @G ...
- 仿写confirm和alert弹框
在工作中,我们常常会遇到原生的样式感觉比较丑,又和我们做的项目风格不搭.于是就有了仿写原生一些组件的念头,今天我就带大家仿写一下confirm和alert样式都可以自己修改. 有些的不好的地方请指出来 ...
- 宜信开源|手把手教你安装第一个LAIN应用
LAIN是宜信公司大数据创新中心开发的开源PaaS平台.在金融的场景下,LAIN 是为解放各个团队和业务线的生产力而设计的一个云平台.LAIN 为宜信大数据创新中心各个团队提供了统一的测试和生产环境, ...
- Mysql常用的查询语句,记录一下,好东西大家共享
一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...
- idea 创建maven项目(一)
1.新建 Project 2.点击Next 3.填写组织名称和项目名称,点击next 4.在你的本地仓库目录下创建settings.xml文件,把mirror的url改成阿里云的 <?xml v ...
- Codeforces Round #564 (Div. 2)A
A. Nauuo and Votes 题目链接:http://codeforces.com/contest/1173/problem/A 题目 Nauuo is a girl who loves wr ...
- Spring Boot使用MyBatis Generator、Swagger
MyBatis是Java目前主流的ORM框架,在Spring Boot中使用MyBatis可以参考这篇文章:http://www.ityouknow.com/springboot/2016/11/06 ...
- PCA(主成分分析)算法
设有\(m\)个指标,\(n\)个样本的原始数据 将原始数据按列组成矩阵 \(X _ { n \times m }\) 将\(X\) 的每一列进行中心化 求\(X\)的协方差矩阵\(\Sigma _ ...
- C# 异步转同步 TaskCompletionSource
本文通过TaskCompletionSource,实现异步转同步 首先有一个异步方法,如下异步任务延时2秒后,返回一个结果 private static async Task<string> ...