Java tutorial
/** * Copyright (c) 2011-2020, 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.activerecord; 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.mybatisX.core.MybatisSessionFactoryBuilder; import com.mybatisX.plugins.Page; import com.mybatisX.test.mysql.TestMapper; import com.mybatisX.test.mysql.entity.Test; import com.mybatisX.toolkit.IdWorker; import com.mybatisX.toolkit.TableInfoHelper; /** * <p> * ActiveRecord * </p> * * @author Caratacus * @date 2016-10-11 */ 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()); // 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.selectListSql(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); } }