[Mybatis]entity的属性名字和数据库字段对应不上的解决方式

EG:数据库表中的数据为last_name 而 实体类的数据为 lastName

解决方式

  1. SQl语句中起别名和实体类的属性对应即可
    select last_name lastName age age from a_admin

  2. Mybatis中开启驼峰命名,需要在Mybatis的配置文件中开启

<settings>
	<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
  1. 使用高级映射在resultMap中自定义映射然后在查询到的Sql语句中映射实体类
<resultMap id="BaseResultMap"
		type="com.sli.crowd.entity.Admin">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="login_acct" property="loginAcct"
			jdbcType="VARCHAR" />
		<result column="user_pswd" property="userPswd" jdbcType="CHAR" />
		<result column="user_name" property="userName"
			jdbcType="VARCHAR" />
		<result column="email" property="email" jdbcType="VARCHAR" />
		<result column="create_time" property="createTime"
			jdbcType="CHAR" />
</resultMap>
posted @ 2022-04-20 10:25  1_f  阅读(335)  评论(0)    收藏  举报