如何通过数据库在TypeScript class中定义属性?
How to define properties in TypeScript class through the database?
This is my Error when I define properties in my employee.ts
click
export class Employee {
id:number;
fname: string;
lastName:string;
emailId: string; }
&我遇到了这种错误
属性 'id' 没有初始值设定项并且未明确赋值
构造函数。
属性 'fname' 没有初始值设定项并且未明确赋值
构造函数。
属性 'lastName' 没有初始化器,也没有明确赋值
在构造函数中。
属性 'emailId' 没有初始化器,也没有明确赋值
在构造函数中。
这也是我的数据库 click
mysql> SELECT *
-> FROM employees;
+----+-----------------+------------+-----------+
| id | email_id | first_name | last_name |
+----+-----------------+------------+-----------+
| 1 | mara@gmail.com | Amara | Perera |
| 2 | sadun@gmail.com | Sadun | Fernando |
+----+-----------------+------------+-----------+
2 rows in set (0.01 sec)
Here is mY Employee.java file created on eclips IDE
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")//column annotation to provide columns
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email_id")
private String emailId;
public Employee() {
}
//generate constructor
public Employee(String firstName, String lastName, String emailId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
谁能帮我解决这个错误吗??
编译器看到缺少初始化代码的显式类型。您应该将字段标记为可能未定义
class Employee {
id?: number;
}
或在构造函数中初始化它们
export class Employee {
constructor(public readonly id: number) {}
}
TypeScript 正试图警告您这样的情况:
const employee = new Employee();
因为此时employee
中的所有属性都未定义,但您将它们定义为数字和字符串。您可以使属性可选,在构造函数中初始化它们或将以下内容添加到 tsconfig.json:
"strictPropertyInitialization": false
在 Java 你没有这种类型的支票。
This is my Error when I define properties in my employee.ts
click
export class Employee {
id:number;
fname: string;
lastName:string;
emailId: string; }
&我遇到了这种错误
属性 'id' 没有初始值设定项并且未明确赋值 构造函数。
属性 'fname' 没有初始值设定项并且未明确赋值 构造函数。
属性 'lastName' 没有初始化器,也没有明确赋值 在构造函数中。
属性 'emailId' 没有初始化器,也没有明确赋值 在构造函数中。
这也是我的数据库 click
mysql> SELECT *
-> FROM employees;
+----+-----------------+------------+-----------+
| id | email_id | first_name | last_name |
+----+-----------------+------------+-----------+
| 1 | mara@gmail.com | Amara | Perera |
| 2 | sadun@gmail.com | Sadun | Fernando |
+----+-----------------+------------+-----------+
2 rows in set (0.01 sec)
Here is mY Employee.java file created on eclips IDE
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")//column annotation to provide columns
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email_id")
private String emailId;
public Employee() {
}
//generate constructor
public Employee(String firstName, String lastName, String emailId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
谁能帮我解决这个错误吗??
编译器看到缺少初始化代码的显式类型。您应该将字段标记为可能未定义
class Employee {
id?: number;
}
或在构造函数中初始化它们
export class Employee {
constructor(public readonly id: number) {}
}
TypeScript 正试图警告您这样的情况:
const employee = new Employee();
因为此时employee
中的所有属性都未定义,但您将它们定义为数字和字符串。您可以使属性可选,在构造函数中初始化它们或将以下内容添加到 tsconfig.json:
"strictPropertyInitialization": false
在 Java 你没有这种类型的支票。