com.mybatisX.test.oracle.TestUserMapperTest.java Source code

Java tutorial

Introduction

Here is the source code for com.mybatisX.test.oracle.TestUserMapperTest.java

Source

/**
 * Copyright (c) 2011-2014, hubin (jobob@qq.com).
 *
 * 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.mybatisX.test.oracle;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.mybatisX.core.MybatisSessionFactoryBuilder;
import com.mybatisX.mapper.EntityWrapper;
import com.mybatisX.plugins.Page;
import com.mybatisX.test.oracle.entity.TestUser;

/**
 * <p>
 * MybatisPlus 
 * </p>
 * 
 * @author hubin
 * @Date 2016-04-25
 */
public class TestUserMapperTest {

    /**
     * 
     * RUN   MySql  
     * 
     */
    public static void main(String[] args) {

        //?
        InputStream in = TestUserMapperTest.class.getClassLoader().getResourceAsStream("oracle-config.xml");

        /*
         *  MybatisSessionFactoryBuilder 
         * SqlSessionFactoryBaseMapper
         */
        MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();

        /** ? oracle */
        mf.setDbType("oracle");

        /*
         * 1??????
         * 2?????
         * 3?????
         *    ?? @TableId(value = "test_id") 
         */
        //mf.setDbColumnUnderline(true);

        SqlSessionFactory sessionFactory = mf.build(in);
        SqlSession session = sessionFactory.openSession();
        TestUserMapper testUserMapper = session.getMapper(TestUserMapper.class);
        System.err.println(" debug run  test_user ??? ");
        session.delete("deleteAll");

        /**
         * ?
         */
        int rlt = testUserMapper.insert(new TestUser("10", "abc", 18, 1));
        System.err.println("\n--------------insert-------" + rlt);
        sleep();

        /**
         * ??
         */
        List<TestUser> ul = new ArrayList<TestUser>();
        ul.add(new TestUser("11", "1a", 11, 1));
        ul.add(new TestUser("12", "2a", 12, 2));
        ul.add(new TestUser("a", 1, 1));
        ul.add(new TestUser("b", 2, 2));
        ul.add(new TestUser("c", 3, 1));
        for (TestUser u : ul) {
            rlt = testUserMapper.insert(u);
        }
        System.err.println("\n--------------insertBatch-------" + rlt);
        sleep();

        /**
         * ?
         */
        List<TestUser> ul1 = new ArrayList<TestUser>();
        ul1.add(new TestUser("10", "update-0a", 11, 1));
        ul1.add(new TestUser("11", "update-1a", 11, 1));
        ul1.add(new TestUser("12", "update-2a", 12, 2));
        for (TestUser u : ul1) {
            rlt = testUserMapper.updateById(u);
        }
        System.err.println("\n--------------updateBatchById-------" + rlt);
        sleep();

        System.err.println(
                "\n------------------list  ---- testType = 1 ?--id--DESC--?--------");
        Page<TestUser> page = new Page<TestUser>(1, 2);
        EntityWrapper<TestUser> ew = new EntityWrapper<TestUser>(new TestUser(1), "TEST_ID DESC");
        List<TestUser> paginList = testUserMapper.selectPage(page, ew);
        page.setRecords(paginList);
        for (int i = 0; i < page.getRecords().size(); i++) {
            print(page.getRecords().get(i));
        }
        System.err.println(" " + page.toString());

        /* ?  */
        rlt = session.delete("deleteAll");
        System.err.println("?? rlt=" + rlt);

        /**
         * ??
         */
        session.commit();
    }

    /*
     * ??
     */
    private static void print(TestUser user) {
        sleep();
        if (user != null) {
            System.out.println("\n user: id=" + user.getTestId() + ", name=" + user.getName() + ", age="
                    + user.getAge() + ", testType=" + user.getTestType());
        } else {
            System.out.println("\n user is null.");
        }
    }

    /*
     * ? 
     */
    private static void sleep() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}