Merge remote-tracking branch 'origin/master'
| | |
| | | package com.hx.common; |
| | | |
| | | import com.hx.common.service.CommonService; |
| | | import com.hx.exception.ParamException; |
| | | import com.hx.exception.ServiceException; |
| | | import org.springframework.beans.propertyeditors.CustomDateEditor; |
| | |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import org.springframework.web.context.request.WebRequest; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.text.DateFormat; |
| | |
| | | |
| | | public class BaseController { |
| | | |
| | | @Resource |
| | | protected CommonService commonService; |
| | | |
| | | /*请不要声明变量,会导致不安全,因为这个是单列*/ |
| | | |
| | | //只需要加上下面这段即可,注意不能忘记注解 |
New file |
| | |
| | | package com.hx.common.dao; |
| | | 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 sqlSentence 查询参数类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**查询列表,返回实体类的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 sqlSentence 查询参数类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int updateWhere(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**更新,返回更新数量 |
| | | * @param mapperClass mapper类 |
| | | * @param entity 实体类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int updateAll(Class<?> mapperClass, T entity); |
| | | |
| | | /**删除,返回删除数量 |
| | | * @param mapperClass mapper类 |
| | | * @param sqlSentence 查询参数类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int deleteWhere(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**删除,返回删除数量 |
| | | * @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.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 sqlSentence 查询参数类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**查询列表,返回实体类的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 sqlSentence 查询参数类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int updateWhere(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**更新,返回更新数量 |
| | | * @param mapperClass mapper类 |
| | | * @param entity 实体类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int updateAll(Class<?> mapperClass, T entity); |
| | | |
| | | /**删除,返回删除数量 |
| | | * @param mapperClass mapper类 |
| | | * @param sqlSentence 查询参数类 |
| | | * @return 返回条数 |
| | | */ |
| | | <T extends Serializable> int deleteWhere(Class<?> mapperClass, SqlSentence sqlSentence); |
| | | |
| | | /**删除,返回删除数量 |
| | | * @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.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,SqlSentence sqlSentence) { |
| | | return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectCount"),sqlSentence); |
| | | } |
| | | |
| | | /**获取列表*/ |
| | | @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,SqlSentence sqlSentence) { |
| | | return sqlSessionFactory.openSession().update(getStatement(mapperClass,"updateWhere"),sqlSentence); |
| | | } |
| | | |
| | | /**全部字段更新*/ |
| | | @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,SqlSentence sqlSentence) { |
| | | return sqlSessionFactory.openSession().delete(getStatement(mapperClass,"deleteWhere"),sqlSentence); |
| | | } |
| | | |
| | | /**删除*/ |
| | | @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.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,SqlSentence sqlSentence) { |
| | | return commonDao.selectCount(mapperClass,sqlSentence); |
| | | } |
| | | |
| | | /**获取列表*/ |
| | | @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,SqlSentence sqlSentence) { |
| | | return commonDao.updateWhere(mapperClass,sqlSentence); |
| | | } |
| | | |
| | | /**全部字段更新*/ |
| | | @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,SqlSentence sqlSentence) { |
| | | return commonDao.deleteWhere(mapperClass,sqlSentence); |
| | | } |
| | | |
| | | /**删除*/ |
| | | @Override |
| | | public <T extends Serializable> int deleteById(Class<?> mapperClass,Object object) { |
| | | return commonDao.deleteById(mapperClass,object); |
| | | } |
| | | |
| | | } |
| | |
| | | package com.hx.mybatisTool; |
| | | |
| | | import com.hx.exception.TipsException; |
| | | import com.hx.util.SimpleTool; |
| | | import com.hx.util.StringUtils; |
| | | |
| | | import java.util.Map; |
| | |
| | | m = values; |
| | | } |
| | | |
| | | /** |
| | | * 查询的语句 |
| | | * @param sql 如:id = #{m.userId} order by age DESC |
| | | * @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"; |
| | | } |
| | | sqlSentence = 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; |
| | | sqlSentence = sql; |
| | | } |
| | | |
| | | |
| | | /************************************************************************/ |
| | | |
| | |
| | | { |
| | | return (startPage - 1) * pageNum; |
| | | } |
| | | |
| | | } |
| | | |
| | | |