宽为限 紧用功 功夫到 滞塞通
开篇
开发时是不是偶尔会遇到下标越界的问题,哈哈~ 索引有些从0开始,有些从1开始,不认真记一下还真容易混淆。+_+
sql
Hibernate占位符?和:及JPA,hibernate占jpa
小结一下hibernate占位符.
1.最常见的?占位符.
String hql = "select a from Apple a where a.color=? a.weight>?";
Query query = session.createQuery(hql);
query.setParameter(0, "red");
query.setParameter(1, "10");
下标从0开始,最常见的.这个让人头疼的是数?个数…
2.以一个变量名的形式占位.
String hql = "select a from Apple a where a.color=:pcolor a.weight>:pweight";
Query query = session.createQuery(hql);
query.setParameter("pcolor", "red");
query.setParameter("pweight", "10");
这个就不存在数?个数的问题了.应该是比较方便的一种方法了
3.JPA方式,这种方式是1的改良版本..
String hql = "select a from Apple a where a.color=?2 a.weight>?5";
Query query = session.createQuery(hql);
query.setParameter("2", "red");
query.setParameter("5", "10");
方法1中的?的索引可以自己随意任命了..
substring
以下是Java官方API对substring 的描述:
public String substring(int beginIndex)
Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
返回一个字符串,该字符串是此字符串的子字符串。
子字符串以指定索引处的字符开始,并延伸到该字符串的末尾。Examples:
“unhappy”.substring(2) returns “happy”
“Harbison”.substring(3) returns “bison”
“emptiness”.substring(9) returns “” (an empty string)Parameters:
beginIndex - the beginning index, inclusive. 开始指数,包含。Returns:
the specified substring. 指定的子字符串。Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.IndexOutOfBoundsException - 如果beginIndex为负或大于此String对象的长度。
public String substring(int beginIndex, int endIndex)
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
返回一个字符串,该字符串是此字符串的子字符串。子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1(下标从0开始)处的字符。因此,子字符串的长度为endIndex-beginIndex。
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
Parameters:
beginIndex - the beginning index, inclusive. 开始指数,包含。
endIndex - the ending index, exclusive. 结束指数,不包含。Returns:
the specified substring. 指定的子字符串。Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.IndexOutOfBoundsException - 如果beginIndex为负数,或endIndex大于此String对象的长度,或beginIndex大于endIndex。