From 3fde78070037d46fb2af7240de41b47a8c3fab4a Mon Sep 17 00:00:00 2001
From: ChenJiaHe <763432473@qq.com>
Date: 星期五, 16 十月 2020 11:53:43 +0800
Subject: [PATCH] 共用新增改查,action直接引用CommonService,service直接引用CommonDao

---
 src/main/java/com/hx/common/service/impl/CommonServiceImpl.java |   95 +++++++++++
 src/main/java/com/hx/common/service/impl/CommonDaoImpl.java     |   98 ++++++++++++
 src/main/java/com/hx/common/service/CommonService.java          |  101 ++++++++++++
 src/main/java/com/hx/mybatisTool/SqlParam.java                  |   76 +++++++++
 src/main/java/com/hx/common/dao/CommonDao.java                  |  101 ++++++++++++
 5 files changed, 471 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/common/dao/CommonDao.java b/src/main/java/com/hx/common/dao/CommonDao.java
new file mode 100644
index 0000000..44f7419
--- /dev/null
+++ b/src/main/java/com/hx/common/dao/CommonDao.java
@@ -0,0 +1,101 @@
+package com.hx.common.dao;
+import com.hx.mybatisTool.SqlParam;
+import com.hx.mybatisTool.SqlSentence;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * dao閫氱敤鎺ュ彛
+ * @author ChenJiaHe
+ * @Date 2020-10-14
+ */
+public interface CommonDao {
+
+	/**鏂板锛岃繑鍥炰富閿�
+	 * @param mapperClass mapper绫�
+	 * @param entity 瀹炰綋绫�
+	 * @return 鏉℃暟
+	 */
+	<T extends Serializable>	int insert(Class<?> mapperClass, T entity);
+
+	/**鏌ヨ鏉℃暟
+	 * @param mapperClass mapper绫�
+	 * @param sqlParam  鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int selectCount(Class<?> mapperClass, SqlParam sqlParam);
+
+	/**鏌ヨ鍒楄〃锛岃繑鍥炲疄浣撶被鐨凩ist
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖list
+	 */
+	<T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ鍒楄〃锛岃繑鍥濵ap鐨凩ist
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return Map鐨凩ist
+	 */
+	<T extends Serializable> List<Map<String,Object>> selectListMap(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ锛岃繑鍥炲崟涓疄浣�
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> T selectOne(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ锛岃繑鍥炲崟涓猰ap
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> Map<String,Object> selectOneMap(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ锛岃繑鍥炲疄浣撶被娌℃湁澶ф暟鎹殑
+	 * @param mapperClass mapper绫�
+	 * @param object 鏁版嵁鏍囪瘑key
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> T selectOneByKey(Class<?> mapperClass, Object object);
+
+	/**鏌ヨ锛岃繑鍥炲疄浣撶被鏈夊ぇ鏁版嵁鐨�
+	 * @param mapperClass mapper绫�
+	 * @param object 鏁版嵁鏍囪瘑key
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> T selectOneByKeyBlob(Class<?> mapperClass, Object object);
+
+	/**鏇存柊锛岃繑鍥炴洿鏂版暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param sqlParam 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int updateWhere(Class<?> mapperClass, SqlParam sqlParam);
+
+	/**鏇存柊锛岃繑鍥炴洿鏂版暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param entity 瀹炰綋绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int updateAll(Class<?> mapperClass, T entity);
+
+	/**鍒犻櫎锛岃繑鍥炲垹闄ゆ暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param sqlParam 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int deleteWhere(Class<?> mapperClass, SqlParam sqlParam);
+
+	/**鍒犻櫎锛岃繑鍥炲垹闄ゆ暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param object 鏁版嵁鏍囪瘑key
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int deleteById(Class<?> mapperClass, Object object);
+	
+}
\ No newline at end of file
diff --git a/src/main/java/com/hx/common/service/CommonService.java b/src/main/java/com/hx/common/service/CommonService.java
new file mode 100644
index 0000000..94fbdd9
--- /dev/null
+++ b/src/main/java/com/hx/common/service/CommonService.java
@@ -0,0 +1,101 @@
+package com.hx.common.service;
+import com.hx.mybatisTool.SqlParam;
+import com.hx.mybatisTool.SqlSentence;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * dao閫氱敤鎺ュ彛
+ * @author ChenJiaHe
+ * @Date 2020-10-14
+ */
+public interface CommonService {
+
+	/**鏂板锛岃繑鍥炰富閿�
+	 * @param mapperClass mapper绫�
+	 * @param entity 瀹炰綋绫�
+	 * @return 鏉℃暟
+	 */
+	<T extends Serializable> int insert(Class<?> mapperClass, T entity);
+
+	/**鏌ヨ鏉℃暟
+	 * @param mapperClass mapper绫�
+	 * @param sqlParam  鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int selectCount(Class<?> mapperClass, SqlParam sqlParam);
+
+	/**鏌ヨ鍒楄〃锛岃繑鍥炲疄浣撶被鐨凩ist
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖list
+	 */
+	<T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ鍒楄〃锛岃繑鍥濵ap鐨凩ist
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return Map鐨凩ist
+	 */
+	<T extends Serializable> List<Map<String,Object>> selectListMap(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ锛岃繑鍥炲崟涓疄浣�
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> T selectOne(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ锛岃繑鍥炲崟涓猰ap
+	 * @param mapperClass mapper绫�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> Map<String,Object> selectOneMap(Class<?> mapperClass, SqlSentence sqlSentence);
+
+	/**鏌ヨ锛岃繑鍥炲疄浣撶被娌℃湁澶ф暟鎹殑
+	 * @param mapperClass mapper绫�
+	 * @param object 鏁版嵁鏍囪瘑key
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> T selectOneByKey(Class<?> mapperClass, Object object);
+
+	/**鏌ヨ锛岃繑鍥炲疄浣撶被鏈夊ぇ鏁版嵁鐨�
+	 * @param mapperClass mapper绫�
+	 * @param object 鏁版嵁鏍囪瘑key
+	 * @return 杩斿洖瀹炰綋绫�
+	 */
+	<T extends Serializable> T selectOneByKeyBlob(Class<?> mapperClass, Object object);
+
+	/**鏇存柊锛岃繑鍥炴洿鏂版暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param sqlParam 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int updateWhere(Class<?> mapperClass, SqlParam sqlParam);
+
+	/**鏇存柊锛岃繑鍥炴洿鏂版暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param entity 瀹炰綋绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int updateAll(Class<?> mapperClass, T entity);
+
+	/**鍒犻櫎锛岃繑鍥炲垹闄ゆ暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param sqlParam 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int deleteWhere(Class<?> mapperClass, SqlParam sqlParam);
+
+	/**鍒犻櫎锛岃繑鍥炲垹闄ゆ暟閲�
+	 * @param mapperClass mapper绫�
+	 * @param object 鏁版嵁鏍囪瘑key
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int deleteById(Class<?> mapperClass, Object object);
+	
+}
\ No newline at end of file
diff --git a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java
new file mode 100644
index 0000000..4bc281b
--- /dev/null
+++ b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java
@@ -0,0 +1,98 @@
+package com.hx.common.service.impl;
+
+import com.hx.common.dao.CommonDao;
+import com.hx.mybatisTool.SqlParam;
+import com.hx.mybatisTool.SqlSentence;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+
+/**褰撳墠鏄病鏈変簨鍔″鐞嗭紝鍒囧嬁鐩存帴浣跨敤*/
+@Service
+public class CommonDaoImpl implements CommonDao {
+
+    @Resource(name = "sqlSessionFactory")
+    protected SqlSessionFactory sqlSessionFactory;
+
+    /**缁勬嫾XML鏂囦欢sql鐨刬d*/
+    private <T> String getStatement(Class<T> clazz, String prefix){
+        return clazz.getName()+"."+prefix;
+    }
+
+    /**鎻掑叆*/
+    @Override
+    public <T extends Serializable> int insert(Class<?> mapperClass,T entity) {
+        return sqlSessionFactory.openSession().insert(getStatement(mapperClass,"insert"),entity);
+    }
+
+    /**鑾峰彇鏉℃暟*/
+    @Override
+    public <T extends Serializable> int selectCount(Class<?> mapperClass,SqlParam sqlParam) {
+        return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectCount"),sqlParam);
+    }
+
+    /**鑾峰彇鍒楄〃*/
+    @Override
+    public <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return sqlSessionFactory.openSession().selectList(getStatement(mapperClass,"selectList"),sqlSentence);
+    }
+
+    /**鑾峰彇鍒楄〃*/
+    @Override
+    public <T extends Serializable> List<Map<String, Object>> selectListMap(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return sqlSessionFactory.openSession().selectList(getStatement(mapperClass,"selectListMap"),sqlSentence);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁*/
+    @Override
+    public <T extends Serializable> T selectOne(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectOne"),sqlSentence);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁*/
+    @Override
+    public <T extends Serializable> Map<String, Object> selectOneMap(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectOneMap"),sqlSentence);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁-涓嶅惈longText鐨勬暟鎹�*/
+    @Override
+    public <T extends Serializable> T selectOneByKey(Class<?> mapperClass,Object object) {
+        return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectOneByKey"),object);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁-鍚玪ongText鐨勬暟鎹�*/
+    @Override
+    public <T extends Serializable> T selectOneByKeyBlob(Class<?> mapperClass,Object object) {
+        return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectOneByKeyBlob"),object);
+    }
+
+    /**鏇存柊*/
+    @Override
+    public <T extends Serializable> int updateWhere(Class<?> mapperClass,SqlParam sqlParam) {
+        return sqlSessionFactory.openSession().update(getStatement(mapperClass,"updateWhere"),sqlParam);
+    }
+
+    /**鍏ㄩ儴瀛楁鏇存柊*/
+    @Override
+    public <T extends Serializable> int updateAll(Class<?> mapperClass,T entity) {
+        return sqlSessionFactory.openSession().update(getStatement(mapperClass,"updateAll"),entity);
+    }
+
+    /**鍒犻櫎*/
+    @Override
+    public <T extends Serializable> int deleteWhere(Class<?> mapperClass,SqlParam sqlParam) {
+        return sqlSessionFactory.openSession().delete(getStatement(mapperClass,"deleteWhere"),sqlParam);
+    }
+
+    /**鍒犻櫎*/
+    @Override
+    public <T extends Serializable> int deleteById(Class<?> mapperClass,Object object) {
+        return sqlSessionFactory.openSession().delete(getStatement(mapperClass,"deleteById"),object);
+    }
+
+}
diff --git a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java
new file mode 100644
index 0000000..668f428
--- /dev/null
+++ b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java
@@ -0,0 +1,95 @@
+package com.hx.common.service.impl;
+
+import com.hx.common.dao.CommonDao;
+import com.hx.common.service.CommonService;
+import com.hx.mybatisTool.SqlParam;
+import com.hx.mybatisTool.SqlSentence;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+
+/**褰撳墠鏄湁浜嬪姟澶勭悊*/
+@Transactional
+@Service
+public class CommonServiceImpl implements CommonService {
+
+    @Resource
+    private CommonDao commonDao;
+
+    /**鎻掑叆*/
+    @Override
+    public <T extends Serializable> int insert(Class<?> mapperClass,T entity) {
+        return commonDao.insert(mapperClass,entity);
+    }
+
+    /**鑾峰彇鏉℃暟*/
+    @Override
+    public <T extends Serializable> int selectCount(Class<?> mapperClass,SqlParam sqlParam) {
+        return commonDao.selectCount(mapperClass,sqlParam);
+    }
+
+    /**鑾峰彇鍒楄〃*/
+    @Override
+    public <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return commonDao.selectList(mapperClass,sqlSentence);
+    }
+
+    /**鑾峰彇鍒楄〃*/
+    @Override
+    public <T extends Serializable> List<Map<String, Object>> selectListMap(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return commonDao.selectListMap(mapperClass,sqlSentence);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁*/
+    @Override
+    public <T extends Serializable> T selectOne(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return commonDao.selectOne(mapperClass,sqlSentence);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁*/
+    @Override
+    public <T extends Serializable> Map<String, Object> selectOneMap(Class<?> mapperClass, SqlSentence sqlSentence) {
+        return commonDao.selectOneMap(mapperClass,sqlSentence);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁-涓嶅惈longText鐨勬暟鎹�*/
+    @Override
+    public <T extends Serializable> T selectOneByKey(Class<?> mapperClass,Object object) {
+        return commonDao.selectOneByKey(mapperClass,object);
+    }
+
+    /**鑾峰彇鍗曟潯鏁版嵁-鍚玪ongText鐨勬暟鎹�*/
+    @Override
+    public <T extends Serializable> T selectOneByKeyBlob(Class<?> mapperClass,Object object) {
+        return commonDao.selectOneByKeyBlob(mapperClass,object);
+    }
+
+    /**鏇存柊*/
+    @Override
+    public <T extends Serializable> int updateWhere(Class<?> mapperClass,SqlParam sqlParam) {
+        return commonDao.updateWhere(mapperClass,sqlParam);
+    }
+
+    /**鍏ㄩ儴瀛楁鏇存柊*/
+    @Override
+    public <T extends Serializable> int updateAll(Class<?> mapperClass,T entity) {
+        return commonDao.updateAll(mapperClass,entity);
+    }
+
+    /**鍒犻櫎*/
+    @Override
+    public <T extends Serializable> int deleteWhere(Class<?> mapperClass,SqlParam sqlParam) {
+        return commonDao.deleteWhere(mapperClass,sqlParam);
+    }
+
+    /**鍒犻櫎*/
+    @Override
+    public <T extends Serializable> int deleteById(Class<?> mapperClass,Object object) {
+        return commonDao.deleteById(mapperClass,object);
+    }
+
+}
diff --git a/src/main/java/com/hx/mybatisTool/SqlParam.java b/src/main/java/com/hx/mybatisTool/SqlParam.java
new file mode 100644
index 0000000..2586164
--- /dev/null
+++ b/src/main/java/com/hx/mybatisTool/SqlParam.java
@@ -0,0 +1,76 @@
+package com.hx.mybatisTool;
+
+import com.hx.exception.TipsException;
+import com.hx.util.SimpleTool;
+
+import java.util.Map;
+
+/**
+ * mybatis 鑷畾涔夊鐞唖ql璇彞
+ * @author chenjiahe
+ * @Data: 2020-06-08
+ */
+public class SqlParam {
+	
+	private String whereSentence = "1=1";
+	private String updateSentence;
+	private Map<String,Object> m;
+	
+	/********************mother****************************/
+	/**
+	 * 鏌ヨ鐨勮鍙�
+	 * @param sql 濡傦細id = #{m.userId} order by age
+	 * @param values 瀛樻斁鐨勫�煎锛歷alues.put("userId","123456")
+	 */
+	public void sqlWhere(String sql,Map<String,Object> values) {
+		if(!SimpleTool.checkNotNull(values)){
+			throw new TipsException("values is null");
+		}
+		if(!SimpleTool.checkNotNull(sql)) {
+			sql = "1=1";
+		}
+		whereSentence = sql;
+		m = values;
+	}
+	
+	/**
+	 * 鏇存柊璇彞鐨勮鍙�
+	 * @param sql 濡傦細name = #{m.name},age = ? Where id = #{m.id}
+	 * @param values 瀛樻斁鐨勫��
+	 */
+	public void sqlUpdate(String sql,Map<String,Object> values) {
+		if(!SimpleTool.checkNotNull(values)){
+			throw new TipsException("values is null");
+		}
+		m = values;
+		updateSentence = sql;
+	}
+	
+	/************************************************************************/
+
+	public String getWhereSentence() {
+		return whereSentence;
+	}
+
+	public void setWhereSentence(String whereSentence) {
+		this.whereSentence = whereSentence;
+	}
+
+	public String getUpdateSentence() {
+		return updateSentence;
+	}
+
+	public void setUpdateSentence(String updateSentence) {
+		this.updateSentence = updateSentence;
+	}
+
+	public Map<String, Object> getM() {
+		return m;
+	}
+
+	public void setM(Map<String, Object> m) {
+		this.m = m;
+	}
+
+}
+

--
Gitblit v1.8.0