2.2K Star 11.1K Fork 3.4K

GVPbaomidou / mybatis-plus

 / 详情

2.0之后的版本能否支持多表联合查询?

已完成
创建于  
2016-12-24 12:09

或者,针对使用mybaitsplus框架的Spring MVC应用应该怎么做到多表查询?以及多表分页查询?

另外,我在官方文档中好象没有找到“ActiveRecord ”模式的相关介绍和说明,能否补充ActiveRecord相关的文档?

评论 (5)

kucoll 创建了任务

暂时不支持多表,未来会支持。

你们太威武了。崇拜你们!!!!

更多可以查看源码 test 下面的测试用例, mybatis-plus 之 activeRecord 用法如下:

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);
	}

}
青苗 关闭了任务

谢谢! 知道了,ActiveRecord模式更适合非Spring Web项目

@kucoll activeRecord 有 user.sql().select(....); 可以玩多表。 另外提供了 SqlRunner.db().select(...) 也可以直接用。 如果你觉得 xml 多余 mybatis-plus 也是可以删掉 xml 不要的。 多谢支持 @kucoll

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
709883 taoyu cd 1578931166 12260 jobob 1695284587
Java
1
https://gitee.com/baomidou/mybatis-plus.git
git@gitee.com:baomidou/mybatis-plus.git
baomidou
mybatis-plus
mybatis-plus

搜索帮助