java面试题 MyBatis中当实体类中的属性名和表中的字段名不同解决方法

1 写sql语句的时候起别名

select id,u_name uname ,u_age age from emp;   则会将数据库中的u_name 映射为实体类中uname属性上

在MyBatis的全局配置文件中开启驼峰命名规则  可以将数据库中的下划线映射为驼峰命名  注意 数据库中的下划线必须是挨着的

  <settings>

          <setting name="mapUnderscoreToCamelCase"  value="true"/>   

</settings>

在Mapper映射文件中使用resultMap来自定义高级映射

 <select id="select01" resultMap="MyMap">

  select * from emp where id=#{id}

</select >

<resultMap  type="com.atguigu.com.entities.emp"  id="MyMap"

   <!--映射主键-->

    <id column="id"  property="id"/>

    <!--映射其他列  property中就对应实体类中的属性名-->

<result  column="e_name"  property="ename"/>

</resultMap>

猜你喜欢

转载自www.cnblogs.com/weiikun/p/10988066.html
今日推荐