Skip to content

Commit

Permalink
添加单例SQLQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Caratacus committed Dec 11, 2016
1 parent 3c8fb4a commit 7a1ce9c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
*/
package com.baomidou.mybatisplus.activerecord;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;

import com.baomidou.mybatisplus.enums.SqlMethod;
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SQLQuery;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.SqlQuery;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
Expand Down Expand Up @@ -338,8 +337,8 @@ public int selectCount(Wrapper wrapper) {
* 执行 SQL
* </p>
*/
public SQLQuery sql() {
return new SQLQuery(getClass());
public SqlQuery sql() {
return new SqlQuery(getClass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
*/
package com.baomidou.mybatisplus.mapper;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* <p>
Expand All @@ -39,20 +38,21 @@
* @author Caratacus
* @Date 2016-12-11
*/
public class SQLQuery {
private static final Log logger = LogFactory.getLog(SQLQuery.class);
public class SqlQuery {
private static final Log logger = LogFactory.getLog(SqlQuery.class);
// 单例Query
public static final SqlQuery query = new SqlQuery();
private SqlSessionFactory sqlSessionFactory;
private TableInfo tableInfo;

public SQLQuery() {
public SqlQuery() {
this.tableInfo = TableInfoHelper.getRandomTableInfo();
String configMark = tableInfo.getConfigMark();
GlobalConfiguration globalConfiguration = GlobalConfiguration.GlobalConfig(configMark);
this.sqlSessionFactory = globalConfiguration.getSqlSessionFactory();
}

public SQLQuery(Class<?> clazz) {
public SqlQuery(Class<?> clazz) {
this.tableInfo = SqlHelper.table(clazz);
String configMark = tableInfo.getConfigMark();
GlobalConfiguration globalConfiguration = GlobalConfiguration.GlobalConfig(configMark);
Expand Down Expand Up @@ -103,8 +103,8 @@ public List<Map<String, Object>> selectPage(Pagination page, String sql, Object.
*
* @return
*/
public static SQLQuery db() {
return new SQLQuery();
public static SqlQuery db() {
return query;
}

/**
Expand All @@ -113,8 +113,8 @@ public static SQLQuery db() {
* @param clazz
* @return
*/
public static SQLQuery db(Class<?> clazz) {
return new SQLQuery(clazz);
public static SqlQuery db(Class<?> clazz) {
return new SqlQuery(clazz);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/
package com.baomidou.mybatisplus.test.mysql;

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

import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.junit.Test;

import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
import com.baomidou.mybatisplus.mapper.SQLQuery;
import com.baomidou.mybatisplus.mapper.SqlQuery;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.junit.Test;

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

/**
* <p>
Expand All @@ -49,31 +48,31 @@ public void test1() {
SqlSessionFactory sessionFactory = mf.build(in);
TableInfoHelper.initSqlSessionFactory(sessionFactory);

boolean b = SQLQuery.db().insert("INSERT INTO `test` (`id`, `type`) VALUES ('107880983085826048', 't1021')");
boolean b = SqlQuery.db().insert("INSERT INTO `test` (`id`, `type`) VALUES ('107880983085826048', 't1021')");
System.out.println(b);
Assert.assertTrue(b);
boolean b1 = SQLQuery.db().update("UPDATE `test` SET `type`='tttttttt' WHERE (`id`=107880983085826048)");
boolean b1 = SqlQuery.db().update("UPDATE `test` SET `type`='tttttttt' WHERE (`id`=107880983085826048)");
System.out.println(b1);

Assert.assertTrue(b1);
List<Map<String, Object>> maps = SQLQuery.db().selectList("select * from test WHERE (`id`=107880983085826048)");
List<Map<String, Object>> maps = SqlQuery.db().selectList("select * from test WHERE (`id`=107880983085826048)");
System.out.println(maps);
String type = (String) maps.get(0).get("type");
System.out.println(type);
Assert.assertEquals("tttttttt", type);
boolean b2 = SQLQuery.db().delete("DELETE from test WHERE (`id`=107880983085826048)");
boolean b2 = SqlQuery.db().delete("DELETE from test WHERE (`id`=107880983085826048)");
System.out.println(b2);
Assert.assertTrue(b2);
List<Map<String, Object>> maps1 = SQLQuery.db()
List<Map<String, Object>> maps1 = SqlQuery.db()
.selectList("select * from test WHERE (`id`=107880983085826048)");
System.out.println(maps1);
if (CollectionUtils.isEmpty(maps1)) {
maps1 = null;
}
Assert.assertNull(maps1);
List<Map<String, Object>> mapPage = SQLQuery.db().selectPage(new Pagination(1, 5), "select * from test ");
List<Map<String, Object>> mapPage = SqlQuery.db().selectPage(new Pagination(1, 5), "select * from test ");
System.out.println(mapPage);
int i = SQLQuery.db().selectCount("select count(0) from test ");
int i = SqlQuery.db().selectCount("select count(0) from test ");
System.out.println("count:" + i);

}
Expand Down

0 comments on commit 7a1ce9c

Please sign in to comment.