Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改模板默认使用服务器分页 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.stylefeng.guns.common.annotion;

import java.lang.annotation.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
Expand All @@ -11,7 +15,7 @@
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
@Target({ ElementType.METHOD,ElementType.TYPE})
public @interface DataSource {

String name() default "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.stylefeng.guns.common.constant.factory;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.state.Order;
import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.core.util.ToolUtil;

import javax.servlet.http.HttpServletRequest;

/**
* BootStrap Table默认的分页参数创建
*
Expand All @@ -17,8 +17,8 @@ public class PageFactory<T> {

public Page<T> defaultPage() {
HttpServletRequest request = HttpKit.getRequest();
int limit = Integer.valueOf(request.getParameter("limit"));
int offset = Integer.valueOf(request.getParameter("offset"));
int limit = Integer.valueOf(request.getParameter("limit")==null?"10":request.getParameter("limit"));
int offset = Integer.valueOf(request.getParameter("offset")==null?"0":request.getParameter("offset"));
String sort = request.getParameter("sort");
String order = request.getParameter("order");
if(ToolUtil.isEmpty(sort)){
Expand Down
46 changes: 32 additions & 14 deletions src/main/java/com/stylefeng/guns/core/aop/MultiSourceExAop.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.stylefeng.guns.core.aop;

import com.stylefeng.guns.common.annotion.DataSource;
import com.stylefeng.guns.common.constant.DSEnum;
import com.stylefeng.guns.core.mutidatesource.DataSourceContextHolder;
import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.util.GenericSignature.ClassSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import com.stylefeng.guns.common.annotion.DataSource;
import com.stylefeng.guns.common.constant.DSEnum;
import com.stylefeng.guns.core.mutidatesource.DataSourceContextHolder;

/**
*
Expand All @@ -31,26 +33,42 @@ public class MultiSourceExAop implements Ordered {

private Logger log = LoggerFactory.getLogger(this.getClass());


@Pointcut(value = "@annotation(com.stylefeng.guns.common.annotion.DataSource)")
//aop切面拦截,如果不添加父类拦截,则只能拦截显式public方法
@Pointcut(value = "execution(* com.baomidou.mybatisplus.service.impl.ServiceImpl.*(..)) && @target(com.stylefeng.guns.common.annotion.DataSource) || @annotation(com.stylefeng.guns.common.annotion.DataSource)")
private void cut() {

}

/**
* 如果类注解\@DataSource 则整个类使用该注解的数据源,类中的方法如果再\@DataSource 则优先使用方法的注解
* @param point
* @return
* @throws Throwable
*/
@Around("cut()")
public Object around(ProceedingJoinPoint point) throws Throwable {

Signature signature = point.getSignature();
MethodSignature methodSignature = null;
if (!(signature instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
methodSignature = (MethodSignature) signature;

Object target = point.getTarget();
Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
DataSource datasource=null;

DataSource datasource = currentMethod.getAnnotation(DataSource.class);
if (signature instanceof MethodSignature ) {//方法注解
methodSignature = (MethodSignature) signature;
Object target = point.getTarget();
Class<?> currentClass = target.getClass();
datasource = currentClass.getAnnotation(DataSource.class);//如果是父类方法,则先试取类注解
Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
if(currentMethod.getAnnotation(DataSource.class)!=null){//如果存在datasourc 则覆盖类注解
datasource = currentMethod.getAnnotation(DataSource.class);
}
}else if(signature instanceof ClassSignature){//类注解
Object target = point.getTarget();
Class<?> currentClass = target.getClass();
datasource = currentClass.getAnnotation(DataSource.class);
}else {
throw new IllegalArgumentException("该注解只能用于方法或类");
}

if(datasource != null){
DataSourceContextHolder.setDataSourceType(datasource.name());
log.debug("设置数据源为:" + datasource.name());
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ beetl:

################### spring配置 ###################
spring:
session:
store-type: redis
redis:
host: localhost
port: 6379
Expand Down
30 changes: 18 additions & 12 deletions src/main/resources/gunsTemplate/Controller.java.btl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import ${import};
* @Date ${tool.currentTime()}
*/
@Controller
@RequestMapping("/${context.bizEnName}")
@RequestMapping("/${context.moduleName}/${context.bizEnName}")
public class ${context.bizEnBigName}Controller extends BaseController {

private String PREFIX = "/${context.moduleName}/${context.bizEnName}/";

@Autowired
private I${context.bizEnBigName}Service ${context.bizEnName}Service;

/**
* 跳转到${context.bizChName}首页
Expand All @@ -37,6 +40,7 @@ public class ${context.bizEnBigName}Controller extends BaseController {
*/
@RequestMapping("/${context.bizEnName}_update/{${context.bizEnName}Id}")
public String ${context.bizEnName}Update(@PathVariable Integer ${context.bizEnName}Id, Model model) {
model.addAttribute("${context.bizEnName}", ${context.bizEnName}Service.selectById(${context.bizEnName}Id));
return PREFIX + "${context.bizEnName}_edit.html";
}

Expand All @@ -45,26 +49,28 @@ public class ${context.bizEnBigName}Controller extends BaseController {
*/
@RequestMapping(value = "/list")
@ResponseBody
public Object list(String condition) {
return null;
public Object list(${context.bizEnBigName} ${context.bizEnName}) {
Page<${context.bizEnBigName}> page =new PageFactory<${context.bizEnBigName}>().defaultPage();
Wrapper<${context.bizEnBigName}> ${context.bizEnName}Wrapper=new EntityWrapper<>(${context.bizEnName});
return super.packForBT(${context.bizEnName}Service.selectPage(page, ${context.bizEnName}Wrapper));
}

/**
* 新增${context.bizChName}
*/
@RequestMapping(value = "/add")
@ResponseBody
public Object add() {
return super.SUCCESS_TIP;
public Object add(${context.bizEnBigName} ${context.bizEnName}) {
return ${context.bizEnName}Service.insert(${context.bizEnName});
}

/**
* 删除${context.bizChName}
*/
@RequestMapping(value = "/delete")
@ResponseBody
public Object delete() {
return SUCCESS_TIP;
public Object delete(Integer id) {
return ${context.bizEnName}Service.deleteById(id);
}


Expand All @@ -73,16 +79,16 @@ public class ${context.bizEnBigName}Controller extends BaseController {
*/
@RequestMapping(value = "/update")
@ResponseBody
public Object update() {
return super.SUCCESS_TIP;
public Object update(${context.bizEnBigName} ${context.bizEnName}) {
return ${context.bizEnName}Service.updateById(${context.bizEnName});
}

/**
* ${context.bizChName}详情
*/
@RequestMapping(value = "/detail")
@ResponseBody
public Object detail() {
return null;
public Object detail(Integer id) {
return ${context.bizEnName}Service.selectById(id);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/gunsTemplate/Dao.java.btl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package ${dao.packageName};
* @author fengshuonan
* @Date ${tool.currentTime()}
*/
public interface ${context.bizEnBigName}Dao {
public interface ${context.bizEnBigName}Dao extends BaseMapper<${context.bizEnBigName}>{


}
6 changes: 4 additions & 2 deletions src/main/resources/gunsTemplate/Service.java.btl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ${service.packageName};

<% for(import in controller.imports!){ %>
import ${import};
<% } %>
/**
* ${context.bizChName}Service
*
* @author fengshuonan
* @Date ${tool.currentTime()}
*/
public interface I${context.bizEnBigName}Service {
public interface I${context.bizEnBigName}Service extends IService<${context.bizEnBigName}>{

}
3 changes: 1 addition & 2 deletions src/main/resources/gunsTemplate/ServiceImpl.java.btl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import ${import};
* @Date ${tool.currentTime()}
*/
@Service
public class ${context.bizEnBigName}ServiceImpl implements I${context.bizEnBigName}Service {

public class ${context.bizEnBigName}ServiceImpl extends ServiceImpl<${context.bizEnBigName}Dao,${context.bizEnBigName}> implements I${context.bizEnBigName}Service {

}
12 changes: 6 additions & 6 deletions src/main/resources/gunsTemplate/page.js.btl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ${context.bizEnBigName}.openAdd${context.bizEnBigName} = function () {
area: ['800px', '420px'], //宽高
fix: false, //不固定
maxmin: true,
content: Feng.ctxPath + '/${context.bizEnName}/${context.bizEnName}_add'
content: Feng.ctxPath + '/${context.moduleName}/${context.bizEnName}/${context.bizEnName}_add'
});
this.layerIndex = index;
};
Expand All @@ -58,7 +58,7 @@ ${context.bizEnBigName}.open${context.bizEnBigName}Detail = function () {
area: ['800px', '420px'], //宽高
fix: false, //不固定
maxmin: true,
content: Feng.ctxPath + '/${context.bizEnName}/${context.bizEnName}_update/' + ${context.bizEnBigName}.seItem.id
content: Feng.ctxPath + '/${context.moduleName}/${context.bizEnName}/${context.bizEnName}_update/' + ${context.bizEnBigName}.seItem.id
});
this.layerIndex = index;
}
Expand All @@ -69,13 +69,13 @@ ${context.bizEnBigName}.open${context.bizEnBigName}Detail = function () {
*/
${context.bizEnBigName}.delete = function () {
if (this.check()) {
var ajax = new $ax(Feng.ctxPath + "/${context.bizEnName}/delete", function (data) {
var ajax = new $ax(Feng.ctxPath + "/${context.moduleName}/${context.bizEnName}/delete", function (data) {
Feng.success("删除成功!");
${context.bizEnBigName}.table.refresh();
}, function (data) {
Feng.error("删除失败!" + data.responseJSON.message + "!");
});
ajax.set("${context.bizEnName}Id",this.seItem.id);
ajax.set("id",this.seItem.id);
ajax.start();
}
};
Expand All @@ -91,7 +91,7 @@ ${context.bizEnBigName}.search = function () {

$(function () {
var defaultColunms = ${context.bizEnBigName}.initColumn();
var table = new BSTable(${context.bizEnBigName}.id, "/${context.bizEnName}/list", defaultColunms);
table.setPaginationType("client");
var table = new BSTable(${context.bizEnBigName}.id, "/${context.moduleName}/${context.bizEnName}/list", defaultColunms);
table.setPaginationType("server");
${context.bizEnBigName}.table = table.init();
});
4 changes: 2 additions & 2 deletions src/main/resources/gunsTemplate/page_info.js.btl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ${context.bizEnBigName}InfoDlg.addSubmit = function() {
this.collectData();

//提交信息
var ajax = new $ax(Feng.ctxPath + "/${context.bizEnName}/add", function(data){
var ajax = new $ax(Feng.ctxPath + "/${context.moduleName}/${context.bizEnName}/add", function(data){
Feng.success("添加成功!");
window.parent.${context.bizEnBigName}.table.refresh();
${context.bizEnBigName}InfoDlg.close();
Expand All @@ -76,7 +76,7 @@ ${context.bizEnBigName}InfoDlg.editSubmit = function() {
this.collectData();

//提交信息
var ajax = new $ax(Feng.ctxPath + "/${context.bizEnName}/update", function(data){
var ajax = new $ax(Feng.ctxPath + "/${context.moduleName}/${context.bizEnName}/update", function(data){
Feng.success("修改成功!");
window.parent.${context.bizEnBigName}.table.refresh();
${context.bizEnBigName}InfoDlg.close();
Expand Down