Java tutorial
/* * Copyright (c) 2017. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.itfsw.mybatis.generator.plugins; import com.itfsw.mybatis.generator.plugins.tools.*; import org.apache.ibatis.session.SqlSession; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import java.io.IOException; import java.sql.SQLException; import java.util.List; /** * --------------------------------------------------------------------------- * * --------------------------------------------------------------------------- * @author: hewei * @time:2017/7/7 15:02 * --------------------------------------------------------------------------- */ public class LimitPluginTest { /** * ?? */ @BeforeClass public static void init() throws SQLException, IOException, ClassNotFoundException { DBHelper.createDB("scripts/LimitPlugin/init.sql"); } /** * ? */ @Test public void testWarnings() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { MyBatisGeneratorTool tool = MyBatisGeneratorTool .create("scripts/LimitPlugin/mybatis-generator-with-error-driver.xml"); tool.generate(); Assert.assertEquals(tool.getWarnings().get(1), "itfsw:?com.itfsw.mybatis.generator.plugins.LimitPlugin??MySQL??"); } /** * ?Sql? */ @Test public void testSqlAndExecute() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LimitPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { // 1. limit ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample"); tbExample.invoke("limit", 5); // limit(5) String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject()); Assert.assertEquals(sql, "select id, field1 from tb limit 5"); // limit(1, 5) tbExample.invoke("limit", 1, 5); sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject()); Assert.assertEquals(sql, "select id, field1 from tb limit 1, 5"); // List list = (List) tbMapper.invoke("selectByExample", tbExample.getObject()); Assert.assertEquals(list.size(), 5); Assert.assertEquals(new ObjectUtil(list.get(0)).get("id"), 2l); // 2. page tbExample.invoke("page", 2, 3); sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject()); Assert.assertEquals(sql, "select id, field1 from tb limit 6, 3"); // list = (List) tbMapper.invoke("selectByExample", tbExample.getObject()); Assert.assertEquals(list.size(), 3); Assert.assertEquals(new ObjectUtil(list.get(0)).get("id"), 7l); } }); } }