---
title: "Java  数据库中组合字段操作解决方案"
author: "ljl.java"
date: "2013-01-29T21:47:43Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2013-02-05T14:41:17Z"
id: 1779401
url: "https://ljl-java.iteye.com/blog/1779401"
---

两个复选框分别对应数据库中SHOW\_CATEGORY中的值：

1:国家级

2:市级

1,2 国家级 和 市级   
![](http://dl.iteye.com/upload/attachment/0080/0273/78dbada1-950d-3acb-b7af-a672c96ee935.jpg)

   
![](http://dl.iteye.com/upload/attachment/0080/0275/421b8855-96a2-39d0-bf8f-06672918a641.jpg)  
 前端代码（使用Freemarker ）（虽然是操作同一字段但name要定义不同，方便后台存储返回）

&lt;input name="query\_show\_category" type="checkbox"  value="1" &lt;#if query\_show\_category?? && query\_show\_category=="1"&gt; checked="checked"&lt;/#if&gt;&gt;国家级&lt;/input&gt;

&lt;input name="query\_show\_category\_s" type="checkbox"  value="2" &lt;#if query\_show\_category\_s?? && query\_show\_category\_s=="2"&gt; checked="checked"&lt;/#if&gt;&gt;市级&lt;/input&gt;

&lt;input type="submit" value="&lt;@s.m "global.query"/&gt;" /&gt;

 

由于CMS项目 框架及接口以设定好，不易更改后台

show\_category=RequestUtils.getQueryParam(request,"query\_show\_category");

show\_category\_s=RequestUtils.getQueryParam(request,"query\_show\_category\_s");

分别 只能接受单个CheckBox 的值 如 “1”  或 “2” 无法得到组合的值 如“1,2” 

那这种组合值如何得到？

方案：根据后台接收字符串拼接形成组合值 在根据条件 过滤 就能得到 "1" ,"2","1,2" ,null几种情况

 后台代码：

String show\_category=""; //定义接收国家级checkbox

String show\_category\_s="";//定义接收市级级checkbox

String show\_compose=""; //定义接收拼接值如  (1,2)

String Comma=",";  //逗号

if(show\_category!=null ){

//这里只能接收一个参数!!需要接受组合参数

show\_category=RequestUtils.getQueryParam(request,"query\_show\_category");

show\_category\_s=RequestUtils.getQueryParam(request,"query\_show\_category\_s");

根据条件过滤逗号(单个值要过滤逗号 两个值要用逗号拼接)

if (RequestUtils.getQueryParam(request, "query\_show\_category") != null) {

show\_category = RequestUtils.getQueryParam(request,"query\_show\_category");

 

show\_compose = show\_category;

}

if (RequestUtils.getQueryParam(request, "query\_show\_category\_s") != null) {

show\_category\_s = RequestUtils.getQueryParam(request,"query\_show\_category\_s");

 

show\_compose = show\_category\_s;

}

/\*这里判断不够充分的化前面checkbox 单选会把show\_compose重置 效果

if(show\_category!=null && show\_category\_s!=null){

show\_compose=show\_category+Comma+show\_category\_s;

}

\*/

//如果出现条件影响变量重置时  应改为 大范围 排除掉条件来 控制小范围

if((show\_category!=null &&!show\_category.equals("")) && (show\_category\_s!=null) && !show\_category\_s.equals("")){

show\_compose=show\_category+Comma+show\_category\_s;

}else if(show\_category!=null &&!show\_category.equals(""))

{

show\_compose=show\_category;

}else if(show\_category\_s!=null && !show\_category\_s.equals("")){

show\_compose=show\_category\_s;

}

model.addAttribute("query\_show\_category", show\_category);

model.addAttribute("query\_show\_category\_s", show\_category\_s);

 

}

Pagination p = manager.getPageByRight(query\_Area\_id, query\_code\_name,

recommend\_flag, show\_compose, cpn(pageNo), CookieUtils

.getPageSize(request));

 

model.addAttribute("pagination", p);

 

 

 

另外补充一点 Freemaker中 在dom节点中的变量判断范围只限定在当前文本域中

&lt;input name="query\_show\_category" type="checkbox"  value="1" &lt;#if query\_show\_category?? && query\_show\_category=="1"&gt; checked="checked"&lt;/#if&gt;&gt;国家级&lt;/input&gt;