mybatis-plus 相关
这里有几个很全的教程:
https://www.cnblogs.com/okong/p/mybatis-plus-guide-one.html
mybtais-plus学习--BaseMapper提供的方法及SQL语句生成
1、实体继承 Model 如下
public class Test extends Model<Test> {
// 静态属性会自动忽略
private static final long serialVersionUID = 1L;
/** 主键 */
// 默认会找 id 为主键,特殊命名需要注解 @TableId
private Long id;
private String type;
...
@Override
protected Serializable pkVal() {
return id;
}
}
2、使用方法如下:
import java.io.InputStream;
import java.util.List;
import java.util.Map; import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSessionFactory; import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.test.mysql.TestMapper;
import com.baomidou.mybatisplus.test.mysql.entity.Test;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.TableInfoHelper; /**
* <p>
* ActiveRecord 测试
* </p>
*/
public class ActiveRecordTest { public static void main(String[] args) {
// 加载配置文件
InputStream in = TestMapper.class.getClassLoader().getResourceAsStream("mysql-config.xml");
MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = mf.build(in);
TableInfoHelper.initSqlSessionFactory(sqlSessionFactory);
sqlSessionFactory.openSession(false);
// 保存一条记录
Test t1 = new Test();
t1.setType("test10");
boolean rlt = t1.insert();
print(" ar save=" + rlt + ", id=" + t1.getId()); // 根据ID更新
t1.setType("t1023");
rlt = t1.updateById();
print(" ar updateById:" + rlt); // 更新 SQL
Test t11 = new Test();
t11.setType("123");
rlt = t11.update("id={0}", t1.getId());
print("update sql=" + rlt); // 查询 SQL
Test t10 = t1.selectOne("id={0}", t1.getId());
print("selectOne=" + t10.getType()); // 插入OR更新
t1.setType("t1021");
rlt = t1.insertOrUpdate();
print(" ar saveOrUpdate:" + rlt); // 根据ID查询
Test t2 = t1.selectById();
print(" t2 = " + t2.toString());
t2.setId(IdWorker.getId());
t2.insert(); // 查询所有
List<Test> tl = t2.selectAll();
for (Test t : tl) {
print("selectAll=" + t.toString());
} // 查询总记录数
print(" count=" + t2.selectCount(null)); // 翻页查询
Page<Test> page = new Page<Test>(0, 10);
page = t2.selectPage(page, null);
print(page.toString()); // 根据ID删除
rlt = t2.deleteById();
print("deleteById=" + rlt + ", id=" + t2.getId()); // 执行 SQL 查询总数
List<Map<String, Object>> ul = t2.sql().selectList(new SQL() {
{
SELECT("*");
FROM("test");
WHERE("type='t1021'");
}
}.toString());
System.err.println("selectList SQL:");
for (Map<String, Object> map : ul) {
System.err.println(map);
} // 根据ID查询
Test t20 = t2.selectById();
print("t2 删除后是否存在?" + (null != t20)); // 删除 SQL
rlt = t2.delete("type={0}", "t1021");
System.err.println("delete sql=" + rlt);
} /*
* 慢点打印
*/
private static void print(String text) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(text);
} }
mybatis-plus 相关的更多相关文章
- JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...
- spring MyBatis的相关面试题
(相关面试题! 供参考!) 1.ORM框架有哪些? MyBatis:半自动化框架(不是纯ORM) 需要写动态SQL语句,实体类和SQL语句之间建立映射关系 Spring:轻量级框架, Java EE的 ...
- MySQL和mybatis查询相关
0.mybatis的xml文件头 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapp ...
- Spring Boot集成Mybatis注解相关
mybatis3开始支持java注解,使用java注解可以替代xml配置文件,简化代码.下面来看一下怎么在spring boot中使用mybatis注解. 1 使用mybatis注解需要的配置.如下面 ...
- Mybatis架构相关的知识
如上所示,这是一个简单的Mybaits执行流程. 我们其实可以看到,一直到第三步(Sqlsession)那么一步,这都是我们的程序里需要创建的.而之后的步骤才是底层完成的任务. 这里就有了一个引申的概 ...
- 8 Spring / Spring MVC / Mybatis 框架相关知识点
1)Spring 的 IOC 和 AOP 有了解吗? IOC:控制反转,不需要手动 new 对象,将其交给 Spring 容器,降低程序耦合度. AOP:面向切面编程,动态代理技术.
- 使用Mybatis Generator自动生成Mybatis相关代码
本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...
- Spring集成MyBatis
本文原创,原文地址为http://www.cnblogs.com/fengzheng/p/5045105.html 如果觉得Hibernate不够灵活,可以尝试用Mybatis.相比于Hibernat ...
- Mybatis整合Spring
根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
随机推荐
- MyBatis 源码篇-MyBatis-Spring 剖析
本章通过分析 mybatis-spring-x.x.x.jar Jar 包中的源码,了解 MyBatis 是如何与 Spring 进行集成的. Spring 配置文件 MyBatis 与 Spring ...
- maftools|TCGA肿瘤突变数据的汇总,分析和可视化
本文首发于公众号“生信补给站”,https://mp.weixin.qq.com/s/WG4JHs9RSm5IEJiiGEzDkg 之前介绍了使用maftools | 从头开始绘制发表级oncoplo ...
- dockerfile相关命令
官方dockerfile:https://github.com/play-with-docker/play-with-docker 可以根据一直的镜像,学习dockerfile的编写 dockerfi ...
- (八)shiro之jdbcRealm
pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- ASP.NET Core 入门(4)(IIS 部署前后端站点)
.NET Core发布部署的文章园内有很多了,大家可以自行百度,该篇主要想总结需要注意的地方,列举前后端(比如前段 Vue,后端 WebAPI)在同一台服务器上的主要两种方式. 两种方式: 1. 前后 ...
- call、apply、bind一直是不求甚解!
一直感觉代码中有call和apply就很高大上(看不懂),但是都草草略过,今天非要弄明白!以前总是死记硬背:call.apply.bind 都是用来修改函数中的this,传参时,call是一个个传参, ...
- Android中BroadcastReceiver的使用
1.Android中广播分为静态注册和动态注册 2.下面是一个简单静态注册的例子 创建一个继承BroadcastReceiver的子类 public class DeviceBootReceiver ...
- CentOS7数据库架构之NFS+heartbeat+DRBD(亲测,详解)
目录 参考文档 理论概述 DRBD 架构 NFS 架构部署 部署DRBD 部署heartbeat 部署NFS及配合heartbeat nfs切记要挂载到别的机器上不要为了省事,省机器 参考文档 htt ...
- 用javafx webview 打造自己的浏览器
背景 项目需要做一个客户端的壳,内置浏览器,访问指定 的url 采用技术 java 1.8 开始吧! java环境配置略 hello world import javafx.application.A ...
- Win10建立标准账户并设置标准账户权限
Win10建立标准账户,并使用组策略对标准帐户的权限进行管理. 注意:本文内容均在管理员帐户下操作,可以只看图片按图示步骤操作即可. 一.建立一个标准账户用于公用登录 (1)按”win健+R”运行“c ...