封装基础的ssm dao service serviceImpl 基础增删改差代码,只需条件查询时写一个sql
This commit is contained in:
+8
@@ -17,6 +17,14 @@ public class BaseConst {
|
||||
|
||||
public static final String BASEMSG_PREFIX = DEFAULT_VALIDATE_CODE+ ExceptionUtil.split;
|
||||
|
||||
/**
|
||||
* 分页排序的分隔符
|
||||
*/
|
||||
public static final String ORDER_SPLIT = "_";
|
||||
/**
|
||||
* 分页排序的连接符号
|
||||
*/
|
||||
public static final String ORDER_CONTACT = " ";
|
||||
/**
|
||||
* 服务器响应数据字典
|
||||
*/
|
||||
|
||||
@@ -11,4 +11,5 @@
|
||||
组的形式.
|
||||
- 除此之外`mybatis`的`dao`层扫描的配置需要在项目中自行配置
|
||||
- 需要注意的是:当使用多数据源的时候,需要在使用的项目之中去除默认的数据源自动装配`@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)`
|
||||
- 对于数据源的切换方法是使用aop的方式对`@SwitchDataSource('数据源的名字')`进行拦截动态切换数据源,主数据源的名字是`master`,从数据源的名字是`slave`.对于没有注解默认的数据源是`master`.
|
||||
- 对于数据源的切换方法是使用aop的方式对`@SwitchDataSource('数据源的名字')`进行拦截动态切换数据源,主数据源的名字是`master`,从数据源的名字是`slave`.对于没有注解默认的数据源是`master`.
|
||||
- PageHelper只对紧跟着的第一个SQL语句起作用
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.micro.fast.common.dao;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SsmMapper<T,ID> {
|
||||
|
||||
int deleteByPrimaryKey(ID id);
|
||||
@@ -16,4 +18,11 @@ public interface SsmMapper<T,ID> {
|
||||
int updateByPrimaryKeyWithBLOBs(T record);
|
||||
|
||||
int updateByPrimaryKey(T record);
|
||||
|
||||
/**
|
||||
* 按条件分页查询
|
||||
* @param record
|
||||
* @return
|
||||
*/
|
||||
List<T> selectByCondition(T record);
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ public interface SsmService<T,ID> {
|
||||
/**
|
||||
* 按条件分页查询
|
||||
*/
|
||||
ServerResponse getByCondition(T pojo);
|
||||
ServerResponse getByCondition(T pojo,int pageNum,int pageSize,String orderBy);
|
||||
/**
|
||||
* 修改
|
||||
*/
|
||||
|
||||
+38
-7
@@ -1,13 +1,18 @@
|
||||
package com.micro.fast.common.service.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.micro.fast.boot.starter.common.exception.SystemException;
|
||||
import com.micro.fast.boot.starter.common.response.BaseConst;
|
||||
import com.micro.fast.boot.starter.common.response.ServerResponse;
|
||||
import com.micro.fast.common.dao.SsmMapper;
|
||||
import com.micro.fast.common.service.SsmService;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* T表示的是实体类型,ID表示的是主键类型,DAO表示的是dao层的类型
|
||||
@@ -17,7 +22,7 @@ import java.io.Serializable;
|
||||
public abstract class SsmServiceImpl<T, ID extends Serializable, MAPPER extends SsmMapper<T,ID>> implements SsmService<T,ID>{
|
||||
|
||||
/**
|
||||
* 这个属性一定要使用子类的覆盖
|
||||
* 这个属性一定要使用子类的覆盖,不然会报空指针
|
||||
*/
|
||||
private MAPPER mapper;
|
||||
|
||||
@@ -38,13 +43,39 @@ public abstract class SsmServiceImpl<T, ID extends Serializable, MAPPER extends
|
||||
return ServerResponse.successData(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象的,提示开发者条件化查询需要自定义实现
|
||||
* @param pojo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public abstract ServerResponse getByCondition(T pojo);
|
||||
public ServerResponse getByCondition(T pojo,int pageSize,int pageNum,String orderBy){
|
||||
// 判断是否排序
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
}else{
|
||||
// 是否排序的标记
|
||||
boolean isOrderBy = false;
|
||||
// 判断传入的排序条件是否正确
|
||||
String[] split = orderBy.split(BaseConst.ORDER_SPLIT);
|
||||
if (split.length==2){
|
||||
Class<?> aClass = pojo.getClass();
|
||||
Field[] declaredFields = aClass.getDeclaredFields();
|
||||
for (Field declaredField : declaredFields) {
|
||||
String name = declaredField.getName();
|
||||
if (name.equals(split[0])){
|
||||
if (split[1].equals("desc")||split[1].equals("asc")){
|
||||
PageHelper.startPage(pageNum, pageSize,split[0]+BaseConst.ORDER_CONTACT+split[1]);
|
||||
isOrderBy = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//如果排序条件不正确就简单分页查询
|
||||
if (isOrderBy == false){
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
}
|
||||
}
|
||||
List<T> list = mapper.selectByCondition(pojo);
|
||||
PageInfo pageInfo = new PageInfo(list);
|
||||
return ServerResponse.successData(pageInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerResponse update(T pojo) {
|
||||
|
||||
@@ -43,9 +43,11 @@ public class UpmsSystemController {
|
||||
|
||||
@ApiOperation("根据条件分页查询")
|
||||
@GetMapping
|
||||
public ServerResponse getSystemByCondition(){
|
||||
|
||||
return null;
|
||||
public ServerResponse getSystemByCondition(UpmsSystem upmsSystem,
|
||||
@RequestParam(defaultValue = "1",required = false) int pageNum,
|
||||
@RequestParam(defaultValue = "10",required = false)int pageSize,
|
||||
@RequestParam(required = false) String orderBy){
|
||||
return upmsSystemService.getByCondition(upmsSystem,pageNum,pageSize,orderBy);
|
||||
}
|
||||
|
||||
@ApiOperation("修改系统信息")
|
||||
@@ -62,6 +64,4 @@ public class UpmsSystemController {
|
||||
@NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){
|
||||
return upmsSystemService.deleteById(Integer.valueOf(id));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,17 +3,28 @@ package com.micro.fast.upms.dao;
|
||||
import com.micro.fast.common.dao.SsmMapper;
|
||||
import com.micro.fast.upms.pojo.UpmsSystem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UpmsSystemMapper extends SsmMapper<UpmsSystem,Integer> {
|
||||
|
||||
@Override
|
||||
int deleteByPrimaryKey(Integer id);
|
||||
|
||||
@Override
|
||||
int insert(UpmsSystem record);
|
||||
|
||||
@Override
|
||||
int insertSelective(UpmsSystem record);
|
||||
|
||||
@Override
|
||||
UpmsSystem selectByPrimaryKey(Integer id);
|
||||
|
||||
@Override
|
||||
int updateByPrimaryKeySelective(UpmsSystem record);
|
||||
|
||||
@Override
|
||||
int updateByPrimaryKey(UpmsSystem record);
|
||||
|
||||
@Override
|
||||
List<UpmsSystem> selectByCondition(UpmsSystem record);
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.micro.fast.upms.service.impl;
|
||||
|
||||
import com.micro.fast.boot.starter.common.response.ServerResponse;
|
||||
import com.micro.fast.common.service.impl.SsmServiceImpl;
|
||||
import com.micro.fast.upms.dao.UpmsSystemMapper;
|
||||
import com.micro.fast.upms.pojo.UpmsSystem;
|
||||
@@ -13,16 +12,12 @@ import org.springframework.stereotype.Service;
|
||||
* @author lsy
|
||||
*/
|
||||
@Service
|
||||
public class UpmSystemServiceImpl extends SsmServiceImpl<UpmsSystem,Integer,UpmsSystemMapper> implements UpmsSystemService<UpmsSystem,Integer>,InitializingBean {
|
||||
public class UpmSystemServiceImpl extends SsmServiceImpl<UpmsSystem,Integer,UpmsSystemMapper>
|
||||
implements UpmsSystemService<UpmsSystem,Integer>,InitializingBean {
|
||||
|
||||
@Autowired
|
||||
private UpmsSystemMapper mapper;
|
||||
|
||||
@Override
|
||||
public ServerResponse getByCondition(UpmsSystem pojo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在这个bean初始化完成后覆盖父类的mapper
|
||||
*/
|
||||
|
||||
@@ -162,4 +162,21 @@
|
||||
orders = #{orders,jdbcType=BIGINT}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
|
||||
<!--自定义的-->
|
||||
<!--根据条件分页查询信息-->
|
||||
<select id="selectByCondition" resultType="com.micro.fast.upms.pojo.UpmsSystem">
|
||||
SELECT <include refid="Base_Column_List"/> FROM upms_system
|
||||
<where>
|
||||
<if test="title != null">
|
||||
title like "%"#{title}"%"
|
||||
</if>
|
||||
<if test="basepath !=null">
|
||||
and basepath like "%"#{basepath}"%"
|
||||
</if>
|
||||
<if test="name != null">
|
||||
and name like "%"#{name}"%"
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user