mybatis-plus 2.0 之 ActiveRecord 語法使用

mybatis-plus 2.0 之  ActiveRecord 語法使用

 

這裏簡單介紹使用,更多支持點擊這裏查看源碼單元測試!java

一、實體繼承 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;
	}

}

 

二、使用方法以下:

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

}
相關文章
相關標籤/搜索