- 在Mybatis的全局配置文件mybatis-config.xml中加入
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
- ssm整合项目中,在spring-mybatis.xml文件中
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!--可以加入驼峰命名法其他mybatis的配置也就是mybatis.cfg.xml的相关配置都会转移到这里-->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
开启驼峰命名的作用: 将数据库带有_的字段与pojo类的属性驼峰命名相关联
例如pojo类有个属性叫 userName,数据库对应字段叫user_name
<select id="queryByName" resultType="com.yangfan.pojo.User">
select *
from user
<where>
<if test="userName !=null and userName!=''">
and user_name = #{userName}
</if>
</where>
</select>
这样通过sql语句查询的结果中的带下划线字段 就可以映射上User类以驼峰命名的属性了,但是不可以写成 select * from user where userName=#{userName}