Java教程 第27页

Java 实例 – 读取文件内容-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 读取文件内容

Java 实例 – 读取文件内容 Java 实例 以下实例演示了使用 readLine() 方法来读取文件 test.log 内容,其中 test.log 文件内容为: w3cschool在线教程 www.w3cschool.cn java 代码如下: /* author by w3cschool.cn Main.java */ import java.io.*; public class Main { public static void main(String[] args) { try { BufferedReader in = new BufferedReader (new FileReader("test.log")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } System.out.println(str); } catch (IOException e) { } } } 以上代码运行输出结果为: 优聚教程 www.w3cschool.cn null Java 实例

JPA教程 - JPA表名示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA教程 - JPA表名示例

JPA教程 – JPA表名示例 默认情况下,JPA使用类的简单名称来命名相应的表。 @Entity @Table(name="EMP") public class Person { 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="EMP") public class Person { @Id @Column(name = "EMP_ID") private long id; @Basic private String name; private String surname; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } @Override public String toString() { return "Person [id=" + id + ",...

JPA 查询Between and示例

JPA教程 – JPA查询Between and示例 可以在条件表达式中使用BETWEEN运算符来确定表达式的结果是否落在值的包含范围内。 可以以这种方式评估数字,字符串和日期表达式。 BETWEEN运算符也可以使用NOT运算符取反。 以下代码显示了如何在JPQL中使用BETWEEN AND运算符。 "SELECT e FROM Professor e WHERE e.salary BETWEEN 40 AND 50") 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Professor { @Id private int id; private String name; private long salary; @Temporal(TemporalType.DATE) private Date startDate; @OneToOne private Address address; @OneToMany(mappedBy="employee") private Collection<Phone> phones = new ArrayList<Phone>(); @ManyToOne private Department department; @ManyToOne private Professor manager; @OneToMany(mappedBy="manager") private Collection<Professor> directs = new ArrayList<Professor>(); @ManyToMany(mappedBy="employees") private Collection<Project> projects = new ArrayList<Project>(); public int getId() { return id; } public void setId(int empNo) { this.id = empNo; } public String getName() { return name;...

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

JPA 查询IN示例

JPA教程 – JPA查询IN示例 IN表达式可以检查单值路径表达式是否是集合的成员。 集合可以内联定义为一组字面值,或者可以从子查询派生。 以下代码显示了如何在JPQL中使用IN运算符。 List l = em.createQuery( "SELECT DISTINCT p FROM Professor e, IN(e.phones) p") .getResultList(); 以下代码显示如何使用NOT IN运算符 List l = em.createQuery( "SELECT e FROM Professor e WHERE e.address.state NOT IN ("NY", "CA")") .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 DISTINCT p FROM Professor e, IN(e.phones) p") .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); Phone phone = new Phone(); phone.setId(1); phone.setNumber("111-111-1111"); phone.setProfessor(p); em.persist(p); em.persist(phone); em.persist(d); } private static void printResult(Object result) {...

JPA 一对多映射示例

JPA教程 – JPA一对多映射示例 以下代码显示了如何在JPA中执行一对多映射。 部门可以有一个或多个员工。在Java代码中,我们可以创建一个Java Bean来表示Employee的Employee类,以及一个Department类来表示一个部门。 当链接员工和部门时,我们引用Employee的Department类如下。 @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne private Department department; 我们使用 @ManyToOne 来标记部门以指示关系部门和员工之间。 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne private Department department; public Person() {} public Person(String...

JPA 多对一两个连接列示例

JPA教程 – JPA多对一两个连接列示例 以下代码显示了如何在多对一映射中设置连接列。 @ManyToOne @JoinColumns({ @JoinColumn(name="DEPT_NAME", referencedColumnName="Name"), @JoinColumn(name="DEPT_ID", referencedColumnName="ID") }) private Department department; 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderBy; @Entity public class Department implements java.io.Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToMany(targetEntity=Person.class, mappedBy="department") @OrderBy("name ASC") private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Department [id=" + id + ", name=" +...

JPA 查询like Escape示例

JPA教程 – JPA查询like Escape示例 如果模式字符串包含应匹配的下划线或百分号,则ESCAPE子句可以使用\指定转义字符: SELECT d FROM Department d WHERE d.name LIKE 'QA\_%' ESCAPE '\' 例子 下面的代码来自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 d FROM Department d WHERE d.name LIKE "Q\\_%" ESCAPE "\\"") .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"); } else if (result...

JPA 查询本地查询结果类示例

JPA教程 – JPA查询本地查询结果类示例 以下代码显示如何创建本机命名查询。 @Entity @Table(name = "EMP") @NamedNativeQuery(name = "nativeSQL", query = "SELECT emp1.emp_id, emp1.name, emp1.manager_id, " + "emp1.dept_id, emp1.address_id " + "FROM EMP emp1, EMP emp2 " + "WHERE ((emp2.EMP_ID = ?) AND (emp2.EMP_ID = emp1.MANAGER_ID))", resultClass = Professor.class) public class Professor { 例子 下面的代码来自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(); Object l = em.createNamedQuery("findSalary").getSingleResult(); printResult(l); } 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) {...

JPA 查询集合大小示例

JPA教程 – JPA查询集合大小示例 我们可以使用SIZE函数来获取实体计数。 以下代码选择具有两个员工的部门。 "SELECT d FROM Department d WHERE SIZE(d.employees) = 2") 例子 下面的代码来自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 d FROM Department d WHERE SIZE(d.employees) = 2") .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); Phone phone = new Phone(); phone.setId(1); phone.setNumber("111-111-1111"); phone.setProfessor(p); em.persist(p); em.persist(phone); em.persist(d); } private static void printResult(Object result) { if (result == null) { System.out.print("NULL"); } else if (result instanceof Object[]) { Object[] row = (Object[]) result; System.out.print("["); for (int i...

JPA 一对多排序示例

JPA教程 – JPA一对多排序示例 以下代码显示如何在一对多映射中按属性设置顺序。 @OrderBy(“name ASC")设置按人员名称设置的人员排序。 @OneToMany(targetEntity=Person.class, mappedBy="department") @OrderBy("name ASC") private Set<Person> persons = new HashSet<Person>(); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test(){ Department d = new Department(); d.setName("Design"); Person p1 = new Person("Tom"); p1.setDepartment(d); Person p2 = new Person("Jack"); p2.setDepartment(d); d.getPersons().add(p1); d.getPersons().add(p2); em.persist(p1); em.persist(p2); em.persist(d); } @PersistenceContext private EntityManager em; } 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderBy; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToMany(targetEntity=Person.class, mappedBy="department") @OrderBy("name ASC") private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons)...