今天我在测试mybatis的时候,发现查询出来的一些字段为null,而且这些字段都是驼峰命名了的。所以我首先就想到了是数据库表字段和类映射出了问题。
mybatisplus是mybatis的升级版,所以配置还是有点儿差别,如果是mybatis的话,直接在configuration中配置就行了,如下:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="cacheEnabled" value="false"/>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="logImpl" value="LOG4J2"/>
settings>
configuration>
但是mybatisplus的驼峰命名配置是在MP全局配置中完成的
废话不说,直接上代码
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<property name="idType" value="0"/>
<property name="dbColumnUnderline" value="true"/>
bean>
直接上代码:
<resultMap id="userVoResultMap" type="com.ranqing.model.vo.UserVo">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="login_name" property="loginName" jdbcType="VARCHAR"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="sex" property="sex" jdbcType="TINYINT"/>
<result column="age" property="age" jdbcType="TINYINT"/>
<result column="user_type" property="userType" jdbcType="TINYINT"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="organization_id" property="organizationId" jdbcType="INTEGER"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="phone" property="phone" jdbcType="VARCHAR"/>
<result column="organizationName" property="organizationName" jdbcType="VARCHAR"/>
<collection property="rolesList" ofType="Role">
<id column="roleId" property="id"/>
<result column="roleName" property="name"/>
collection>
resultMap>
<sql id="Base_Column_List">
id, login_name AS loginName, name, password, salt, sex, age, phone, user_type AS userType, status, organization_id AS organizationId, create_time AS createTime
sql>
<select id="selectUserByLoginNameAndPwd" resultType="com.ranqing.model.vo.UserVo">
SELECT
<include refid="Base_Column_List"/>
from user
where login_name=#{loginName} and password=#{password}
select>
注意:
1、凡是需要驼峰的字段都要用“as”取别名,然后直接resultType到UserVo上
2、这种方法不用驼峰命名也没关系,
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<property name="idType" value="0"/>
<property name="dbColumnUnderline" value="false"/>
bean>
dbColumnUnderline为true或false都可以
先上代码:
<resultMap id="userVoResultMap" type="com.ranqing.model.vo.UserVo">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="login_name" property="loginName" jdbcType="VARCHAR"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="sex" property="sex" jdbcType="TINYINT"/>
<result column="age" property="age" jdbcType="TINYINT"/>
<result column="user_type" property="userType" jdbcType="TINYINT"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="organization_id" property="organizationId" jdbcType="INTEGER"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="phone" property="phone" jdbcType="VARCHAR"/>
<result column="organizationName" property="organizationName" jdbcType="VARCHAR"/>
<collection property="rolesList" ofType="Role">
<id column="roleId" property="id"/>
<result column="roleName" property="name"/>
collection>
resultMap>
<sql id="Base_Column_List">
id, login_name , name, password, salt, sex, age, phone, user_type , status, organization_id , create_time
sql>
<select id="selectUserByLoginNameAndPwd" resultMap="userVoResultMap">
SELECT
<include refid="Base_Column_List"/>
from user
where login_name=#{loginName} and password=#{password}
select>
注意:
1、这种方法是使用了驼峰命名了的,所有一定要在MP中设置驼峰命名
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<property name="idType" value="0"/>
<property name="dbColumnUnderline" value="true"/>
bean>
dbColumnUnderline必须为true
2、既然使用了驼峰命名,就自然不能再数据库字段上用“as”了。