2015-10-12 编程小经验

酷酷酷

Q:mysql+mybatis 数据库里面的字段值为null,类型为int,在查询的时候,如何向java进行换转?

A: int类型的字段,值为null,从数据库里读出来,赋值给Integer,值为 null,赋值给int,值是0

    java :

	private Integer selfDegraNum; // 主动降级次数
	private int punishDegraNum; // 处罚降级次数
//	private Integer punishDegraNum; // 处罚降级次数

 

    数据库中的记录:

mysql> select self_degra_num,  punish_degra_num from company_certify_guaranty where state=1 and guaranty_type=1;
+----------------+------------------+
| self_degra_num | punish_degra_num |
+----------------+------------------+
|           NULL |             NULL |
+----------------+------------------+
1 row in set (0.01 sec)

 

     查出来的json值

"selfDegraNum":null,"punishDegraNum":0

 

最终结论: 持久化对象(pojo,dto,)里面不要使用基本类型,需要使用class类型,0和NULL明显是有很大的区别的,NULL是未定义,0是一个明确的值,虽然我们此种场景下认为“0,或者null都表示不做配置”

 

发散思考:能用类类型的地方就不用基本类型,基本类型需要考虑默认值,毕竟JAVA是一种面向对象语言,从一切都是对象的角度出来,使用类类型替换基本类型更加符合面向对象的设计思想

	public AjaxResult listCertifyProcess(String userName, String state, Integer pageSize, Integer currentPage, String promoteType, String startTime,

 

 

 酷酷酷

Q: 表里面的int型字段,允许为空,定义的时候是否要设置初始值为0 ?

A : 根据场景来定,如果需要对这个字段做自增,减少等操作,可以设置default 0,便于做++,否则保留值为NULL.

 

 酷酷酷

Q: mysql +mybatis,varchar类型,如果数据库里面是NULL,赋值给java的string,值是什么?

A:   NULL或者空串, 这是废话,一般来说返回空串是一种更加友好的方式,可以避免程序出错

      不管怎样,用StringUtils.isblank()进行判断是有必要的

 

 酷酷酷

Oracle 数据库里面没有空串,只有NULL,将oracle里面的NULL读出来给java String,得到的一定是空串,

将“”赋值给字段,oracle里面的存放的值是NULL

 

酷酷酷

 给常量起名字,集中对常量进行管理能够带来的易维护的好处:

1  常量的值不会杂七杂八的散落在代码中,想看到当前用到了哪些常量一目了然

2  常量有一个响亮的大写名称,标志了它的业务含义或者功能

3  修改常量的值会对所有用到常量的地方都生效

4  便于做资源的国际化

 

 

public class BaMerchantCompanyCertifyController extends BaseController {
	private static final String PROMOTE_TYPE_VOUCH = "1"; // 担保晋级方式
	private static final String PROMOTE_TYPE_SECOND = "2"; // 二次晋级方式
	private static final String TARGET_LEVEL_CERTIFIED = "认证商家";
	private static final String TARGET_LEVEL_GOLD = "金牌商家";
	private static final String PROMOTE_TYPE_VOUCH_TIP = "担保晋级";
	private static final String PROMOTE_TYPE_SECOND_TIP = "二次晋级";
	private static final String PROMOTE_STATE_DESC_APPLY = "待审核";
	private static final String PROMOTE_STATE_DESC_PASSED = "已通过";
	private static final String PROMOTE_STATE_DESC_REJECT = "已驳回";
	private static final String PROMOTE_STATE_DESC_ERROR = "状态错误";

 

 酷酷酷

标准sql,使用子查询:

1   以子查询的结果为临时表,进行select,必须指明临时表的别名

2 不管是select from 子select,还是  xx in 子select,子select都必须用()进行包裹

delete  from hyip_merchant_company_certified where id in 
( 
SELECT  a.id from 
( select id from hyip_merchant_company_certified where user_id 
in (select user_Id from hyip_merchant_company_certified  where state=1) and state!=1 ) a 
) 

 

 酷酷酷 

JSON数据的格式

 

简单对象
{
    "firstName": "Brett",
    "lastName": "McLaughlin",
    "email": "aaaa"
}

json数组
{
    "people": [
        {
            "firstName": "Brett",
            "lastName": "McLaughlin",
            "email": "aaaa"
        },
        {
            "firstName": "Jason",
            "lastName": "Hunter",
            "email": "bbbb"
        },
        {
            "firstName": "Elliotte",
            "lastName": "Harold",
            "email": "cccc"
        }
    ]
}

key可以不使用双引号
[
    {
        guarantyType: 0,
        amount: 2000000,
        minSellNum: 0,
        minTotalAmount: 0,
        minTotalCount: 0
    }
]

 

 

 酷酷酷 

Spring MVC,前后台之间传输json数组

 

JAVA 
public AjaxResult setEnabledCompanyCertifyQulification(String qualificationArray) {

URL
 http://XXX/**/setQulification.html?qualificationArray=[{guarantyType:0,amount:2000000,minSellNum:0,minTotalAmount:0,minTotalCount:0}]
 

发散:

用String,多个String参数可以在前后台之间传输任意多的复杂类型或者简单参数

 

 酷酷酷 

URL传参数的时候,参数的值不要使用单引号引起来,直接将值跟在=后面,tomcat+spingMVC会将单引号也作为值的一部分

 

错误的方式
http://XXX/**/listCertifyProcess.html?userName='lifengye15'

正确的方式
http://XXX/**/listCertifyProcess.html?userName=lifengye15

参数的值从=后面的第一字符开始一直到遇到&结束或者到URL结尾字符结束
http://XXXify/listCertifyProcess.html?userName=lifengye15=123,sds=sd&
 

 

 酷酷酷 

URL具备很强的描述能力,可以描述协议,host,path,参数可灵活定制,dubbo框架使用com.alibaba.dubbo.common.URL 在不同层之间传递配置和数据,采用了URL总线的设计.

 

public final class URL implements Serializable {
 private static final long serialVersionUID = -1985165475234910535L;
  
  private final String protocol;
	private final String username;
	private final String password;
	private final String host;
	private final int port;
	private final String path;
  private final Map<String, String> parameters;
 ............
    
        public static URL valueOf(String url) {
        if (url == null || (url = url.trim()).length() == 0) {
            throw new IllegalArgumentException("url == null");
        }
        String protocol = null;
        String username = null;
        String password = null;
        String host = null;
        int port = 0;
        String path = null;
        Map<String, String> parameters = null;
        int i = url.indexOf("?"); // seperator between body and parameters 
        if (i >= 0) {
            String[] parts = url.substring(i + 1).split("\\&");
            parameters = new HashMap<String, String>();
            for (String part : parts) {
                part = part.trim();
                if (part.length() > 0) {
                    int j = part.indexOf('=');
                    if (j >= 0) {
                        parameters.put(part.substring(0, j), part.substring(j + 1));
                    } else {
                        parameters.put(part, part);
                    }
                }
            }
            url = url.substring(0, i);
        }
        i = url.indexOf("://");
        if (i >= 0) {
            if(i == 0) throw new IllegalStateException("url missing protocol: \"" + url + "\"");
            protocol = url.substring(0, i);
            url = url.substring(i + 3);
        }
        else {
            // case: file:/path/to/file.txt
            i = url.indexOf(":/");
            if(i>=0) {
                if(i == 0) throw new IllegalStateException("url missing protocol: \"" + url + "\"");
                protocol = url.substring(0, i);
                url = url.substring(i + 1);
            }
        }
        
        i = url.indexOf("/");
        if (i >= 0) {
            path = url.substring(i + 1);
            url = url.substring(0, i);
        }
        i = url.indexOf("@");
        if (i >= 0) {
            username = url.substring(0, i);
            int j = username.indexOf(":");
            if (j >= 0) {
                password = username.substring(j + 1);
                username = username.substring(0, j);
            }
            url = url.substring(i + 1);
        }
        i = url.indexOf(":");
        if (i >= 0 && i < url.length() - 1) {
            port = Integer.parseInt(url.substring(i + 1));
            url = url.substring(0, i);
        }
        if(url.length() > 0) host = url;
        return new URL(protocol, username, password, host, port, path, parameters);
    }
    
    ............
    }

 

 

  酷酷酷 

Q mybatis+mysql 如何处理查询时,参数可能为空的场景?

A  注意非String,不要使用 and xx != ''

SQL
	<select id="listCertifyProcess" resultType="com.enterprise.domain.MerchantCertifyProcess">
		select * from hyip_merchant_certify_process where merchant_type='0'
			<if test="userName !=null and userName != ''">
				and username = #{userName}
			</if>
			<if test="state !=null and state != ''">
				and state = #{state}
			</if>
			<if test="startTime != null and startTime != ''">
				<![CDATA[ and create_time >=  #{startTime}  ]]>
			</if>
			<if test="endTime != null and endTime != ''">
				<![CDATA[ and create_time <= #{endTime} ]]>
			</if>
			<if test="subProcessTypes != null">
				and process_sub_type in 
				<foreach item="item" index="index" collection="subProcessTypes" open="(" separator="," close=")">
	    			#{item}
				</foreach>
			</if>
			order by create_time desc 
			limit #{pageSize} offset #{offset};
	</select>

REPOSITORY
	int countCertifyProcess(Map<String, Object> condtion);

JAVA 传值
	Map<String, Object> condtion = new HashMap<String, Object>();
		condtion.put("userName", userName);
		if (state != null) {
			condtion.put("state", state + "");
		}
		if (!StringUtils.isBlank(startTime)) {
			condtion.put("startTime", DateUtil.startOneDay(startTime));
		}
		if (!StringUtils.isBlank(endTime)) {
			condtion.put("endTime", DateUtil.endOneDay(endTime));
		}
		condtion.put("subProcessTypes", subProcessTypes);
		return merchantCertifyProcessRepository.countCertifyProces(condtion);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     

 

 

 

你可能感兴趣的:(2015-10-12 编程小经验)