終極JPA查詢和技巧列表–第2部分

這一部分是該系列文章的第一部分 。

JPA:NamedQuery,使用日期查詢,有關getSingleResult方法的警告

為了避免重復查詢代碼,提高性能并簡化維護查詢,我們可以使用NamedQueries。 NamedQuery使用JPQL作為語法,并在實體類中聲明。 類代碼更新后,更容易編輯查詢。

如果要使用日期作為參數進行查詢,則只能發送日期對象,也可以傳遞描述日期類型的枚舉(推薦)。

在下面,您將看到如何創建和使用@NamedQuery以及如何使用日期查詢:

package com.model;import java.util.Date;import javax.persistence.*;@Entity
@NamedQuery(name='Dog.FindByDateOfBirth', query='select d from Dog d where d.dateOfBirth = :dateOfBirth')
public class Dog {public static final String FIND_BY_DATE_OF_BIRTH = 'Dog.FindByDateOfBirth';@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;// get and set
}
package com.model;import java.util.*;import javax.persistence.*;@Entity
@NamedQueries({@NamedQuery(name='Person.findByName', query='select p from Person p where p.name = :name'),@NamedQuery(name='Person.findByAge', query='select p from Person p where p.age = :age')})
})
public class Person {public static final String FIND_BY_NAME = 'Person.findByName';public static final String FIND_BY_AGE = 'Person.findByAge';@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;// get and set}
package com.main;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;import com.model.Dog;
import com.model.Person;public class Page07 {public static void main(String[] args) {CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();int age = 70;List<Person> personByAge = getPersonByAge(em, 70);System.out.println('Found ' + personByAge.size() + ' person(s) with the age of: ' + age);SimpleDateFormat formatter = new SimpleDateFormat('dd/MM/yyyy');Date dateOfBirth = null;try {dateOfBirth = formatter.parse('10/1/1995');} catch (ParseException e) {e.printStackTrace();}List<Dog> dogsByDayOfBirth = getDogsByDayOfBirth(em, dateOfBirth);System.out.println('Found ' + dogsByDayOfBirth.size() + ' dog with birth date of ' + formatter.format(dateOfBirth));/** This queries will raise Runtime Exceptions** em.createQuery('select p from Person p').getSingleResult(); // NonUniqueResultException** em.createQuery('select p from Person p where p.name = 'JJJJ'').getSingleResult(); //NoResultException*/CodeGenerator.closeConnection();}@SuppressWarnings('unchecked')private static List<Dog> getDogsByDayOfBirth(EntityManager em, Date dateOfBirth) {Query query = em.createNamedQuery(Dog.FIND_BY_DATE_OF_BIRTH);query.setParameter('dateOfBirth', dateOfBirth, TemporalType.DATE);return query.getResultList();}@SuppressWarnings('unchecked')private static List<Person> getPersonByAge(EntityManager em, int age) {Query query = em.createNamedQuery(Person.FIND_BY_AGE);query.setParameter('age', age);return query.getResultList();}
}

關于上面的代碼:

  • 如果只有一個查詢,則可以使用@NamedQuery注釋; 如果您有多個查詢,則可以使用@NamedQueries批注。
  • 使用日期對象進行查詢時,還可以使用TemporalType枚舉來詳細說明日期的類型。 對于日期查詢,可以使用“ java.util.Date ”或“ java.util.GregorianCalendar ”。

getSingleResult()

使用此方法時要小心。 它有一種特殊的方式來處理很容易發生的兩種行為,并且兩種行為都會引發異常:

  • 從查詢結果中查找多個對象: NonUniqueResultException
  • 找不到結果: NoResultException

您始終需要使用try / catch以避免在生產環境中引發這些異常。

如果要實時查看此異常,則在上面的代碼中可以找到兩個帶注釋的查詢。 它將引發以下異常:

Exception in thread 'main' <span style='text-decoration: underline;'>javax.persistence.NonUniqueResultException</span>: result returns more than one elements
at org.hibernate.ejb.QueryImpl.getSingleResult(<span style='text-decoration: underline;'>QueryImpl.java:287</span>)
Exception in thread 'main' <span style='text-decoration: underline;'>javax.persistence.NoResultException</span>: No entity found for query
at org.hibernate.ejb.QueryImpl.getSingleResult(<span style='text-decoration: underline;'>QueryImpl.java:280</span>)

JPA:NativeQuery,名為NativeQuery

JPA使用的JPQL沒有任何特定于數據庫的功能。 當JPQL僅提供數據庫之間的公用功能時,如何進行查詢以調用特定的數據庫功能?

從人員p中選擇p,其中p.name?*:name ”該查詢語法是對Postgres數據庫的有效查詢; 如果您嘗試使用NamedQuery(使用JPQL)運行此查詢,則會看到以下異常:

<strong><em>ERROR SessionFactoryImpl:422 - Error in named query: Person.FindByName</em></strong>
<strong><em>org.hibernate.QueryException: unexpected char: '~' [select p from com.model.Person p where p.name ~* :name]</em></strong>
<strong><em>at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:229)</em></strong>
<strong><em>at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)</em></strong>
<strong><em>at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)</em></strong>
<strong><em>at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)</em></strong>
<strong><em>at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)</em></strong>
<strong><em>at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:547)</em></strong>
<strong><em>at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:411)</em></strong>
<strong><em>at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)</em></strong>
<strong><em>at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)</em></strong>
<strong><em>at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)</em></strong>
<strong><em>at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)</em></strong>
<strong><em>at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)</em></strong>
<strong><em>at com.main.CodeGenerator.startConnection(CodeGenerator.java:27)</em></strong>
<strong><em>at com.main.Page05.main(Page05.java:12)</em></strong>
<strong><em>Exception in thread “main” javax.persistence.PersistenceException: [PersistenceUnit: JpaQuery] Unable to build EntityManagerFactory</em></strong>
<strong><em>at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911)</em></strong>
<strong><em>at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)</em></strong>
<strong><em>at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)</em></strong>
<strong><em>at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)</em></strong>
<strong><em>at com.main.CodeGenerator.startConnection(CodeGenerator.java:27)</em></strong>
<strong><em>at com.main.Page05.main(Page05.java:12)</em></strong>
<strong><em>Caused by: org.hibernate.HibernateException: Errors in named queries: Person.FindByName</em></strong>
<strong><em>at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:424)</em></strong>
<strong><em>at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)</em></strong><strong><em> </em></strong>
<strong><em>at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)</em></strong>
<strong><em> ... 5 more</em></strong>

解決這種情況的方法是使用不使用JPQL作為其語法的查詢。 使用NativeQuery,您將能夠使用數據庫語法進行查詢。

您可以在下面的代碼中看到如何使用NativeQueries:

package com.main;import javax.persistence.EntityManager;
import javax.persistence.Query;import com.model.Dog;public class Page08 {public static void main(String[] args) {CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();String nameOfFirstPerson = getFirstPersonName(em);System.out.println(nameOfFirstPerson);Dog dog = getTopDogDescending(em);System.out.println(dog.getName());CodeGenerator.closeConnection();}/*** Returns the name of the first person using a native sql*/private static String getFirstPersonName(EntityManager em) {Query query = em.createNativeQuery('select top 1 name from person');return (String) query.getSingleResult();}/*** Return an object using a native sql*/private static Dog getTopDogDescending(EntityManager em) {Query query = em.createNativeQuery('select top 1 id, name, weight from dog order by id desc', Dog.class);return (Dog) query.getSingleResult();}
}

關于上面的代碼:

  • 請注意,我們使用本機查詢代替JPQL。 作為本地查詢的結果,我們可以有一個實體。 方法“ getTopDogDescending ”在本機查詢調用之后返回Dog對象。

您還可以將本地查詢創建為@NamedNativeQuery。 NamedNativeQuery和NativeQuery之間的區別在于NamedNativeQuery是在其實體類上定義的,并且您只能使用該名稱類。

使用@NamedNativeQuery的優點是:

  • 易于維護的代碼:每個查詢都在該類上,如果一個類更新屬性,則更新查詢將變得更加容易。
  • 幫助提高性能:聲明查詢后,JPA將對其進行映射并將其語法保留在內存中。 JPA不需要在項目每次使用時都“解析”您的查詢。
  • 提高代碼重用性:聲明@NamedNativeQuery后,必須為“ name”參數提供一個值,并且該名稱對于Persistence Unit范圍必須是唯一的(您在“ persistence.xml”中設置此值,并由EntityManager)。

您將在下面看到如何聲明@NamedNativeQuery:

package com.model;import java.util.*;import javax.persistence.*;@Entity
@NamedQueries({@NamedQuery(name='Person.findByName', query='select p from Person p where p.name = :name'),@NamedQuery(name='Person.findByAge', query='select p from Person p where p.age = :age')
})@NamedNativeQuery(name='Person.findByNameNative', query='select id, name, age from person where name = :name')
public class Person {public static final String FIND_BY_NAME = 'Person.findByName';public static final String FIND_BY_AGE = 'Person.findByAge';@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;// get and set
}

您將可以使用@NamedNativeQuery,就像@NativeQuery一樣: em.createNamedQuery(“ Person.findByNameNative”); ”。

這里有一些壞消息。 不幸的是,Hibernate尚未實現@NamedNativeQuery。 如果您嘗試使用此批注運行代碼,則會看到以下異常:

<em>Caused by: <span style='text-decoration: underline;'>org.hibernate.cfg.NotYetImplementedException</span>: Pure native scalar queries are not yet supported</em>
<em>at org.hibernate.cfg.annotations.QueryBinder.bindNativeQuery(<span style='text-decoration: underline;'>QueryBinder.java:140</span>)</em>
<em>at org.hibernate.cfg.AnnotationBinder.bindQueries(<span style='text-decoration: underline;'>AnnotationBinder.java:339</span>)</em>
<em>at org.hibernate.cfg.AnnotationBinder.bindClass(<span style='text-decoration: underline;'>AnnotationBinder.java:548</span>)</em>
<em>at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(<span style='text-decoration: underline;'>Configuration.java:3977</span>)</em>
<em>at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(<span style='text-decoration: underline;'>Configuration.java:3931</span>)</em>
<em>at org.hibernate.cfg.Configuration.secondPassCompile(<span style='text-decoration: underline;'>Configuration.java:1368</span>)</em>
<em>at org.hibernate.cfg.Configuration.buildMappings(<span style='text-decoration: underline;'>Configuration.java:1345</span>)</em>
<em>at org.hibernate.ejb.Ejb3Configuration.buildMappings(<span style='text-decoration: underline;'>Ejb3Configuration.java:1477</span>)</em>
<em>at org.hibernate.ejb.EventListenerConfigurator.configure(<span style='text-decoration: underline;'>EventListenerConfigurator.java:193</span>)</em>
<em>at org.hibernate.ejb.Ejb3Configuration.configure(<span style='text-decoration: underline;'>Ejb3Configuration.java:1096</span>)</em>
<em>at org.hibernate.ejb.Ejb3Configuration.configure(<span style='text-decoration: underline;'>Ejb3Configuration.java:278</span>)</em>
<em>at org.hibernate.ejb.Ejb3Configuration.configure(<span style='text-decoration: underline;'>Ejb3Configuration.java:362</span>)</em>

JPA:復雜的本地查詢

您將能夠創建映射到NativeQuery的復合體。 此映射將返回多個類或值。

您可以在下面看到我們的類如何映射此復雜結果以及如何使用它執行查詢:

package com.model;import java.util.*;import javax.persistence.*;@Entity
@NamedQueries({@NamedQuery(name='Person.findByName', query='select p from Person p where p.name = :name'),@NamedQuery(name='Person.findByAge', query='select p from Person p where p.age = :age')})
})
@SqlResultSetMappings({@SqlResultSetMapping(name='personAndAdress',entities={@EntityResult(entityClass=Person.class),@EntityResult(entityClass=Address.class,fields={@FieldResult(name='id', column='ADDRESS_ID')})}),@SqlResultSetMapping(name='personWithDogAmount',entities={@EntityResult(entityClass=Person.class)},columns={@ColumnResult(name='dogAmount')})
})
public class Person {public static final String FIND_BY_NAME = 'Person.findByName';public static final String FIND_BY_AGE = 'Person.findByAge';public static final String MAPPING_PERSON_AND_ADDRESS = 'personAndAdress';public static final String MAPPING_DOG_AMOUNT = 'personWithDogAmount';@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;// get and set
}
package com.main;import java.math.BigInteger;import javax.persistence.EntityManager;
import javax.persistence.Query;import com.model.Address;
import com.model.Person;public class Page09 {public static void main(String[] args) {CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();Query query = em.createNativeQuery('select id, name, age, a.id as ADDRESS_ID, houseNumber, streetName ' +'from person p join address a on a.id = p.address_id where p.id = 1',Person.MAPPING_PERSON_AND_ADDRESS);Object[] result = (Object[]) query.getSingleResult();Person personWithAdress = (Person) result[0];Address address = (Address) result[1];System.out.println(personWithAdress.getName() + ' lives at ' + address.getStreetName());query = em.createNativeQuery('select p.id, p.name, count(0) as dogAmount ' +'from person p join dog d on p.id = d.person_id where name = 'Mark' ' +'group by p.id, p.name',Person.MAPPING_DOG_AMOUNT);result = (Object[]) query.getSingleResult();Person person = (Person) result[0];BigInteger total = (BigInteger) result[1];System.out.println(person.getName() + ' has ' + total + ' dogs');CodeGenerator.closeConnection();}
}

關于上面的代碼:

  • 使用“ @SqlResultSetMapping ”,您將通知JPA我們想要哪個實體作為結果。 注意,在映射“ personAndAdress ”中,我們編寫了將返回的類。 我們還使用了一個名為“ @FieldResult ”的屬性。 此屬性將映射具有相同名稱的查詢字段,在我們的查詢中,我們獲得了人員ID和地址ID。 這就是為什么我們使用“ @FieldResult ”來通知JPA將列ADDRESS_ID映射到Address類的ID屬性的原因。
  • 在“ dogAmount ”映射中,我們設置“ @ColumnResult”屬性,該屬性通知JPA我們將在查詢結果中包含一個“ 額外的列 ”,而該“ 額外的列 ”不屬于任何類。

JPA:使用EJB優化查詢

每當您在事務范圍內執行查詢時,持久性上下文將使結果保持“ 附加 ”狀態。 持久性上下文將“ 監視 ”該對象,以防萬一該“ 附加 ”對象中的任何對象收到任何更新。 所有“ 附加的 ”對象更新都將保留在數據庫中。

@PersistenceContext(unitName = 'myPU')
private EntityManager em;@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void editPersonName(Integer personId, String personNewName){Person person = em.find(Person.class, personId);person.setName(personNewName);
}

您可以在上面的代碼中看到我們不需要調用“ em.merge() ”來更新數據庫中的人名。

當我們從數據庫中帶來一個通常顯示在數據表或報表上的集合時,所有這些對象都將附加到持久性上下文中。 制作此對象的過程將觸發多個過程,數據驗證和同步。 當對象數越高時,分配給查詢結果的內存就越高,而保持所有這些對象“ 附加 ”的持久性上下文的工作就越高。

將所有這些對象的最終目的地發送到視圖時,將所有這些對象“附加”起來有什么意義? 當對象離開EJB項目并轉到視圖項目時,它們將被視為“分離的”。 在這種情況下,我們無需進行不必要的工作即可從數據庫中獲取所有數據,進行make(而不是“附加”)并將其發送到視圖以使其“分離”。

有一種簡單的方法可以使這些對象來自已經分離的數據庫。 這種方法的優勢在于,持久性上下文永遠不會浪費時間,容器處理器會嘗試同步查詢結果。

您可以在下面的代碼中看到此解決方案的工作方式。

package com.main;import java.util.List;import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;import com.model.Person;@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class PersonDAO {@PersistenceContext(unitName = 'myPU')private EntityManager em;@TransactionAttribute(TransactionAttributeType.REQUIRED)public void editPersonName(Integer personId, String personNewName){Person person = em.find(Person.class, personId);person.setName(personNewName);}@SuppressWarnings('unchecked')public List<Person> listAll(){Query query = em.createQuery('select p from Person p');return query.getResultList();}@SuppressWarnings('unchecked')public List<Person> listAllWithoutDogs(){Query query = em.createQuery('select p from Person p where p.dogs is empty');return query.getResultList();}
}

在上面的代碼中,我們獲得了一個DAO類,即EJB。 默認情況下,我們的EJB缺少事務(“ @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) ”),使用這種事務,持久性上下文將不會“ 附加 ”查詢結果。 查詢返回的對象將被視為“ 分離 ”。

請注意,方法“ editPersonName ”的注釋仍然相同:“ @TransactionAttribute(TransactionAttributeType.REQUIRED) ”。 這種事務向EJB指示如果尚未啟動任何新事務,則應啟動一個新事務。 您可以為該類設置事務屬性,但是方法可以覆蓋此屬性,就像我們在“ editPersonName ”中所做的一樣 。 方法事務定義將優先于類事務定義。

JPA:分頁

如果要進行JPA分頁,請像下面的方法一樣:

package com.main;import java.util.List;import javax.persistence.EntityManager;
import javax.persistence.Query;import com.model.Dog;public class Page11 {@SuppressWarnings('unchecked')public static void main(String[] args) {CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();Query query = em.createQuery('select d from Dog d');List<Dog> dogs = query.getResultList();System.out.println('Total of dogs found: ' + dogs.size());query.setMaxResults(5);query.setFirstResult(0);List<Dog> fiveFirstDogs = query.getResultList();System.out.print('Total of dogs found: ' + fiveFirstDogs.size() + ' ');for (Dog dog : fiveFirstDogs) {System.out.print(dog.getName() + ' ');}System.out.println();query.setMaxResults(5);query.setFirstResult(5);List<Dog> fiveSecondDogs = query.getResultList();System.out.print('Total of dogs found: ' + fiveSecondDogs.size() + ' ');for (Dog dog : fiveSecondDogs) {System.out.print(dog.getName() + ' ');}CodeGenerator.closeConnection();}
}

關于上面的代碼:

  • 方法“ setMaxResults ”將設置查詢將返回的結果量。
  • 方法“ setFirstResult ”將設置將要帶來的第一行。

在第一個查詢中,我們搜索了數據庫中的所有數據。

在第二個查詢中,我們從位置0開始獲得了五個結果。

在上一個查詢中,我們再次獲得了五個結果,但是從位置5開始。

請記住,第一個位置始終為零而不是一個。

JPA:數據庫提示

數據庫供應商向我們提供了名為“提示”的特定功能。 這些提示非常有用,因為它們可以優化查詢并幫助我們完成其他任務。 每個數據庫都有自己的提示,并且這些值不可移植。

在下面,您可以看到一些提示:

  • SQLServer:OPTION(OPTIMIZE FOR(@name ='Mark',@age UNKNOWN));
  • Oracle:選擇/ * + first_rows(100)* /名稱
  • MySQL:從人忽略索引中選擇*(col3_index)

每個數據庫供應商都將規則設置為其提示,諸如語法和執行命令之類的規則。

有兩種定義提示的方法:

package com.main;import java.util.List;import javax.persistence.EntityManager;
import javax.persistence.Query;import com.model.Dog;public class Page12 {@SuppressWarnings('unchecked')public static void main(String[] args) {CodeGenerator.startConnection();CodeGenerator.generateData();EntityManager em = CodeGenerator.getEntityManager();Query query = em.createQuery('select d from Dog d');query.setHint('org.hibernate.timeout', 1000);List<Dog> dogs = query.getResultList();System.out.println('Found ' + dogs.size() + ' dogs');CodeGenerator.closeConnection();}
}
package com.model;import java.util.*;import javax.persistence.*;@Entity
@NamedQueries({@NamedQuery(name='Person.findByName', query='select p from Person p where p.name = :name'),@NamedQuery(name='Person.findByAge', query='select p from Person p where p.age = :age', hints={@QueryHint(name='org.hibernate.timeout', value='1000')})
})
@SqlResultSetMappings({@SqlResultSetMapping(name='personAndAdress',entities={@EntityResult(entityClass=Person.class),@EntityResult(entityClass=Address.class,fields={@FieldResult(name='id', column='ADDRESS_ID')})}),@SqlResultSetMapping(name='personWithDogAmount',entities={@EntityResult(entityClass=Person.class)},columns={@ColumnResult(name='dogAmount')})
})
public class Person {public static final String FIND_BY_NAME = 'Person.findByName';public static final String FIND_BY_AGE = 'Person.findByAge';public static final String MAPPING_PERSON_AND_ADDRESS = 'personAndAdress';public static final String MAPPING_DOG_AMOUNT = 'personWithDogAmount';@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;// get and set
}

您可以在@NamedQuery和直接在查詢中設置提示。

您必須始終記住提示的缺點 。 在NamedQuery上設置提示后,您的代碼將無法移植到其他數據庫供應商。 一種解決方案是僅使用查詢而不是NamedQuery。 在將提示添加到查詢之前,您可以檢查當前數據庫是否支持該提示。 您可以使用屬性文件來定義應用程序的當前數據庫。

繼續本系列的第三部分

參考: uaiHebert博客上來自JCG合作伙伴 Hebert Coelho的JPA查詢和技巧 。


翻譯自: https://www.javacodegeeks.com/2012/07/ultimate-jpa-queries-and-tips-list-part_09.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371717.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371717.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371717.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

設置UITableView設置contentsize

由于UITableView是繼承自UIScrollView的&#xff0c;所以他是可以設置contentsize的。 但是&#xff0c;我在試驗的過程中&#xff0c;初始化UITableView實例后&#xff0c;直接設置它的contentsize是不起作用&#xff0c;在搜尋相關資料得知&#xff0c;UITableView會自動設置…

java 線程什么時候結束_java線程什么時候讓出cpu?

Thread.sleep();sleep就是正在執行的線程主動讓出cpu&#xff0c;cpu去執行其他線程&#xff0c;在sleep指定的時間過后&#xff0c;cpu才會回到這個線程上繼續往下執行&#xff0c;如果當前線程進入了同步鎖&#xff0c;sleep方法并不會釋放鎖&#xff0c;即使當前線程使用sle…

Hibernate配置方式

Hibernate配置方式 Hibernate給人的感受是靈活的&#xff0c;要達到同一個目的&#xff0c;我們可以使用幾種不同的辦法。就拿Hibernate配置來說&#xff0c;常用的有如下三種方式&#xff0c;任選其一。 在 hibernate.cfg.xml 中加入元素 <property>、<mapping>&a…

js中 javascript:void(0) 用法詳解

javascript:void(0)表示不做任何動作。如&#xff1a; 復制代碼代碼如下:<a href"javascript:void(0);" οnclick"alert(ok);"></a> 這里表示這個鏈接不做跳轉動作&#xff0c;執行onClick事件。 我想使用過ajax的都常見這樣的代碼&#xff1…

帶有ActiveMQ的JMS

帶有ActiveMQ的JMS JMS是Java Message Service的縮寫&#xff0c;它提供了一種以松散耦合&#xff0c;靈活的方式集成應用程序的機制。 JMS以存儲和轉發的方式跨應用程序異步傳遞數據。 應用程序通過充當中介的MOM&#xff08;面向消息的中間件&#xff09;進行通信&#xff0c…

矩陣分解 java_使用矩陣分解為推薦系統

矩陣分解假設“潛在因素”&#xff0c;例如對用戶的意大利食物的偏好和項目食物的意外性與矩陣中的評級有關 .因此&#xff0c;整個問題類型轉變為矩陣重構問題&#xff0c;存在許多不同的解決方案 . 一個簡單的&#xff0c;可能很慢的解決方案是(除了ALS和其他一些矩陣重建的可…

用戶故事排球教練助手

計劃&#xff1a;估計這個任務需要一周時間 需求分析&#xff1a;作為一名排球教練助手&#xff0c;我需要了解每場每位隊員的技術動作&#xff0c;每場比賽每位隊員的得分情況&#xff0c;以便教練更好的了解到每位隊員的發揮情況和特長。 設計文檔&#xff1a;用戶進入此界面…

TMS320DM642學習----第一篇(硬件連接)

DSP設備型號&#xff1a;SEED-DTK-VPM642&#xff08;目前實驗室用途&#xff1a;視頻處理&#xff0c;圖像處理方向&#xff0c;預計搭載目標跟蹤以及云臺防抖等算法&#xff09; 官網鏈接&#xff1a;http://www.seeddsp.com/index.php/Home/Product/detail/name/1/id/174.ht…

字符串內存內部

本文基于我對StackOverflow的回答 。 我正在嘗試解釋String類如何存儲文本&#xff0c;內部存儲和常量池如何工作。 這里要理解的要點是String Java對象與其內容– private value字段下的char[]之間的區別。 String基本上是char[]數組的包裝器&#xff0c;將其封裝并使其無法修…

關于inline-block 元素之間為何會產生間隔

關于inline-block 元素之間為何會產生間隔 現象&#xff1a; <body><input type"text"><input type"text"> </body> 在瀏覽器中的表現&#xff1a; 實時上不僅僅是 inline-block 會導致這種現象。 inline 也會導致。 那問題來了&a…

java 入參 是 枚舉_java 枚舉 參數傳遞

展開全部這樣做是不行的&#xff0c;原因是&#xff1a;Java中的對象實例化都是在堆中&#xff0c;如果是普通的類實例變量&#xff0c;比如在方法636f707962616964757a686964616f313333376166371中定義的普通類實例變量&#xff0c;傳到了方法2中&#xff0c;由于方法1和方法2…

loadView的使用總結

一、loadView 1. loadView什么時候被調用&#xff1f; 每次訪問UIViewController的view&#xff08;如 controller.view、self.view&#xff09;并且view為nil&#xff0c;loadView方法就會被調用 2. 有什么作用 loadView 方法是用來負責創建UIViewController的view 3. 默認實…

數據庫備份 java jar_Java實現數據庫備份并利用ant導入SQL腳本

?數據備份對于經常在運維部署方面的工作者來說&#xff0c;是一件相對簡單的事情&#xff0c;都可以通過某一個SQL工具進行備份&#xff0c;但是如果在項目運行當中&#xff0c;我們需要對數據進行實時&#xff0c;或者是每隔一星期&#xff0c;一個月&#xff0c;等等進行數據…

JSF簡單Ajax示例

今天&#xff0c;我們將看到一些使用JSF的Ajax簡單樣本。 如果要查看有關JSF / Web應用程序的其他文章&#xff0c;請單擊以下鏈接&#xff1a; 重定向后的JSF持久化對象和消息 &#xff0c; 使用JAAS和JSF進行用戶登錄驗證 &#xff0c; JSF&#xff1a;Converter and Bean Au…

常用的好用的window工具

1. FastStone Capture截圖錄屏軟件 百度軟件中心&#xff1a;http://rj.baidu.com/soft/detail/13504.html?ald 注冊企業版&#xff1a; 用戶名&#xff1a;c1ikm 注冊碼&#xff1a;AXMQX-RMMMJ-DBHHF-WIHTV 中文輸入亂碼解決方法&#xff1a; 2. Notepad文本編輯器&#xff…

表分區

http://www.cnblogs.com/leestar54/p/6225821.html轉載于:https://www.cnblogs.com/jouny/p/6262850.html

java飛鴿傳書_feige 飛鴿傳書源代碼java 實現不錯的聯系網絡編程的資料飛鴿傳書的GUI(java實現) - 下載 - 搜珍網...

我的飛鴿傳書/FileFilter.java我的飛鴿傳書/FileNameExtensionFilter.java我的飛鴿傳書/飛鴿傳書/classes/feige/About.class我的飛鴿傳書/飛鴿傳書/classes/feige/ConnectOthers$ReadMessageThread.class我的飛鴿傳書/飛鴿傳書/classes/feige/ConnectOthers.class我的飛鴿傳書…

JAXB和根元素

XmlRootElement是人們習慣于與JAXB&#xff08;JSR-222&#xff09;一起使用的注釋。 目的是將根元素與類唯一關聯。 由于JAXB類映射到復雜類型&#xff0c;因此一個類有可能對應于多個根元素。 在這種情況下&#xff0c;無法使用XmlRootElement &#xff0c;人們開始感到有些困…

python socket模塊實現udp通信_Python基于socket模塊實現UDP通信功能示例

Python基于socket模塊實現UDP通信功能示例本文實例講述了Python基于socket模塊實現UDP通信功能。分享給大家供大家參考&#xff0c;具體如下&#xff1a;一 代碼1、接收端import socket#使用IPV4協議&#xff0c;使用UDP協議傳輸數據ssocket.socket(socket.AF_INET, socket.SOC…