本文主要讲述mybatis如何解决mysql的字段名与java实体类的成员变量名称不一致。
public class Employee {
private Integer empId;
private String empName;
private Integer age;
private String gender;
// private Integer deptId;
public Employee() {
}
public Employee(Integer empId, String empName, Integer age, String gender) {
this.empId = empId;
this.empName = empName;
this.age = age;
this.gender = gender;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\\'' +
", age=" + age +
", gender='" + gender + '\\'' +
'}';
}
}
CREATE TABLE t_emp (
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(32) NOT NULL DEFAULT '',
age INT NOT NULL DEFAULT 20,
gender CHAR(1) CHECK(gender IN ('男','女')))
CHARACTER SET utf8 COLLATE utf8_bin ENGINE INNODB;
EmpMapper接口声明如下
public interface EmpMapper {
// 显示所有数据
List<Employee> selectEmpAll();
}
EmpMapper.xml文件声明如下
2024 .