假如数据库中的命名为stu_id,则实体类中使用驼峰命名法,将下划线后第一个字母大写,则为stuId.
实体类中命名
private int subId;
private String subName;
private int stuId;
数据库命名
create table subject(
sub_id number primary key,
sub_name varchar2(50) not null,
stu_id number references student(stu_id) not null
);
解决方法:mapper.xml结果集映射
<!--type类型是写对应的实体类-->
<resultMap id="BaseResultMap" type="com.gxy.entity.Teacher">
<!--columns主键数据库中的字段名 property实体类的对应属性-->
<id column="t_id" property="tId"></id>
<!--columns普通的数据库中的字段名 property实体类的对应属性-->
<result column="t_name" property="tName"></result>
<result column="t_profession" property="tProfession"></result>
</resultMap>
其中:
- id:指定主键自增
- result:指定非主键自增
- column:数据库中的字段名
- prooerty:实体类中的属性名