diff --git a/mybatis-plus/pom.xml b/mybatis-plus/pom.xml index 8b5c3b8071..e61786d6b4 100644 --- a/mybatis-plus/pom.xml +++ b/mybatis-plus/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.baomidou mybatis-plus - 2.0-rc + 2.0 jar mybatis-plus diff --git a/mybatis-plus/src/main/java/com/baomidou/mybatisplus/activerecord/Model.java b/mybatis-plus/src/main/java/com/baomidou/mybatisplus/activerecord/Model.java index 810de74c5b..8ae7904e30 100644 --- a/mybatis-plus/src/main/java/com/baomidou/mybatisplus/activerecord/Model.java +++ b/mybatis-plus/src/main/java/com/baomidou/mybatisplus/activerecord/Model.java @@ -15,6 +15,15 @@ */ 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; @@ -24,14 +33,6 @@ 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; /** *

@@ -92,6 +93,9 @@ public boolean deleteById(Serializable id) { * @return */ public boolean deleteById() { + if (null == this.pkVal()) { + throw new MybatisPlusException("deleteById primaryKey is null."); + } return deleteById(this.pkVal()); } @@ -134,7 +138,7 @@ public boolean delete(Wrapper wrapper) { */ public boolean updateById() { if (null == this.pkVal()) { - throw new MybatisPlusException("primaryKey is null."); + throw new MybatisPlusException("updateById primaryKey is null."); } // updateById return SqlHelper.retBool(sqlSession().update(sqlStatement(SqlMethod.UPDATE_BY_ID), this)); @@ -204,6 +208,9 @@ public T selectById(Serializable id) { * @return */ public T selectById() { + if (null == this.pkVal()) { + throw new MybatisPlusException("selectById primaryKey is null."); + } return selectById(this.pkVal()); } diff --git a/mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlQuery.java b/mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlQuery.java index 05ca728766..576539589e 100644 --- a/mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlQuery.java +++ b/mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlQuery.java @@ -15,24 +15,25 @@ */ 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; /** *

- * SQLQuery直接执行SQL + * SqlQuery 执行 SQL *

* * @author Caratacus @@ -41,7 +42,7 @@ public class SqlQuery { private static final Log logger = LogFactory.getLog(SqlQuery.class); // 单例Query - public static final SqlQuery query = new SqlQuery(); + public static final SqlQuery SQL_QUERY = new SqlQuery(); private SqlSessionFactory sqlSessionFactory; private TableInfo tableInfo; @@ -54,8 +55,7 @@ public SqlQuery() { public SqlQuery(Class clazz) { this.tableInfo = SqlHelper.table(clazz); - String configMark = tableInfo.getConfigMark(); - GlobalConfiguration globalConfiguration = GlobalConfiguration.GlobalConfig(configMark); + GlobalConfiguration globalConfiguration = GlobalConfiguration.GlobalConfig(tableInfo.getConfigMark()); this.sqlSessionFactory = globalConfiguration.getSqlSessionFactory(); } @@ -104,7 +104,7 @@ public List> selectPage(Pagination page, String sql, Object. * @return */ public static SqlQuery db() { - return query; + return SQL_QUERY; } /** diff --git a/mybatis-plus/src/main/java/com/baomidou/mybatisplus/service/impl/ServiceImpl.java b/mybatis-plus/src/main/java/com/baomidou/mybatisplus/service/impl/ServiceImpl.java index 172247e4a8..4bf32c3575 100644 --- a/mybatis-plus/src/main/java/com/baomidou/mybatisplus/service/impl/ServiceImpl.java +++ b/mybatis-plus/src/main/java/com/baomidou/mybatisplus/service/impl/ServiceImpl.java @@ -67,6 +67,19 @@ protected SqlSession sqlSessionBatch() { return SqlHelper.sqlSessionBatch(currentModleClass()); } + /** + *

+ * 判断数据库操作是否成功 + *

+ * + * @param result + * 数据库操作返回影响条数 + * @return boolean + */ + public boolean retBool(int result) { + return result >= 1; + } + /** *

* TableId 注解存在更新记录,否插入一条记录 @@ -104,7 +117,7 @@ public boolean insertOrUpdate(T entity) { } public boolean insert(T entity) { - return SqlHelper.retBool(baseMapper.insert(entity)); + return retBool(baseMapper.insert(entity)); } public boolean insertBatch(List entityList) { @@ -166,30 +179,30 @@ public boolean insertBatch(List entityList, int batchSize) { } public boolean deleteById(Serializable id) { - return SqlHelper.retBool(baseMapper.deleteById(id)); + return retBool(baseMapper.deleteById(id)); } public boolean deleteByMap(Map columnMap) { if (MapUtils.isEmpty(columnMap)) { throw new MybatisPlusException("deleteByMap columnMap is empty."); } - return SqlHelper.retBool(baseMapper.deleteByMap(columnMap)); + return retBool(baseMapper.deleteByMap(columnMap)); } public boolean delete(Wrapper wrapper) { - return SqlHelper.retBool(baseMapper.delete(wrapper)); + return retBool(baseMapper.delete(wrapper)); } public boolean deleteBatchIds(List idList) { - return SqlHelper.retBool(baseMapper.deleteBatchIds(idList)); + return retBool(baseMapper.deleteBatchIds(idList)); } public boolean updateById(T entity) { - return SqlHelper.retBool(baseMapper.updateById(entity)); + return retBool(baseMapper.updateById(entity)); } public boolean update(T entity, Wrapper wrapper) { - return SqlHelper.retBool(baseMapper.update(entity, wrapper)); + return retBool(baseMapper.update(entity, wrapper)); } public boolean updateBatchById(List entityList) { diff --git a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java index 9c7bcd29ac..5c112aef5f 100644 --- a/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java +++ b/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java @@ -18,8 +18,8 @@ import java.util.HashMap; import java.util.Map; -import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.AutoGenerator; +import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; @@ -116,6 +116,16 @@ public void initMap() { }; mpg.setCfg(cfg); + // 自定义模板配置 + // TemplateConfig tc = new TemplateConfig(); + // tc.setController("..."); + // tc.setEntity("..."); + // tc.setMapper("..."); + // tc.setXml("..."); + // tc.setService("..."); + // tc.setServiceImpl("..."); + // mpg.setTemplate(tc); + // 执行生成 mpg.execute();