从数据库中获取数据时,我在控制台中得到奇怪的输出
I am getting Weird output in console when fetching Data from Database
我正在尝试获取一些数据,我正在使用 H2 数据库。型号是
@Entity
public class Employee {
@Id
private long empid;
private String empname;
private Date empdoj;
private double emptotalsalary;
private String emptype;
服务class是我使用方法的地方
@Autowired
EmployeeRepository repo;
public int countAllEmployees() {
return (int)repo.count();
}
public List<Employee> getPermanentEmployees(String employeetype){
Employee filter = new Employee();
filter.setEmptype(employeetype);
Example<Employee> example = Example.of(filter);
List<Employee> emplist = (List<Employee>)repo.findAll();
return emplist;
}
public List<Employee> getEmployeeNames(){
Employee filter = new Employee();
filter.setEmpname("tha");
ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING).withIgnoreCase();
Example<Employee> example = Example.of(filter,matcher);
return (List<Employee>)repo.findAll();
}
在控制台中我得到这个输出
Employee Count
5
Get List of permanent Employees
[com.example.demo.Employee@416c1b0, com.example.demo.Employee@a302f30, com.example.demo.Employee@4af44f2a, com.example.demo.Employee@47994b51, com.example.demo.Employee@143fe09c]
Get List of all Employees Ending with Tha
com.example.demo.Employee@416c1b0
com.example.demo.Employee@a302f30
com.example.demo.Employee@4af44f2a
com.example.demo.Employee@47994b51
com.example.demo.Employee@143fe09c
我不明白为什么这是 coming.please 帮助。
我想你的意思是像 com.example.demo.Employee@143fe09c
这样的文字很奇怪
在您的代码中的某处,您正在打印员工记录。例如:System.out.println(employee)
System.out.println
使用对象的 toString() 方法来打印对象。由于您没有在 Employee class 中提供它,因此它使用 Employee 的 superclass 中的 toString 方法,即 Object.
如文档中所述,它打印 classname 后跟 @ 和哈希。
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--
Returns a string representation of the object. In general, the
toString method returns a string that "textually represents" this
object. The result should be a concise but informative representation
that is easy for a person to read. It is recommended that all
subclasses override this method.
The toString method for class Object returns a string consisting of
the name of the class of which the object is an instance, the at-sign
character `@', and the unsigned hexadecimal representation of the hash
code of the object. In other words, this method returns a string equal
to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
只需覆盖 Employee class 中的 toString 方法即可生成 Employee 对象的文本表示。您的 IDE 已经提供了生成此方法的好方法。
@Override
public String toString() {
return "Employee{" + "empid=" + empid +
", empname=" + empname + "'" +
'}';
}
我正在尝试获取一些数据,我正在使用 H2 数据库。型号是
@Entity
public class Employee {
@Id
private long empid;
private String empname;
private Date empdoj;
private double emptotalsalary;
private String emptype;
服务class是我使用方法的地方
@Autowired
EmployeeRepository repo;
public int countAllEmployees() {
return (int)repo.count();
}
public List<Employee> getPermanentEmployees(String employeetype){
Employee filter = new Employee();
filter.setEmptype(employeetype);
Example<Employee> example = Example.of(filter);
List<Employee> emplist = (List<Employee>)repo.findAll();
return emplist;
}
public List<Employee> getEmployeeNames(){
Employee filter = new Employee();
filter.setEmpname("tha");
ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING).withIgnoreCase();
Example<Employee> example = Example.of(filter,matcher);
return (List<Employee>)repo.findAll();
}
在控制台中我得到这个输出
Employee Count
5
Get List of permanent Employees
[com.example.demo.Employee@416c1b0, com.example.demo.Employee@a302f30, com.example.demo.Employee@4af44f2a, com.example.demo.Employee@47994b51, com.example.demo.Employee@143fe09c]
Get List of all Employees Ending with Tha
com.example.demo.Employee@416c1b0
com.example.demo.Employee@a302f30
com.example.demo.Employee@4af44f2a
com.example.demo.Employee@47994b51
com.example.demo.Employee@143fe09c
我不明白为什么这是 coming.please 帮助。
我想你的意思是像 com.example.demo.Employee@143fe09c
在您的代码中的某处,您正在打印员工记录。例如:System.out.println(employee)
System.out.println
使用对象的 toString() 方法来打印对象。由于您没有在 Employee class 中提供它,因此它使用 Employee 的 superclass 中的 toString 方法,即 Object.
如文档中所述,它打印 classname 后跟 @ 和哈希。 https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
只需覆盖 Employee class 中的 toString 方法即可生成 Employee 对象的文本表示。您的 IDE 已经提供了生成此方法的好方法。
@Override
public String toString() {
return "Employee{" + "empid=" + empid +
", empname=" + empname + "'" +
'}';
}