SSM框架表现层+业务层+持久层逻辑思路(持久层的数据获取调用的是远端接口),ajax动态前后台交互。

ssm框架分为三层表现层,业务层,持久层

1.持久层

持久层这一部分相对于传统的ssm框架来说是与mybatis打交道的,我接触到的都是创建接口然后在调用相对应的xml文件,完成对业务层的功能接口。接下来我会对远端接口调用时持久层该怎么写进行描述。

1-1.接口创建

public interface IPersist {

    public static final int QUERY_TYPE_ICCID = 1;
    public static final int QUERY_TYPE_IMSI = 2;
    //接口名字为queryProfiles,接口类型对应自己创建的javabean对象
    List<Profile> queryProfiles(int type, String values) throws ProfileException;
}

1-2.接口实现

@Service
public class PersistRemoteAPIImpl implements IPersist {
	/*
	 * send profile list to smsr by http post
	 */
	public List<Profile> queryProfiles(int t, String values) throws ProfileException {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		// FIXME populat cert signature data
		headers.add("x.auth", "nonce_str");
		headers.add("x.auth.nonce", "Authorization");
		switch (t) {
		case QUERY_TYPE_ICCID:
			headers.add("x.profile.query.type", "ICCID");
			break;
		case QUERY_TYPE_IMSI:
			headers.add("x.profile.query.type", "IMSI");
			break;
		default:
			System.out.println("获取失败");
			break;
		}
		headers.add("x.profile.query.key", "iccic");

		HttpEntity<String> entity = new HttpEntity<>(null, headers);

		ProfileQueryJSONResult result = restTemplate.postForObject("http://localhost:8080/项目名/../../", entity,
				ProfileQueryJSONResult.class);

		if (result.getResult() != 0) {
			throw new ProfileException("获取失败");
		}
		return result.getProfiles();

	}
}

这是借阅的别的博主的博客,希望大家一起进步。感谢分享ResTemplate经验的博主。ResTemplate介绍。

上面就是持久层调用远端接口采用的方法。

2.业务层

业务层这部分使用了注解的开发方法,利用注解调用了持久层接口。

2-1.业务层创建接口

public interface IProfileService {
    
   List<Profile> queryProfileByICCID(String iccid) throws ProfileException;
   
   List<Profile> queryProfileByIMSI(String imsi) throws ProfileException;
}

2-2.业务层接口实现

@Service
public class ProfileService implements IProfileService {

	@Autowired
	IPersist profilePersist;

         public List<Profile> queryProfileByICCID(String iccid) throws ProfileException {
		if (iccid == null || iccid.isEmpty()) {
			throw new ProfileException(" iccid is null");
		}

		//TODO call persist API to get pofiles
		return profilePersist.queryProfiles(IPersist.QUERY_TYPE_ICCID, iccid);
	}

	public List<Profile> queryProfileByIMSI(String imsi) throws ProfileException {
		if (imsi == null || imsi.isEmpty()) {
			throw new ProfileException(" imsi is null");
		}

		//TODO call persist API to get pofiles
		return profilePersist.queryProfiles(IPersist.QUERY_TYPE_IMSI, imsi);
	}
}

利用@Service@Autowired实现了注解开发,注解持久层的IPersist接口并创建了profilePersist对象,通过注解的方式,直接调用接口的方法而不用再生命对象。

3.表现层

表现层也是利用了注解的方式。

3-1表现层Controller类

@Controller
public class ProfileQueryController extends HttpServlet{
   
    @Autowired
    private IProfileService ProfileService;
    
    @RequestMapping(value="/ProfileQueryController")
    @ResponseBody
    public Map<String, Object> SearchProfilePage(HttpServletRequest request, HttpServletResponse response ,String query_type) throws Exception {
        
            Map<String, Object> map=new HashMap<String, Object>();  
            //TODO get iccid type from request
            if ("ICCID".equals(request.getParameter("query_type"))) {
                String iccidValue ="iccic";
                List<Profile> list = ProfileService.queryProfileByICCID(iccidValue);
                //TODO populate profile list to map;
                map.put("list", list);
            }
            if ("IMSI".equals(request.getParameter("query_type"))) {
                String imsiValue ="";
                List<Profile> list = ProfileService.queryProfileByICCID(imsiValue);
                //TODO populate profile list to map;
            }
            return map;   
    }
    
}

利用注解@Controller,@Autowired完成对象ProfileService的方法的实现。

创建map对象作为传输到前端的媒介。

Map<String, Object> map=new HashMap<String, Object>(); 

利用注解方法获取后端的数据。

List<Profile> list = ProfileService.queryProfileByICCID(iccidValue);

最后数据返回到前端。


3-2.前段接口实现。

利用ajax动态局部刷新实现前后台数据的交互。

<script type="text/javascript" >
function studnet(){  
    //搜索条件  
    var query= document.getElementById("selector1").value;
    var path = getpath()+'/ProfileQueryController'
    $.ajax({  
        url:path,
        type:"post",  
        dataType:"json",  
        data:{"query_type":query},  
        success:function(result){  
            var _html='';  
                _html +='<tr>'+  
                    '<td>'+1+'</td>'+  
                    '<td>'+result.list[0].operator+'</td>'+  
                    '<td>'+result.list[0].meta+'</td>'+  
                    '<td>'+result.list[0].meta.acc+'</td>'+  
                    '<td>'+result.list[0].status+'</td>'+  
                    '<td>'+result.list[0].status.state+'</td>'+   
                '</tr>';  
            $("#students").html(_html);
        }   
    });  
} 
</script>

ajax对应的前台控件为下:

<table class="table table-hover" id="table1">
    <thead>
        <tr>
	    <th>#</th>
	    <th>ICCID profile激活状态</th>
	    <th>profile激活时间</th>
	    <th>profile激活方式</th>
	    <th>客户信息</th>
	    <th>标签信息</th>
	</tr>
    </thead>
	<tbody id="students"></tbody>
</table>
大概逻辑就是这样^-^,希望对大家有一定帮助。






猜你喜欢

转载自blog.csdn.net/qq_31026587/article/details/80756044
今日推荐