跳转至

java开发demo

项目结构

项目 说明
web-server controller层
service 接口实现层
api 接口层(所有的接口在这里定义)

假如现在要增加一个功能:通过给定的角色ID查询出角色的信息。

新建api

1) 在api项目中新建RoleService接口;

2) 在RoleService接口加上注解@RemoteService;

3) 在接口里面定义方法;若该方法只是查询,那么需要在方法上加注解@Transactional(readOnly = true);

import com.zxy.common.base.annotation.RemoteService;
import com.zxy.demo.project.jooq.tables.pojos.RoleEntity;

@RemoteService
public interface RoleService {

    @Transactional(readOnly = true)
    RoleEntity get(String id);
}

新建service

1) 在service项目中新建RoleServiceSupport类,实现RoleService接口;

2) 在RoleServiceSupport类上增加注解@Service

3) 注入CommonDao在set方法上增加注解@Autowired ;不允许在属性上使用@Autowired

4) 实现接口

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zxy.common.dao.support.CommonDao;
import com.zxy.demo.project.api.RoleService;
import com.zxy.demo.project.jooq.tables.pojos.RoleEntity;

@Service
public class RoleServiceSupport implements RoleService {

    private CommonDao<RoleEntity> roleDao;

    @Autowired
    public void setRoleDao(CommonDao<RoleEntity> roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public RoleEntity get(String id) {
        return roleDao.get(id);
    }

}

新建controller

1) 在web-server项目中新建RoleController类;

2) 在类上增加注解@Controller、@RequestMapping("/role");

3) 注入RoleService,在set方法上增加@Autowired注解;不允许在属性上使用@Autowired

4) 新增方法,使用RoleService的get方法;

@RequestMapping(value = "/{id}" , method = RequestMethod.GET)
@Param(name = "id", type = String.class, required = true)
@JSON("id,name,remark,nodeId,superRole,createTime")
@JSON("permissions.(id)")
public Role get(RequestContext requestContext) {
    return roleService.get(requestContext.get("id", String.class));
}

说明

注解 说明
@RequestMapping spring的注解,value指定映射的url,method指定请求的方法
@Param 前台传过来的参数,name:参数名称,type:参数类型,required:参数是不是必须有;如果有多个参数,则写多个@Param
@Permitted 权限注解,perms: 指明当前方法必须拥有的权限,roles: 指明当前方法必须有的角色;若不需要权限,则不使用该注解
@JSON 后台反回前台的字段,必须是当前实体的属性,尽量不使用*