MyBatis中当实体类中的属性名和表中的字段名不一样怎么办

MyBatis中当实体类中的属性名和表中的字段名不一样怎么办

    • 解决方案:

解决方案:

  • 写sql语句时起别名
  • 在MyBatis的全局配置文件中开启驼峰命名规则
<configuration>
 	<settings>
		 
 		<setting name="mapUnderscoreToCamelCase" value="true"/>
 	settings>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			dataSource>
		environment>
	environments>
	
	<mappers>
		<mapper resource="EmployeeMapper.xml" />
	mappers>
  • 在Mapper映射文件中使用resultMap来自定义映射规则


<mapper namespace="com.atguigu.mybatis.mapper.EmployeeMapper">
	
	<select id="getEmployeeById" resultMap="myMap">
		select * from employees where id = #{id}
	select>
	
	
    <resultMap type="com.atguigu.mybatis.entities.Employee" id="myMap">
    	
    	<id column="id" property="id"/>
    	
    	<result column="last_name" property="lastName"/>
    	<result column="email" property="email"/>
    	<result column="salary" property="salary"/>
    	<result column="dept_id" property="deptId"/>
    resultMap>
mapper>

你可能感兴趣的:(面试)