共用新增改查,action直接引用CommonService,service直接引用CommonDao
New file |
| | |
| | | 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); |
| | | |
| | | /**查询列表,返回实体类的List |
| | | * @param mapperClass mapper类 |
| | | * @param sqlSentence 查询参数类 |
| | | * @return 返回list |
| | | */ |
| | | <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**查询列表,返回Map的List |
| | | * @param mapperClass mapper类 |
| | | * @param sqlSentence 查询参数类 |
| | | * @return Map的List |
| | | */ |
| | | <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); |
| | | |
| | | /**查询,返回单个map |
| | | * @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); |
| | | |
| | | } |
New file |
| | |
| | | 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); |
| | | |
| | | /**查询列表,返回实体类的List |
| | | * @param mapperClass mapper类 |
| | | * @param sqlSentence 查询参数类 |
| | | * @return 返回list |
| | | */ |
| | | <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**查询列表,返回Map的List |
| | | * @param mapperClass mapper类 |
| | | * @param sqlSentence 查询参数类 |
| | | * @return Map的List |
| | | */ |
| | | <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); |
| | | |
| | | /**查询,返回单个map |
| | | * @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); |
| | | |
| | | } |
New file |
| | |
| | | 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的id*/ |
| | | 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); |
| | | } |
| | | |
| | | /**获取单条数据-含longText的数据*/ |
| | | @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); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | 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); |
| | | } |
| | | |
| | | /**获取单条数据-含longText的数据*/ |
| | | @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); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.hx.mybatisTool; |
| | | |
| | | import com.hx.exception.TipsException; |
| | | import com.hx.util.SimpleTool; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * mybatis 自定义处理sql语句 |
| | | * @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 存放的值如:values.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; |
| | | } |
| | | |
| | | } |
| | | |