Java教程 第55页

Lucene 索引文件

Lucene教程 – Lucene索引文件 索引是识别文档并为搜索准备文档的过程。 下表列出了索引过程中常用的类。 类 描述 IndexWriter 在索引过程中创建/更新索引。 Directory 表示索引的存储位置。 Analyzer 分析文档并从文本中获取标记/单词。 Document 带有字段的虚拟文档。分析仪可以处理文档。 Field 索引过程的最低单位。它表示键值对,其中键用于标识索引值。 例子 以下代码显示了如何使用Lucene索引文本文件。 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT...

Lucene HelloWorld

Lucene教程 – Lucene HelloWorld 我们可以使用Lucene为您的应用程序添加全文搜索功能。 索引 我们将从一些字符串创建一个内存索引。 StandardAnalyzer analyzer = new StandardAnalyzer(Version.LATEST); Directory index = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer); IndexWriter w = new IndexWriter(index, config); addDoc(w, "Lucene in Action", "1"); addDoc(w, "Lucene for Dummies", "2"); addDoc(w, "Java", "3"); addDoc(w, "Oracle", "4"); w.close(); private static void addDoc(IndexWriter w, String title, String isbn) throws IOException { Document doc = new Document(); doc.add(new TextField("title", title, Field.Store.YES)); doc.add(new StringField("isbn", isbn, Field.Store.YES)); w.addDocument(doc); } TextField对内容进行标记化,而StringField不对其内容进行标记化。 查询 以下代码显示了如何从Java字符串构建查询。 String querystr = "lucene"; Query q = new QueryParser(Version.LATEST, "title", analyzer).parse(querystr); 搜索 当进行搜索时,我们首先打开索引,这是一个内存索引。TopScoreDocCollector用于收集前10个评分点击。 int hitsPerPage = 10; IndexReader reader = IndexReader.open(index); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true); searcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; 显示 以下是显示结果的逻辑。 System.out.println("Found " + hits.length +...

JPA 教程

//www.w3cschool.cn/java/jpa-entitymanager.html

JPA 查询Distinct多对一示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA 查询Distinct多对一示例

JPA教程 – JPA查询Distinct多对一示例 DISTINCT运算符在功能上等同于同名的SQL运算符。 收集结果集后,将删除重复值,以便只返回唯一结果。 以下JPQL在多对一映射中使用DISTINCT运算符。 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department { @Id private int id; private String name; @OneToMany(mappedBy="department") private Collection<Employee> employees; public Department() { employees = new ArrayList<Employee>(); } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setEmployees(Collection<Employee> employees) { this.employees = employees; } public int getId() { return id; } public String getName() { return name; } public Collection<Employee> getEmployees() { return employees; } public String toString() { return "Department no: " + getId() + ", name: " + getName(); } } 以下代码来自Project.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import...

JPA 查询Distinct Join条件示例

JPA教程 – JPA查询Distinct Join条件示例 以下查询使用distinct关键字以获取连接条件的唯一值。 List l = em.createQuery( "SELECT DISTINCT e.department FROM Project p JOIN p.employees e WHERE p.name = "proj"") .getResultList(); 例子 以下代码来自Phone.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Phone { @Id private long id; private String number; private String type; @ManyToOne Professor employee; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String phoneNo) { this.number = phoneNo; } public String getType() { return type; } public void setType(String phoneType) { this.type = phoneType; } public Professor getProfessor() { return employee; } public void setProfessor(Professor employee) { this.employee = employee; } public String toString() { return...

JPA 查询IN一对多示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA 查询IN一对多示例

JPA教程 – JPA查询IN一对多示例 以下JPQL显示如何在一对多映射中使用IN运算符。 executeAndPrintQuery("SELECT DISTINCT p FROM Employee e, IN(e.directs) p"); 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department { @Id private int id; private String name; @OneToMany(mappedBy="department") private Collection<Employee> employees; public Department() { employees = new ArrayList<Employee>(); } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setEmployees(Collection<Employee> employees) { this.employees = employees; } public int getId() { return id; } public String getName() { return name; } public Collection<Employee> getEmployees() { return employees; } public String toString() { return "Department no: " + getId() + ", name: " + getName(); } } 以下代码来自Project.java。 package cn.w3cschool.common; import...

JPA 查询ANY示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA 查询ANY示例

JPA教程 – JPA查询ANY示例 以下代码显示了如何在JPQL中使用ANY运算符。 List l = em.createQuery( "SELECT e FROM Professor e WHERE e.department = ANY (SELECT DISTINCT d FROM Department d)") .getResultList(); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { prepareData(); List l = em.createQuery( "SELECT e FROM Professor e WHERE e.department = ANY (SELECT DISTINCT d FROM Department d)") .getResultList(); for (Object p : l) { printResult(p); } } private void prepareData() { Address address = new Address(); address.setState("BC"); Professor p = new Professor(); p.setId(0); p.setName("TOM"); p.setSalary(1111L); p.setAddress(address); Department d = new Department(); d.setId(1); d.setName("Design"); p.setDepartment(d); d.getProfessors().add(p); Phone phone = new Phone(); phone.setId(1); phone.setNumber("111-111-1111"); phone.setProfessor(p); em.persist(p); em.persist(phone); em.persist(address); em.persist(d); } private static void printResult(Object...

JPA 查询All示例

JPA教程 – JPA查询All示例 我们可以在JPQL中使用带有子查询的ALL运算符。 List l = em.createQuery( "SELECT e FROM Professor e WHERE e.salary < " + "ALL (SELECT d.salary FROM e.directs d)") .getResultList(); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { prepareData(); List l = em.createQuery( "SELECT e FROM Professor e WHERE e.salary < " + "ALL (SELECT d.salary FROM e.directs d)") .getResultList(); for (Object p : l) { printResult(p); } } private void prepareData() { Professor p = new Professor(); p.setId(0); p.setName("TOM"); p.setSalary(1111L); Department d = new Department(); d.setId(1); d.setName("Design"); p.setDepartment(d); d.getProfessors().add(p); em.persist(p); em.persist(d); } private static void printResult(Object result) { if (result == null) { System.out.print("NULL"); } else if (result instanceof Object[]) {...

JPA 查询GroupBy Having示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA 查询GroupBy Having示例

JPA教程 – JPA查询GroupBy Having例子 以下代码显示了如何在JPQL中使用GROUP BY和HAVING子句。 List l = em.createQuery( "SELECT d.name, AVG(e.salary) FROM Department d JOIN d.employees e " + "WHERE e.directs IS EMPTY " + "GROUP BY d.name " + "HAVING AVG(e.salary) > 50") 例子 以下代码来自Address.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Address { @Id private int id; private String street; private String city; private String state; private String zip; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String address) { this.street = address; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; }...

JPA 查询Not Empty示例

JPA教程 – JPA查询Not Empty示例 JPA SQL可以检查属性是否为空。 List l = em.createQuery( "SELECT p FROM Project p WHERE p.employees IS NOT EMPTY") .getResultList(); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { prepareData(); List l = em.createQuery( "SELECT p FROM Project p WHERE p.employees IS NOT EMPTY") .getResultList(); for (Object p : l) { printResult(p); } } private void prepareData() { Address address = new Address(); address.setState("BC"); Professor p = new Professor(); p.setId(0); p.setName("TOM"); p.setSalary(1111L); p.setAddress(address); Department d = new Department(); d.setId(1); d.setName("Design"); p.setDepartment(d); d.getProfessors().add(p); Phone phone = new Phone(); phone.setId(1); phone.setNumber("111-111-1111"); phone.setProfessor(p); em.persist(p); em.persist(phone); em.persist(address); em.persist(d); } private static void printResult(Object result) { if (result == null) { System.out.print("NULL");...